ios uiwebview 内存的内存问题还是没解决吗

iOS7 UIWebView内存泄露问题解决方法 - 简书
iOS7 UIWebView内存泄露问题解决方法
关于iOS的UIWebView内存泄露的问题,已经存在了很长时间。一直也没有什么好的解决方法。最近因公司的一个项目,因为内存问题一直闪退。为了解决这个问题,在网上找了很多方法,但是基本上都不怎么好用问题依旧。以前也碰到过这个问题,当时的解决方法就是设置NSURLCache大小。因为iOS当中的网络通讯默认都是通过NSURLConnection来实现的。所以UIWebView内部通讯也是通过NSURLConnection来下载网页资源的。
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
int cacheSizeMemory = 1*; // 4MB
int cacheSizeDisk = 5*; // 32MB
NSURLCache *sharedCache = [[[NSURLCache alloc] initWithMemoryCapacity:cacheSizeMemory diskCapacity:cacheSizeDisk diskPath:@"nsurlcache"] autorelease];
[NSURLCache setSharedURLCache:sharedCache];
并且在收到内存警告的时候,清除缓存内容
_webView.delegate =
[_webView loadHTMLString:@"" baseURL:nil];
[_webView stopLoading];
[_webView removeFromSuperview];
[[NSURLCache sharedURLCache] removeAllCachedResponses];
[_webView release];
但是这么做基本上效果不是很大。囧啊。。今天偶然的机会看到一篇国外的Blog文章,终于有种找到我想要的答案了。原文地址是:
有兴趣的朋友可以去看看,大概说的内容就是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来设置。关于iOS的UIWebView内存泄露的问题,以前也碰到过这个问题,解决方法就是设置NSURLCache大小。因为iOS当中的网络通讯默认都是通过NSURLConnection来实现的。所以UIWebView内部通讯也是通过NSURLConnection来下载网页资源的。- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions{& & int cacheSizeMemory = 1*; // 4MB& & int cacheSizeDisk = 5*; // 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 =[_webView loadHTMLString:@&& baseURL:nil];[_webView stopLoading];[_webView removeFromSuperview];[[NSURLCache sharedURLCache] removeAllCachedResponses];[_webView release];这么做基本上效果不是很大看到一篇国外的Blog文章,找到我想要的答案。原文地址是:http://blog.techno-barje.fr//post//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来设置。
最新教程周点击榜
微信扫一扫关于UIWebView内存不足的问题
关于UIWebView内存不足的问题
最近做的demo,设计到电子书展示的部分,我的电子书是epub3.0的模式,设计到html的加载,由于3.0后html中可以加载很多资源,致使一个html网页非常的大。我的一个webView的高度最高可以有16000多像素,我的demo是两个webView循环加载。致使内存占有量很大。运行时发现总是内存不足而崩溃。在网上搜索的很久,发现webView的加载的数据是无法被真正释放的,也就是说当webView调用release时,内存其实没有被释放掉。
现在大家比较管用的方法是在&- (void)applicationDidReceiveMemoryWarning:(UIApplication*)application;
这个方法中添加&& & [[NSURLCache sharedURLCache] removeAllCachedResponses]; 对缓存进行清除,
并在释放webView时填下如下的代码:
_webView.delegate
[_webView loadHTMLString:@&& baseURL:nil];
[_webView stopLoading];
[_webView removeFromSuperview];
[[NSURLCache sharedURLCache] removeAllCachedResponses];
[_webView release];
但是效果甚微。
我从一篇技术博客上看到,在调用JS代码时,会出现一个内存泄露,会将WebKitCacheModelPreferenceKey这个属性置成1;
我的demo中通过JS实现很多OC 和HTML的双向交互,所以JS调用很频繁。
所以要在JS调用结束后 调用,
&[[NSUserDefaults
standardUserDefaults] setInteger:0 forKey:@&WebKitCacheModelPreferenceKey&];
&[[NSUserDefaults standardUserDefaults] synchronize];
这个方法。
同时主要在频繁调用webView 的loadRequest的方法时,请先调用下stopLoading
我的热门文章
即使是一小步也想与你分享内容字号:
段落设置:
字体设置:
UIWebview的内存优化方法
有朋友讨论WKWebview的优势,他确实占用内存更小,渲染网页更快,但是他需要iOS8以上才行,像我现在开发的应用需要支持iOS7,这就必须要UIWebview和WKWebview的混合使用,所以我一开始的版本,确实在iOS7上因为UIWebview的问题导致内存溢出,在网上搜了很多也没有找到相对应的优化方法,好不容易自己摸索了出来,今天公布出来,希望对大家有用,或者大家有更好的方法可以一起交流.
我新创建了一个UIWebView
UIWebView *webView = [[UIWebView alloc] initWithFrame:[UIScreen mainScreen].bounds];
webView.delegate =
NSURL *url = [NSURL URLWithString:@&http://www.qq.com/&];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
[self.view addSubview:webView];
self.webView = webV
当我第一次运行,他的内存是这个样子的,一下子飙到了100多
第一次.png
第一次 内存.png
并且当我点击返回主页的时候,内存并没有随之销毁,或者说只是回收了一部分
并且当我多次点击,内存达到了200多m
但是当我优化后,相同网页第一次加载,内存是这个样子的
当放回上一页,内存是这样的
并且多次点击内存是这样的
只有一次点击,差距其实是挺明显的,你也不要小看这几m的差距,他就能保证我在4s上运行,不至于内存溢出,我是怎么做到的呢?
首先你在APPDelagate,改变缓存策略,使他到达你设定的值后,进行强制内存回收,这样做还有一个好处就是提高网页加载速度
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
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];
然后在初始化webview的.m文件里,当页面消失的时候,强制把webview的delegate设置为nil
-(void)dealloc {
self.webView.delegate =
好了,这样你就可以愉快地在iOS7上使用UIWebView了,有用的话记得点个赞:blush:
分享给小伙伴们:
本类最热新闻
48小时最热
01020304050607089101112
CopyRight © 2015- , All Rights Reserved.
清屏网 版权所有 豫ICP备号

我要回帖

更多关于 uiwebview 内存泄漏 的文章

 

随机推荐