NSURLConnection is growing in tools, is it NSURLCache?

So, I have a memory problem with my application. The application has MKMapView with MKOverlayRenderer, which loads images on a map. Everything works fine, but after 30-60 minutes the application crashes from memory.

With tools, I found that NSURLConnection is growing, and because I cannot find anything else (besides all the things that I do not understand), I think this is my problem.

Screenshot after launch for 1-2 minutes:

instruments NSURLConnection

Screenshot after launch for 12-13 minutes: instruments after 12-13 minutes

Images are loaded like this in the canDrawMapRect method and saved in the tmp folder (if I disable this, then NSURLConnection will not grow so high):

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
    [NSURLConnection sendAsynchronousRequest:request
                                       queue:[OperationQueues sharedOperationQueue]
                           completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {


                               [data writeToFile:path atomically:YES];

It is then loaded into the drawMapRect method.

I tried to fix this with the following code:

AppDelegate:

int cacheSizeMemory = 4*1024*1024; // 4MB
int cacheSizeDisk = 32*1024*1024; // 32MB
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:cacheSizeMemory diskCapacity:cacheSizeDisk diskPath:@"nsurlcache"];
[NSURLCache setSharedURLCache:sharedCache];

NSURLCache *URLCache = [[NSURLCache alloc] initWithMemoryCapacity:cacheSizeMemory
                                                     diskCapacity:cacheSizeDisk
                                                         diskPath:nil];
[NSURLCache setSharedURLCache:URLCache];

ViewController:

- (void)didReceiveMemoryWarning
{
    [[NSURLCache sharedURLCache] removeAllCachedResponses];
    [super didReceiveMemoryWarning];
}

: []

[[NSURLCache sharedURLCache] removeCachedResponseForRequest:request];

.

  • ?
  • NSURLCache, - ?

:

statisticsenter image description here

+4
1

-, 400 . , , . , , .

-, NSURLCache , - . /, ios 512 000 . 4 194 304 ( ).

, , . , , , .

+2

Source: https://habr.com/ru/post/1568670/


All Articles