美文网首页IOSiOS开发iOS 开发
iOS 关于加载webView内存剧增的问题

iOS 关于加载webView内存剧增的问题

作者: 蓝摇扼剑 | 来源:发表于2016-04-08 09:13 被阅读3569次

    声明:本人不是原创,本人只是问题解决的搬运工。

    解决方式:iOS8的用WKWebView,7的用UIWebView加下边的部分优化。

    问题参考:转自:iOS7 UIWebView内存泄露问题解决方法 http://blog.csdn.net/primer_programer/article/details/24855329

    一篇国外的Blog文章:

    原文地址是:http://blog.techno-barje.fr//post/2010/10/04/UIWebView-secrets-part1-memory-leaks-on-xmlhttprequest/

    (不愿意点开看的同学可也在最下面看,纯英文,本人懒英语差)

    问题描述:每次加载webView时候内存剧增,导致程序反应开始变慢。

    问题出现的原因:

    解决思路:先百度,后谷歌,实在不行问大哥!

    解决办法:

    - (void)webViewDidFinishLoad:(UIWebView *)webView{

    // 防止内存飙升

    [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"WebKitCacheModelPreferenceKey"];

    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"WebKitDiskImageCacheEnabled"];//自己添加的,原文没有提到。

    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"WebKitOfflineWebApplicationCacheEnabled"];//自己添加的,原文没有提到。

    [[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:@"WebKitCacheModelPreferenceKey"];

    }

    So are you going to have much less crash due to "Low Memory" :)

    相关文章

      网友评论

      • iOS白水:7没有用的。
        蓝摇扼剑:@超S神 我以为是iPhone7:joy:
        iOS白水:@蓝摇扼剑 真逗。 我说了是iOS7.
        蓝摇扼剑:7用WKWebiview就行了,毕竟7都是9.0以上的系统了。
      • kailee_kg:没什么效果
        蓝摇扼剑:效果不是很大,建议使用WKWebiview
      • 书剑灵:设置完了有效果吗???我这边设置完了都没效果
        蓝摇扼剑:@书剑灵 效果有一点,不是太大,建议使用一个webView加载h5的东西,不要每次都新建一个,在真机上效果明显。

      本文标题:iOS 关于加载webView内存剧增的问题

      本文链接:https://www.haomeiwen.com/subject/hvrvlttx.html