有些東西還是記下來比較好,記記隨筆,歡迎批評建議。
前段時(shí)間在項(xiàng)目中就用到webview展示大量的新聞資訊頁面,然后就驚喜的出現(xiàn)內(nèi)存泄漏了,于是乎我在網(wǎng)上查了一些資料然后在這里總結(jié)一下解決方法,歡迎拍磚。
(方法4劃重點(diǎn))。
Android混合開發(fā)時(shí)經(jīng)常用到WebView加載html等頁面,而WebView的內(nèi)存泄漏就是最經(jīng)常遇到的問題,尤其是當(dāng)項(xiàng)目中需要用webview加載的頁面比較多時(shí)。
即使當(dāng)我退出頁面時(shí)在我的BrowserActivity的onDestroy()方法中進(jìn)行內(nèi)存占用回收(如下圖)但并沒有效果:
mWebView.removeAllViews();
mWebView.destroy();
mWebView=null;
當(dāng)我點(diǎn)開了多少條新聞內(nèi)存中就存在多少個(gè)BrowserActivity的實(shí)例,說明我退出時(shí)這個(gè)BrowserActivity沒有被回收,這樣的話當(dāng)我瀏覽的新聞比較多時(shí),內(nèi)存就會累積存在一定的OOM風(fēng)險(xiǎn),而且新聞界面一般存在大量圖片,所以這個(gè)問題是必須要解決的。
1. new一個(gè)而不是在.xml中定義webview節(jié)點(diǎn)
attention:最初在寫這篇的時(shí)候這一小節(jié)可能寫的不夠嚴(yán)謹(jǐn),要是造成誤解真是抱歉;寫這篇小結(jié)時(shí)的目的也是想把知道的一些解決方法記下來方便自己查看,沒想到能收到評論和質(zhì)疑,我還是很開心的,但是通過這個(gè)我也發(fā)現(xiàn),發(fā)出來的東西還是要寫的嚴(yán)謹(jǐn)一些,會慢慢改進(jìn)的。所以這一小節(jié)重新說明了一下,要是有不對的地方還是歡迎大家拍磚。
不要在布局文件中定義webview的節(jié)點(diǎn),而是在需要的時(shí)候動態(tài)生成。你可以在需要webview的布局位置放一個(gè)LinearLayout,需要時(shí)在代碼中動態(tài)生成webview并add進(jìn)去:
//mWebView=new WebView(this);
mWebView=new WebView(getApplicationContext());
LinearLayout linearLayout = findViewById(R.id.xxx);
linearLayout.addView(mWebView);
然后在onDestroy()方法中調(diào)用:
@Override
protected void onDestroy() {
if( mWebView!=null) {
mWebView.setVisibility(View.GONE);
mWebView.removeAllViews();
mWebView.destroy();
}
super.onDestroy();
}
tips: 關(guān)于創(chuàng)建webview時(shí)new WebView(...);到底是傳入ApplicationContext還是Activity的context,說法不一,但是網(wǎng)上較為一致的觀點(diǎn)是采用application的context。
傳ApplicationContext貌似可以防止webview對activity的引用而造成的內(nèi)存泄漏;但是在很多情況下會報(bào)錯(cuò),但是這個(gè)出錯(cuò)應(yīng)該是webview的某些特殊動作產(chǎn)生由Application到Activity的類型轉(zhuǎn)換錯(cuò)誤;
采用activity的context細(xì)想來貌似和在xml中直接定義沒有什么區(qū)別;
2. 手動刪除引用
這個(gè)方法在我的項(xiàng)目中沒有效果,但原文博主說在他的項(xiàng)目中效果很好,也許對其他人的情況有效,在這里也記下來。
public void setConfigCallback(WindowManager windowManager) {
try {
Field field = WebView.class.getDeclaredField("mWebViewCore");
field = field.getType().getDeclaredField("mBrowserFrame");
field = field.getType().getDeclaredField("sConfigCallback");
field.setAccessible(true);
Object configCallback = field.get(null);
if (null == configCallback) {
return;
}
field = field.getType().getDeclaredField("mWindowManager");
field.setAccessible(true);
field.set(configCallback, windowManager);
} catch(Exception e) {
}
}
然后在activity中調(diào)用:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setConfigCallback(WindowManager)getApplicationContext().getSystemService(Context.WINDOW_SERVICE));
}
public void onDestroy() {
setConfigCallback(null);
super.onDestroy();
}
3. 進(jìn)程
為加載WebView的界面開啟新進(jìn)程,在該頁面退出之后關(guān)閉這個(gè)進(jìn)程。
這個(gè)方法我沒有測試,不知道應(yīng)用和效果如何,有興趣的可以試試。
4. 從根源解決(劃重點(diǎn))
前面的方法都沒有解決我內(nèi)存泄漏的問題,然后我看到了一篇文章是從源碼角度分析了webview內(nèi)存泄漏的原因,最后按作者的方法解決了問題,后面會貼上原文地址。這里簡單說一下:
原文里說的webview引起的內(nèi)存泄漏主要是因?yàn)閛rg.chromium.android_webview.AwContents 類中注冊了component callbacks,但是未正常反注冊而導(dǎo)致的。
org.chromium.android_webview.AwContents 類中有這兩個(gè)方法 onAttachedToWindow 和 onDetachedFromWindow;系統(tǒng)會在attach和detach處進(jìn)行注冊和反注冊component callback;
在onDetachedFromWindow() 方法的第一行中:
if (isDestroyed()) return;,
如果 isDestroyed() 返回 true 的話,那么后續(xù)的邏輯就不能正常走到,所以就不會執(zhí)行unregister的操作;我們的activity退出的時(shí)候,都會主動調(diào)用 WebView.destroy() 方法,這會導(dǎo)致 isDestroyed() 返回 true;destroy()的執(zhí)行時(shí)間又在onDetachedFromWindow之前,所以就會導(dǎo)致不能正常進(jìn)行unregister()。
然后解決方法就是:讓onDetachedFromWindow先走,在主動調(diào)用destroy()之前,把webview從它的parent上面移除掉。
ViewParent parent = mWebView.getParent();
if (parent != null) {
((ViewGroup) parent).removeView(mWebView);
}
mWebView.destroy();
完整的activity的onDestroy()方法:
@Override
protected void onDestroy() {
if( mWebView!=null) {
// 如果先調(diào)用destroy()方法,則會命中if (isDestroyed()) return;這一行代碼,需要先onDetachedFromWindow(),再
// destory()
ViewParent parent = mWebView.getParent();
if (parent != null) {
((ViewGroup) parent).removeView(mWebView);
}
mWebView.stopLoading();
// 退出時(shí)調(diào)用此方法,移除綁定的服務(wù),否則某些特定系統(tǒng)會報(bào)錯(cuò)
mWebView.getSettings().setJavaScriptEnabled(false);
mWebView.clearHistory();
mWebView.clearView();
mWebView.removeAllViews();
mWebView.destroy();
}
super.on Destroy();
}
這個(gè)方法親測有效。
原文地址:http://blog.csdn.net/xygy8860/article/details/53334476?utm_source=itdadao&utm_medium=referral
附上檢查內(nèi)存泄漏的工具:leakcanary