WebView - memory leak to plain code on devices <= 4.1

my test code:

public class MyActivity extends AppCompatActivity {
    WebView myWebView;
    RelativeLayout WebViewLayout;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mylayout);
        WebViewLayout = (RelativeLayout) findViewById(R.id.webViews);
        myWebView = new WebView(this);
        WebViewLayout.addView(myWebView);
    }
}

      

causing a memory leak in a WebView on my 4.1 device (I am using MAT for analysis):

Class Name
java.lang.Thread @ 0x4212f6b8 WebViewCoreThread Thread
'- <Java Local>, target android.webkit.WebViewCore$WebCoreThread @ 0x4205d390
'- core android.webkit.WebViewCore @ 0x4212f608
'- mContext com.packagename.MyActivity

      

I added onDestroy ():

@Override
public void onDestroy() {
    WebViewLayout.removeAllViews();
    if(myWebView != null) {
        myWebView.removeAllViews();
        myWebView.onPause();
        myWebView.getSettings().setJavaScriptEnabled(false);
        myWebView.destroy();
        myWebView = null;
    }
    super.onDestroy();
}

      

But on 4.1 the memory leak persists. I tried to use getApplicationContext () instead of [this] but the result is the same.

There are no memory leaks on 4.4 device. Is this a bug in WebView for <= 4.1? Can this be fixed?

Thank!

+3


source share





All Articles