Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'NSData dataWithContentsOfURL:' memory leak iOS 9.x?

My code is the following:

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSString *theURLString = @"http://website.com/musicFile";
    NSData *theData = [NSData dataWithContentsOfURL:[NSURL URLWithString:theURLString]];
}

There is nothing special at all. I am not even using the background thread.

Here is the behavior I get on iOS 8.x (and the behavior that I expect to get):

enter image description here

So, NSData is fully released and all of the occupied memory is back.

However, iOS 9.x surprised me a lot:

enter image description here

My questions are:

  1. Approximately 100 MB are gone for nothing in iOS 9.x. How can I get them back? Are there any workarounds?

  2. iOS 8.x has occupied 136.2 MB at max, while iOS 9.x used 225.9 MB at max. Why is this happening?

  3. What is going on in iOS 9.x?

UPDATE #1:

I have also tried using NSURLSession 'dataTaskWithURL:completionHandler:' (thanks to @CouchDeveloper). This reduces the leak, but doesn't fully solve the problem (this time both iOS 8.x and iOS 9.x).

I used the code below:

NSURLSession *theURLSession = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSURLSessionDataTask *theURLSessionDataTask = [theURLSession dataTaskWithURL:[NSURL URLWithString:theURLString]
                                                               completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error)
                                                   {
                                                       NSLog(@"done");
                                                   }];
[theURLSessionDataTask resume];

enter image description here

As you can see, 30 MB are still lost.

UPDATE #2:

The above tests where done using Xcode simulator.

However, I have also decided to test on actual iOS 9.2 iPhone 4S (as recommended by @Sohil R. Memon).

The results of 'NSData dataWithContentsOfURL:' are below:

enter image description here

The results of using 'NSURLSession dataTaskWithURL:completionHandler:' are below:

enter image description here

It looks like 'NSData dataWithContentsOfURL:' works perfectly on actual device, while 'NSURLSession dataTaskWithURL:completionHandler:' -- doesn't.

However, does anyone know solutions which show identical information on BOTH actual device AND Xcode simulator?

like image 752
OlDor Avatar asked Nov 17 '25 08:11

OlDor


2 Answers

Approximately 100 MB are gone for nothing in iOS 9.x. How can I get them back? Are there any workarounds?

For a couple of reasons, we should use NSURLSession to download data from a web service. So, this is not a workaround, but the correct approach.

What is going on in iOS 9.x?

I have no idea - possibly cached data, network buffers, or some other issues. But this is irrelevant - you should try the correct approach with NSURLSession.

From the docs:

IMPORTANT

Do not use this synchronous method to request network-based URLs. For network-based URLs, this method can block the current thread for tens of seconds on a slow network, resulting in a poor user experience, and in iOS, may cause your app to be terminated.

Instead, for non-file URLs, consider using the dataTaskWithURL:completionHandler: method of the NSURLSession class. See URL Session Programming Guide for details.

Edit:

Those "reasons" are:

  • NSURLSession is specifically designed to load remote resources.

  • NSURLSession methods are asynchronous which is crucial for methods which complete only after a perceivable duration (it will not block the calling thread).

  • A session can handle authentication by means of a default method or with a custom delegate.

  • Session tasks can be cancelled.

like image 176
CouchDeveloper Avatar answered Nov 18 '25 23:11

CouchDeveloper


Here is also an answer which helped me. The answer states to use [NSData dataWithContentsOfURL:url options:0 error:&error]; instead.

Hope this helps

like image 36
Saheb Roy Avatar answered Nov 18 '25 22:11

Saheb Roy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!