聲明:本人不是原創(chuàng),本人只是問(wèn)題解決的搬運(yùn)工。
解決方式:iOS8的用WKWebView,7的用UIWebView加下邊的部分優(yōu)化。
問(wèn)題參考:轉(zhuǎn)自:iOS7 UIWebView內(nèi)存泄露問(wèn)題解決方法 http://blog.csdn.net/primer_programer/article/details/24855329
一篇國(guó)外的Blog文章:
原文地址是:http://blog.techno-barje.fr//post/2010/10/04/UIWebView-secrets-part1-memory-leaks-on-xmlhttprequest/
(不愿意點(diǎn)開(kāi)看的同學(xué)可也在最下面看,純英文,本人懶英語(yǔ)差)
問(wèn)題描述:每次加載webView時(shí)候內(nèi)存劇增,導(dǎo)致程序反應(yīng)開(kāi)始變慢。
問(wèn)題出現(xiàn)的原因:
解決思路:先百度,后谷歌,實(shí)在不行問(wèn)大哥!
解決辦法:
- (void)webViewDidFinishLoad:(UIWebView *)webView{
// 防止內(nèi)存飆升
[[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"WebKitCacheModelPreferenceKey"];
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"WebKitDiskImageCacheEnabled"];//自己添加的,原文沒(méi)有提到。
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"WebKitOfflineWebApplicationCacheEnabled"];//自己添加的,原文沒(méi)有提到。
[[NSUserDefaults standardUserDefaults] synchronize];
}
原文:
UIWebView Secrets - Part1 - Memory Leaks on XmlhttprequestOCT 4TH, 2010My first blog post on iphone subject reveal a big memory bug when using UIWebView component. This is the (only one) component to display some HTML content in an iphone interface. UIWebView object has a lot of differents issues and I’m going to highlight the biggest of them. Actually, all XMLHttpRequests used in javascript code are fully leaking!!! I mean when you do a request that retrieve 100ko of data, your memory used grow up for 100ko! This bug is not always active, but almost always. In fact the trigger to enable it is to simply open one link in your UIWebView. For example, clicking on alink.
But let’s look at a memory usage graph while we execute this simple test application: memory usage graph
Create the UIWebView object
Load a local HTML test file
Execute 3 XMLHttpRequest to google.com, notice how the memory is freed three times after each request!
Trigger the leak by opening a page that redirect back to our test file
Execute the same 3 XMLHttpRequest and look how much memory is used and totally leaked :/
We clean the HTML document with document.body.innerHTML=”; (sometimes free some memory, when we have a lot of DOM objects)
release the UIWebView (almost no memory freed, next post is going to analyse that)
Test Application
So, to sum up, usually, when you execute this Javascript in a UIWebView:
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();
Your are going to have a big memory usage and leak a lot of data!
But there is a hack to solve this problem: revert what is done when you open a link.
In fact, the key property which leads to this leak is the WebKitCacheModelPreferenceKey application setting. And when you open a link in a UIWebView, this property is automatically set to the value "1". So, the solution is to set it back to 0 everytime you open a link. You may easily do this by adding a UIWebViewDelegate to your UIWebView :
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"WebKitCacheModelPreferenceKee you going to have much less crash due to "Low Memory" :)
聯(lián)系客服