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).
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).

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).
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! Nothing shows up either on Allocations or 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 ?

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. 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.

链接地址: http://www.djcxy.com/p/30598.html

上一篇: JSON数据在NSURLConnection上返回

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