Webview Memory leakage solution 1: webview Leakage

Source: Internet
Author: User

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.

  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.