Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 903
David González Avatar asked May 30 '26 10:05

David González


1 Answers

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().

like image 117
marcinj Avatar answered Jun 02 '26 00:06

marcinj