有些東西還是記下來比較好,記記隨筆搏嗡,歡迎批評建議。
前段時間在項目中就用到webview展示大量的新聞資訊頁面拉一,然后就驚喜的出現(xiàn)內(nèi)存泄漏了采盒,于是乎我在網(wǎng)上查了一些資料然后在這里總結(jié)一下解決方法,歡迎拍磚蔚润。
(方法4劃重點)磅氨。
Android混合開發(fā)時經(jīng)常用到WebView加載html等頁面,而WebView的內(nèi)存泄漏就是最經(jīng)常遇到的問題嫡纠,尤其是當項目中需要用webview加載的頁面比較多時烦租。
即使當我退出頁面時在我的BrowserActivity的onDestroy()方法中進行內(nèi)存占用回收(如下圖)但并沒有效果:
mWebView.removeAllViews();
mWebView.destroy();
mWebView=null;
當我點開了多少條新聞內(nèi)存中就存在多少個BrowserActivity的實例,說明我退出時這個BrowserActivity沒有被回收除盏,這樣的話當我瀏覽的新聞比較多時叉橱,內(nèi)存就會累積存在一定的OOM風險,而且新聞界面一般存在大量圖片者蠕,所以這個問題是必須要解決的窃祝。
1. new一個而不是在.xml中定義webview節(jié)點
attention:最初在寫這篇的時候這一小節(jié)可能寫的不夠嚴謹,要是造成誤解真是抱歉踱侣;寫這篇小結(jié)時的目的也是想把知道的一些解決方法記下來方便自己查看粪小,沒想到能收到評論和質(zhì)疑,我還是很開心的抡句,但是通過這個我也發(fā)現(xiàn)探膊,發(fā)出來的東西還是要寫的嚴謹一些,會慢慢改進的待榔。所以這一小節(jié)重新說明了一下逞壁,要是有不對的地方還是歡迎大家拍磚。
不要在布局文件中定義webview的節(jié)點究抓,而是在需要的時候動態(tài)生成猾担。你可以在需要webview的布局位置放一個LinearLayout袭灯,需要時在代碼中動態(tài)生成webview并add進去:
//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時new WebView(...);到底是傳入ApplicationContext還是Activity的context刺下,說法不一,但是網(wǎng)上較為一致的觀點是采用application的context稽荧。
傳ApplicationContext貌似可以防止webview對activity的引用而造成的內(nèi)存泄漏橘茉;但是在很多情況下會報錯,但是這個出錯應(yīng)該是webview的某些特殊動作產(chǎn)生由Application到Activity的類型轉(zhuǎn)換錯誤工腋;
采用activity的context細想來貌似和在xml中直接定義沒有什么區(qū)別;
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中調(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. 進程
為加載WebView的界面開啟新進程趁冈,在該頁面退出之后關(guān)閉這個進程。
這個方法我沒有測試拜马,不知道應(yīng)用和效果如何渗勘,有興趣的可以試試。
4. 從根源解決(劃重點)
前面的方法都沒有解決我內(nèi)存泄漏的問題俩莽,然后我看到了一篇文章是從源碼角度分析了webview內(nèi)存泄漏的原因旺坠,最后按作者的方法解決了問題,后面會貼上原文地址扮超。這里簡單說一下:
原文里說的webview引起的內(nèi)存泄漏主要是因為org.chromium.android_webview.AwContents 類中注冊了component callbacks取刃,但是未正常反注冊而導致的。
org.chromium.android_webview.AwContents 類中有這兩個方法 onAttachedToWindow 和 onDetachedFromWindow出刷;系統(tǒng)會在attach和detach處進行注冊和反注冊component callback璧疗;
在onDetachedFromWindow() 方法的第一行中:
if (isDestroyed()) return;,
如果 isDestroyed() 返回 true 的話馁龟,那么后續(xù)的邏輯就不能正常走到病毡,所以就不會執(zhí)行unregister的操作;我們的activity退出的時候屁柏,都會主動調(diào)用 WebView.destroy() 方法啦膜,這會導致 isDestroyed() 返回 true;destroy()的執(zhí)行時間又在onDetachedFromWindow之前淌喻,所以就會導致不能正常進行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();
// 退出時調(diào)用此方法,移除綁定的服務(wù)匕荸,否則某些特定系統(tǒng)會報錯
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
附上檢查內(nèi)存泄漏的工具:leakcanary