使用NSURLConnection和dispatch_async时,不会释放内存

时间:2022-05-15 20:59:36

In the interest of knowing how stuff works I have written a simple iPhone application that has a start button. Pressing that button triggers an action to do the following:

为了了解工作原理,我编写了一个简单的iPhone应用程序,它有一个开始按钮。按下该按钮会触发以下操作:

- (IBAction)start:(id)sender {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    @autoreleasepool {
        NSString *urlString = @"http://www.aftonbladet.se";
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
        NSHTTPURLResponse *response = nil;
        NSError *error = nil;
        NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
        if (error) {
            NSLog(@"Error: %@", [error localizedDescription]);
        }
        if (data) {
            NSLog(@"Data length: %d", [data length]);
        }
        if (response) {
            NSLog(@"Status code: %d", [(NSHTTPURLResponse*)response statusCode]);
        }
        [ViewController ReportMemory];
      }
});

}

}

The ReportMemory function looks like this:

ReportMemory函数是这样的:

+ (void)ReportMemory {
struct task_basic_info info;
mach_msg_type_number_t size = sizeof(info);
kern_return_t kerr = task_info(mach_task_self(),
                               TASK_BASIC_INFO,
                               (task_info_t)&info,
                               &size);
if( kerr == KERN_SUCCESS ) {
    NSLog(@"Memory in use: %u kB", info.resident_size/1024);
} else {
    NSLog(@"Error with task_info(): %s", mach_error_string(kerr));
}

}

}

I've read that global queues have autorelease pools but they are only emptied intermittently, so I have tried both with and without the @autoreleasepool macro and I cannot see any difference with regards to memory usage.

我读到全局队列有自动上传池,但它们只是间歇性地清空,所以我尝试了@autoreleasepool宏,也尝试了@autoreleasepool宏,但在内存使用方面我看不到任何区别。

The question is, why does ReportMemory show more and more memory used for each time I press the start button? I would have thought that the auto release pools at some point would be emptied. But in my case it keeps adding up until I get a few memory warnings, and when ReportMemory reports about 400MB used, the application gets shut down.

问题是,为什么每当我按下开始按钮时,ReportMemory会显示越来越多的内存?我会认为自动释放池在某个时刻会被清空。但是在我的情况下,它不断地增加,直到我得到一些内存警告,当ReportMemory报告使用400MB时,应用程序就会被关闭。

Please note, the use of sendSynchronousRequest like this is for demonstration purposes only.

请注意,使用sendsynchronous请求仅用于演示目的。

1 个解决方案

#1


1  

I ran the code you posted in the Allocations Instrument and the only appreciable heap growth I'm seeing from a single, marginal invocation of this method is coming from HTTP cookie storage. Banging on it for a minute or so, the heap allocations looked like this:

我运行了您在分配工具中发布的代码,以及我从一个单一的、边际调用的方法中看到的唯一可感知的堆增长,它来自HTTP cookie存储。敲打它一分钟左右,堆分配看起来是这样的:

使用NSURLConnection和dispatch_async时,不会释放内存

In short, yes it grows, but every once in a while it releases a bunch of accumulated resources. I suspect there's something else going on that's not captured by this code. You should run your (whole) app in Instruments and see what's going on.

简而言之,是的,它会增长,但每隔一段时间它就会释放一堆累积的资源。我怀疑这段代码并没有捕捉到其他东西。你应该在仪器中运行你的(整个)应用,看看发生了什么。

#1


1  

I ran the code you posted in the Allocations Instrument and the only appreciable heap growth I'm seeing from a single, marginal invocation of this method is coming from HTTP cookie storage. Banging on it for a minute or so, the heap allocations looked like this:

我运行了您在分配工具中发布的代码,以及我从一个单一的、边际调用的方法中看到的唯一可感知的堆增长,它来自HTTP cookie存储。敲打它一分钟左右,堆分配看起来是这样的:

使用NSURLConnection和dispatch_async时,不会释放内存

In short, yes it grows, but every once in a while it releases a bunch of accumulated resources. I suspect there's something else going on that's not captured by this code. You should run your (whole) app in Instruments and see what's going on.

简而言之,是的,它会增长,但每隔一段时间它就会释放一堆累积的资源。我怀疑这段代码并没有捕捉到其他东西。你应该在仪器中运行你的(整个)应用,看看发生了什么。