NSString stringWithFormat调用XX次会导致内存警告

时间:2012-09-24 09:31:44

标签: iphone objective-c memory-management memory-leaks nsstring

我开始使用此处描述的技术监控应用内存使用情况: Programmatically retrieve memory usage on iPhone

我写了3个测试来试试,这就是我发现的:

- (void)test1 {
for (int i = 0; i < 1000; i++) {
    NSMutableString *str = [NSMutableString stringWithString:@""];
    for (int j = 0; j < 1000; j++) {
        [str appendString:@"some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string "];
    }
}    

- (void)test2 {
for (int i = 0; i < 100000; i++) {
    @autoreleasepool {
        NSString *stri = @"";
        stri = [NSString stringWithFormat:@"%d some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really", i];
    }
}


- (void)test3 {
NSString *str = @"";
for (int i = 0; i < 500; i++) {
    str = [str stringByAppendingFormat:@"%d some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really", i];
}

当我调用test1或test3内存被正确分配和解除分配时 - 我可以使用上面链接中描述的report_memory函数看到它。 但是当我调用test2时,内存不会被释放 - report_memory会上升。如果我多次调用test2,我的应用程序会收到内存警告并被终止。

我正在使用ARC。谁能解释一下这里发生了什么?

3 个答案:

答案 0 :(得分:1)

stringWithFormat返回自动释放的NSString对象。因此,当在该线程结束时释放自动释放池时,将释放该对象。

答案 1 :(得分:1)

钉了它!!!我启用了Zombie Objects并忘记了它们,因此即使在我使用@autoreleasepool块时,任何已分配的对象都没有被释放。

答案 2 :(得分:0)

这是因为您在每次迭代时使用单独的自动释放实例。第一种情况是您有一个名为str的对象,并且您要附加到同一个实例。这就是记忆没有泄漏的原因。

尝试更改此类调用并检查,

- (void)test2 
{
    NSString *str = @"";
    for (int i = 0; i < 100000; i++) 
    {
       str = [str stringByAppendingFormat:@"%d some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really", i];
    }
}