2

我开始使用此处描述的技术监控应用程序内存使用情况: 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。谁能解释这里发生了什么?

4

3 回答 3

1

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

于 2012-09-24T09:41:35.440 回答
1

搞定了!!!我启用了僵尸对象并忘记了它们,因此即使我使用 @autoreleasepool 块,任何分配的对象都没有被释放。

于 2012-09-26T11:25:22.917 回答
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];
    }
}
于 2012-09-24T09:40:28.683 回答