Webview Memory leakage solution 1: webview Leakage
Recently, in the nested webview of the activity, a large number of pictures and texts are displayed, and the APP memory has been rising, so the memory cannot be released. I checked a lot of information, which is probably a BUG in webview. I referenced the activity to cause memory leakage, so we try to pass getApplicationContext.
1. Avoid writing the webview control directly in xml, which will reference the activity. Therefore, write a LinearLayout in xml and then linearLayout. addView (new MyWebview (getApplicationContext ()));
In this way, dynamic webview generation can avoid Memory leakage. However, this will cause exceptions and program crashes when you click a hyperlink in the webview of some models. The temporary solution is to disable clicking and rewrite webview,
public class MyWebview extends WebView { public MyWebview(Context context) { super(context); } public MyWebview(Context context, AttributeSet attrs) { super(context, attrs); } public MyWebview(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override public boolean onTouchEvent(MotionEvent event) { return false; }}
This avoids program crashes.
2. manually release webview memory when activity is disabled
@Override protected void onDestroy() { super.onDestroy(); if(webview_projectinfo != null){ webview_projectinfo.removeAllViews(); webview_projectinfo.destroy(); webview_projectinfo = null; ll_webview.removeAllViews(); ll_webview = null; }}
The above method can release the memory, but there is a defect, that is, the content of webview cannot be clicked. Another method is to open another process for the nested webview activity and display it as an independent process.