简体   繁体   English

NSURLConnection在下载大部分数据时导致内存警告

[英]NSURLConnection causing memory warning while downloading large portion of data

I am facing a disturbing problem with NSURLConnection on iOS (5.1, 6.0, 6.1). 我在iOS(5.1,6.0,6.1)上遇到了NSURLConnection令人不安的问题。
When I try to download large portion of data via http request, my app does not mark the released data as really released (I will explain that later). 当我尝试通过http请求下载大部分数据时,我的应用程序并未将发布的数据标记为真正发布(稍后我会解释)。

My download code looks like this: 我的下载代码如下所示:

NSURL *url = [NSURL URLWithString:@"http://my.server/file.zip"];  
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];  
NSURLConnection *urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];  
[urlConnection start];

The delegate is my view controller. 代表是我的视图控制器。
This code is invoked on main runloop (no dispatch queue or perform selector stuff like that). 在主runloop上调用此代码(没有调度队列或执行这样的选择器)。
Delegate does ABSOLUTELY NOTHING with the received data - no retain, no copying - nothing. 代表对所收到的数据绝对不做 - 没有保留,没有复制 - 什么都没有。

The problem is, that when I invoke this code with a newly created project - it behaves fine - downloads all the data and finishes without any problem. 问题是,当我使用新创建的项目调用此代码时 - 它表现良好 - 下载所有数据并完成而没有任何问题。

BUT when the code is invoked in my other application, suddenly all of the downloaded data leaves a memory footprint in the application, and after a while the memory runs out, app gets memory warning and crashes. 但是当在我的其他应用程序中调用代码时,突然所有下载的数据都会在应用程序中留下内存空间,一段时间后内存耗尽,应用程序会收到内存警告并崩溃。

You might say - your delegate keeps the data and does not release it. 您可能会说 - 您的委托保留数据但不会释放它。 Well, I tried to find the solution with Instruments. 好吧,我试着用仪器找到解决方案。 Guess what - my app shows on allocations that it keeps about 10 mb of ram while downloading the resource! 猜猜是什么 - 我的应用程序在分配时显示它在下载资源时保持大约10 mb的内存! Nothing shows up either on Allocations or VM Tracker. 在Allocations或VM Tracker上都没有显示任何内容。

So how do I know the app keeps the memory footprint of downloaded data? 那么我怎么知道应用程序会保留下载数据的内存空间?

This is the code to show me my memory report (found it earlier in some answer on this site - can't find it right now though: 这是向我展示我的记忆报告的代码(在本网站的某些答案中较早发现 - 现在无法找到它:

    -(void) report_memory {  
       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 ) {  
          STLog(ST_DEBUG, @"Memory in use (in bytes): %u b ( %u mb )", info.resident_size, info.resident_size / (1024 * 1024) );  
       } else {  
          STLog(ST_DEBUG, @"Error with task_info(): %s", mach_error_string(kerr));  
       }  
   }

My question to you is - what is going on here? 我的问题是 - 这里发生了什么?
Why does the app shows some memory footprint? 为什么该应用程序会显示一些内存占用?
why does the instruments shows no excessive allocation? 为什么仪器没有过多的分配?
What can be the cause of excessive allocation by the NSURLConnection ? NSURLConnection可能导致过度分配的原因是什么?

PS. PS。 I tried also to change the shared cache size on disk, in memory, use other cache policies - nothing helped :( 我还尝试更改磁盘上的共享缓存大小,在内存中,使用其他缓存策略 - 没有任何帮助:(

Turns out that when using zombie objects, the NSURLConnection keeps deallocated objects. 事实证明,当使用僵尸对象时,NSURLConnection会保留释放的对象。 This prevents them to fully deallocate - hence the memory footprint. 这可以防止它们完全解除分配 - 从而导致内存占用。

The solution is NOT to use zombie objects while downloading large portions of data via NSURLConnection. 解决方案是在通过NSURLConnection下载大部分数据时不使用僵尸对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM