iOS Memory issue (ARC) when downloading and saving large ammount of images from server

断了今生、忘了曾经 提交于 2019-12-03 00:43:06

Based on your screenshot, I think the issue is with the NSURL caching rather than the actual NSData objects. Can you try the following:

In your Application Delegate, setup an initial URL cache:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Do initial setup
    int cacheSizeMemory = 16*1024*1024; // 16MB
    int cacheSizeDisk = 32*1024*1024; // 32MB
    NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:cacheSizeMemory diskCapacity:cacheSizeDisk diskPath:@"nsurlcache"];
    [NSURLCache setSharedURLCache:sharedCache];

    // Finish the rest of your didFinishLaunchingWithOptions and head into the app proper
}

Add the following to your Application Delegate:

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
    [[NSURLCache sharedURLCache] removeAllCachedResponses];
}

A cache file will be created:"Library/Caches/your_app_id/nsurlcache"

The link to the Apple example is here: URL Cache

Code not tested but this (or something similar) should sort your problem + plus you can experiment with the cache sizes.

Can you post another screenshot of Allocations in action with this code? I would expect to see memory use stop growing and flatten out.

Michael Dautermann

"dataWithContentsOfURL" returns an autoreleased NSData object and that doesn't normally get released until the end of the run loop or the end of the method, so it's no wonder you're filling up memory pretty quickly.

Change it to an explicit "initWithContentsOfURL" method and then force a release by doing "data = nil;" when you are absolutely done with the image data.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!