在开发中遇到了UIWebview内存回收问题,由于页面主要是由JS加载,引发了内存泄露.使用下面方法基本没有作用
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions { int cacheSizeMemory = 1*1024*1024; // 4MB int cacheSizeDisk = 5*1024*1024; // 32MB NSURLCache *sharedCache = [[[NSURLCache alloc] initWithMemoryCapacity:cacheSizeMemory diskCapacity:cacheSizeDisk diskPath:@"nsurlcache"] autorelease]; [NSURLCache setSharedURLCache:sharedCache]; }
并且在收到内存警告的时候,清除缓存内容。
- (void)applicationDidReceiveMemoryWarning:(UIApplication*)application { [[NSURLCache sharedURLCache] removeAllCachedResponses]; }
这些措施主要是回收UIWebview的缓存,但是无法清除由于JS引起的内存泄露,后来翻墙查询到一种解决办法:
在webViewDidFinishLoad方法里添加可以减小webview内存过大的问题 [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"WebKitCacheModelPreferenceKey"]; [[NSUserDefaults standardUserDefaults] synchronize];
现在也可以使用WKWebview来优化,但是由于对读取iOS8本地文件有问题,只能使用在iOS9以上的系统中