UIWebView內存佔用太高、崩潰、內存溢出,有須要的朋友能夠參考下。
項目當中模擬器上運行UIWebView讀取本地雜誌,沒問題,真機測試常常內存溢出崩潰。
查了資料由於Html裏的js 致使的內存泄漏,每次打開一個鏈接就會把「WebKitCacheModelPreferenceKey」設置成1。
UIWebView 增長
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"WebKitCacheModelPreferenceKey"];
}
解決問題
緣由:
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" :)
崩潰次數減小不少。 web
國外小哥原文地址:http://blog.techno-barje.fr//post/2010/10/04/UIWebView-secrets-part1-memory-leaks-on-xmlhttprequest/ app