'Leaking MainActivity through WebView

I have an app whose Activity has a fragment with a webView. This webView is being instantiated with an Activity context, as the documentation indicates.

The problem lies when, with the "Don't Keep Activities" flag is turned on, we navigate to another activity and back. This Activity is re-instantiated, but looking at the heap dump I saw a memory leak bind to this Activity.

Looking at the reference trace, OnscreenContentProvider is referenced, a class that is used in WebView class.

This class in turn uses ClassLoaderContextWrapperFactory which contains two static references to a context.

Which seems to indicate that there is a bug and is leaking the context. Has anyone an idea what might be going on or even reproduce it?



Solution 1:[1]

I had similar problem and what helped me was to add:

webView.removeAllViews();
webView.destroy();

to Activity.onDestroy(). I think, its important to call webView.removeAllViews before webView.destroy().

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 marcinj