50
51
|
I have an activity using an xml layout where a WebView is embedded. I am not using the WebView in my activity code at all, all it does is sitting there in my xml layout and being visible. Now, when I finish the activity, I find that my activity is not being cleared from memory. (I check via hprof dump). The activity is entirely cleared though if I remove the WebView from the xml layout. I already tried a
in onDestroy() of my activity, but that doesn't help much.
In my hprof dump, my activity (named 'Browser') has the following remaining GC roots (after having called
I found that another developer has experienced similar thing, see the reply of Filipe Abrantes on:http://www.curious-creature.org/2008/12/18/avoid-memory-leaks-on-android/
Now, unfortunately there has been no reply on the blog or the mailing list about this question yet. Therefore I am wondering, is that a bug in the SDK (maybe similar to the MapView bug as reportedhttp://code.google.com/p/android/issues/detail?id=2181) or how to get the activity entirely off the memory with a webview embedded? |
||||||||||||||||
|
35
|
I conclude from above comments and further tests, that the problem is a bug in the SDK: when creating a WebView via XML layout, the activity is passed as the context for the WebView, not the application context. When finishing the activity, the WebView still keeps references to the activity, therefore the activity doesn't get removed from the memory. I filed a bug report for that , see the link in the comment above.
Note that this workaround only works for certain use cases, i.e. if you just need to display html in a webview, without any href-links nor links to dialogs, etc. See the comments below. |
||||||||||||||||||||
|
21
|
I have had some luck with this method: Put a FrameLayout in your xml as a container, lets call it web_container. Then programmatically ad the WebView as mentioned above. onDestroy, remove it from the FrameLayout. Say this is somewhere in your xml layout file e.g. layout/your_layout.xml
Then after you inflate the view, add the WebView instantiated with the application context to your FrameLayout. onDestroy, call the webview's destroy method and remove it from the view hierarchy or you will leak.
Also FrameLayout as well as the layout_width and layout_height were arbitrarily copied from an existing project where it works. I assume another ViewGroup would work and I am certain other layout dimensions will work. This solution also works with RelativeLayout in place of FrameLayout. |
||||||||||||
|
9
|
Here's a subclass of WebView that uses the above hack to seamlessly avoid memory leaks:
To use it, just replace WebView with NonLeakingWebView in your layouts
Then make sure to call Note that this webclient should handle the common cases, but it may not be as full-featured as a regular webclient. I haven't tested it for things like flash, for example. |
||||||||||||||||||||
|
4
|
Based on user1668939's answer on this post (http://stackoverflow.com/a/12408703/1369016), this is how I fixed my WebView leak inside a fragment:
The difference from user1668939's answer is that I have not used any placeholders. Just calling removeAllViews() on the WebvView reference itself did the trick. ## UPDATE ## If you are like me and have WebViews inside several fragments (and you do not want to repeat the above code across all of your fragments), you can use reflection to solve it. Just make your Fragments extend this one:
I am assuming you are calling your WebView field "webView" (and yes, your WebView reference must be a field unfortunately). I have not found another way to do it that would be independent from the name of the field (unless I loop through all the fields and check if each one is from a WebView class, which I do not want to do for performance issues). |
||||
|
3
|
After reading http://code.google.com/p/android/issues/detail?id=9375, maybe we could use reflection to set ConfigCallback.mWindowManager to null on Activity.onDestroy and restore it on Activity.onCreate. I'm unsure though if it requires some permissions or violates any policy. This is dependent on android.webkit implementation and it may fail on later versions of Android.
Calling the above method in Activity
|
||||||||||||
|
2
|
You can try putting the web activity in a seperate process and exit when the activity is destroyed, if multiprocess handling is not a big effort to you. |
||
2
|
I fixed memory leak issue of frustrating Webview like this: (I hope this may help many) Basics:
Turning point: By default, all activities run in same process in one application. (the process is defined by package name). But:
Solution: If a different process is created for an activity, its context can be used to create a webview. And when this process is killed, all components having references to this activity (webview in this case) are killed and the main desirable part is :
Code for help: (one simple case) Total two activities: say A & B Manifest file:
Start A then B
B is created with webview embedded. When backKey is pressed on activity B, onDestroy is called:
and this kills the current process i.e. com.processkill.p3
NOTE: Take extra care while using this kill command. (not recommended due to obvious reasons). Don't implement any static method in the activity (activity B in this case). Don't use any reference to this activity from any other (as it will be killed and no longer available). |
|||
1
|
There is an issue with "app context" workaround: crash when
It could be fixed with |
||||
|