iOS7 UIWebView内存泄露问题解决方法
来源:互联网 发布:windows磁盘碎片整理 编辑:程序博客网 时间:2024/04/30 08:08
关于iOS的UIWebView内存泄露的问题,已经存在了很长时间。一直也没有什么好的解决方法。最近因公司的一个项目,因为内存问题一直闪退。为了解决这个问题,在网上找了很多方法,但是基本上都不怎么好用问题依旧。以前也碰到过这个问题,当时的解决方法就是设置NSURLCache大小。因为iOS当中的网络通讯默认都是通过NSURLConnection来实现的。所以UIWebView内部通讯也是通过NSURLConnection来下载网页资源的。
- (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的时候
_webView.delegate = nil;
[_webView loadHTMLString:@"" baseURL:nil];
[_webView stopLoading];
[_webView removeFromSuperview];
[[NSURLCache sharedURLCache] removeAllCachedResponses];
[_webView release];
但是这么做基本上效果不是很大。囧啊。。
今天偶然的机会看到一篇国外的Blog文章,终于有种找到我想要的答案了。
原文地址是:http://blog.techno-barje.fr//post/2010/10/04/UIWebView-secrets-part1-memory-leaks-on-xmlhttprequest/
有兴趣的朋友可以去看看,大概说的内容就是Html当中的js代码会引起内存泄露的问题。
var xmlhttp = new XMLHttpRequest();xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
// Do whatever you want with the result
}
};
xmlhttp.open("GET", "http://your.domain/your.request/...", true);
xmlhttp.send();
解决这个问题的方法是在webViewDidFinishLoad方法中设置如下:
[[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"WebKitCacheModelPreferenceKey"];
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"WebKitDiskImageCacheEnabled"];//自己添加的,原文没有提到。
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"WebKitOfflineWebApplicationCacheEnabled"];//自己添加的,原文没有提到。
[[NSUserDefaults standardUserDefaults] synchronize];
关于NSUserDefaults的另类用法还有比如设置UserAgent也可以通过NSUserDefaults来设置。
- iOS7 UIWebView内存泄露问题解决方法
- iOS7 UIWebView内存泄露问题解决方法
- iOS7 UIWebView内存泄露问题解决方法
- ios7-uiwebview内存泄露问题解决方法/
- iOS7 UIWebView内存泄露问题解决方法
- iOS7 UIWebView内存泄露问题解决方法
- UIWebView的内存泄露问题解决方法
- UIWebView内存泄露问题解决方法(ios自学笔记)
- cvLoadImage内存泄露问题解决方法
- UIWebView的内存泄露
- iOS UIWebView 内存泄露
- Flex中Module的使用以及内存泄露问题解决方法
- Uiwebview的两种内存泄露
- 优化JS引起UIWebview的内存泄露
- ASIHTTPRequest iOS7下内存泄漏问题解决记录
- 内存泄露检测方法
- 内存泄露检测方法
- NSURLConnection方法内存泄露
- php整数判断
- FaceBook登录
- 带你一起瞧瞧自定义属性以及自定义View的使用
- maven命令管理
- IPA (苹果应用程序文件格式)
- iOS7 UIWebView内存泄露问题解决方法
- QTreeView处理大量数据
- qt拖放 控件之间拖放 自定义数据
- memcache的优点与缺点
- 性别、年龄预测
- 浅谈设计模式之中介者模式
- android logo尺寸
- 浅谈OC中的指针
- java访问控制修饰符