My favorites | Sign in
Project Home Issues
New issue   Search
for
  Advanced search   Search tips   Subscriptions
Issue 5067: WebView causes memory leak - leaks the parent Activity
33 people starred this issue and may be notified of changes. Back to list
Status:  Released
Owner:  reed%and...@gtempaccount.com
Closed:  Dec 2009


Sign in to add a comment
 
Reported by p.ar...@gmail.com, Nov 26, 2009
Starting an activity with a WebView, then destroying that activity (hitting 
back) and getting a heap dump shows that there are uncleaned up references 
that cause the activity to stay in memory (leak).

This is similar in nature to the accepted bug that I reported for the MapView 
(see https://code.google.com/p/android/issues/detail
id=2181&q=memory%20leak&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars#
c10).
memory_leak_from_webview.PNG
28.3 KB   View   Download
heap-dump-tm1259196627-pid301.hprof
3.6 MB   Download
Nov 26, 2009
#1 p.ar...@gmail.com
For example, one of these leaks occurs because there is a static variable mInstance 
in PluginManager and that keeps a reference to the Context, which is the activity.

http://android.git.kernel.org/?
p=platform/frameworks/base.git;a=blob;f=core/java/android/webkit/PluginManager.java;h
=4588f46e57714c86da3095ea67647aeaca55eb0a;hb=HEAD

Unfortunately, since the context that is passed is the parent Activity (not an 
application context), this causes a problem. The comment says it should be an 
ApplicationContext, but if we follow this all the way up (through the Frame and 
WebViewCore), then we see WebView the regular context, not the application context...

http://android.git.kernel.org/?
p=platform/frameworks/base.git;a=blob;f=core/java/android/webkit/WebView.java#l728

So perhaps the fix is:
Change:
mWebViewCore = new WebViewCore(context, this, mCallbackProxy, javascriptInterfaces);
To:
mWebViewCore = new WebViewCore(context.getApplicationContext(), this, mCallbackProxy, 
javascriptInterfaces);

Nov 26, 2009
#2 p.ar...@gmail.com
Or clean up this stuff in WebViewCore.destroy()

http://android.git.kernel.org/?
p=platform/frameworks/base.git;a=blob;f=core/java/android/webkit/WebViewCore.java;h=a5a
48527a389609e40da14ecad8c12dd8377cfaf;hb=HEAD#l1411
Nov 26, 2009
#3 p.ar...@gmail.com
These memory leaks only seem to be there for Android 2.0 and are not there in 1.6.
Nov 30, 2009
#4 romaingu...@gtempaccount.com
(No comment was entered for this change.)
Status: Reviewed
Owner: r...@android.com
Dec 1, 2009
#5 romaingu...@gtempaccount.com
(No comment was entered for this change.)
Status: FutureRelease
Feb 22, 2011
#6 mi...@zoodles.com
I've found the workaround in Comment #1:

mWebViewCore = new WebViewCore(context.getApplicationContext(), this, mCallbackProxy, 
javascriptInterfaces);

is not usable when the Webview is displaying Flash content.  When WebView is created with the Application context, the Flash plugin crashes on a ClassCastException while trying to cast the Application context to an Activity context.

W/dalvikvm(13293): threadid=1: thread exiting with uncaught exception (group=0x4001d7d0)
E/AndroidRuntime(13293): FATAL EXCEPTION: main
E/AndroidRuntime(13293): java.lang.ClassCastException: com.zoodles.kidmode.ZoodlesApplication
E/AndroidRuntime(13293): 	at com.adobe.flashplayer.FlashPaintSurface.getBrowserActivity(FlashPaintSurface.java:483)
E/AndroidRuntime(13293): 	at com.adobe.flashplayer.FlashPaintSurface$2.surfaceCreated(FlashPaintSurface.java:178)
E/AndroidRuntime(13293): 	at android.view.SurfaceView.updateWindow(SurfaceView.java:534)
E/AndroidRuntime(13293): 	at android.view.SurfaceView.dispatchDraw(SurfaceView.java:341)
E/AndroidRuntime(13293): 	at android.view.View.draw(View.java:6743)
E/AndroidRuntime(13293): 	at android.view.SurfaceView.draw(SurfaceView.java:327)
E/AndroidRuntime(13293): 	at android.view.ViewGroup.drawChild(ViewGroup.java:1640)
E/AndroidRuntime(13293): 	at android.webkit.WebView.drawChild(WebView.java:4011)

We're looking for suggestions for another way to work around this memory leak.

Apr 23, 2011
#7 burton.m...@noticesoftware.com
You could keep a single static instance of the WebView, attaching and detaching it as needed.
Dec 8, 2011
#8 mr.chris...@gmail.com
Seems like a quick fix. Why hasn't this been fixed yet? I'm having the same problem.
Aug 16, 2012
#9 mr.chris...@gmail.com
Has anyone found a workaround for this when the Webview displays Flash content? This is a bit of a double edged sword. If you don't pass the Application context, you get a memory leak. If you do pass the Application context, your app crashes when Flash content is on the page. 
Aug 17, 2012
#10 andy.den...@gmail.com
I'm having the same issue with AdMob AdView objects.  Prior to integrating with AdMob, I used the application context workaround, but AdViews don't work if they are inflated via an Application context -- they want an Activity context.  Anybody figured out another workaround?
Jun 23, 2013
#11 jbq@android.com
(No comment was entered for this change.)
Status: Released
Sign in to add a comment

Powered by Google Project Hosting