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:

Screenshot after launch for 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;
int cacheSizeDisk = 32*1024*1024;
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];
.
:

