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