Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android WebView Memory Leak when using Assets

I've recently noticed that when I load a webview based off code from a file (using assets or a local directory), the WebView will leak the memory once the destroy() method is called. I've narrowed it down to a referencing issue. The program/webview makes copies of the original view and loses the references in the JNI table - this eventually causes the program to crash after 512 entries.

I have seen this article, but the non-leaking webview fix does not seem to work with HTML in the assets or external folders.

Memory leak in WebView

The issue does not exist when the application is entirely server based; but, I need the application to be able to "sync" data and update the offline repo with these views.

Further info: I have taken the webviews and put them in a ViewPager to allow for scrolling. This issue did not exist in 3.2.4/1 but has appeared in 4.0.3. I get a lot of JNI Warnings, (failed to find entry) and they still exist after the views are destroyed. I've looked at it through the hprof, and I have noticed that the HTML is staying residual in lost variables.

Any light on how to correct this behavior would be appreciated.

Edit:

Specifics on the crash

Failed adding to JNI local ref table (has 512 entries)
"Thread-375" prio=5 tid=15 RUNNABLE
| group="main" sCount=0 dsCount=0 obj=0x41504a90 self=0x1a9cf80
| sysTid=10314 nice=0 sched=0/0 cgrp=default handle=27159240
| schedstat=( 455083000 66745000 774 ) utm=30 stm=15 core=3

at android.content.res.AssetManager.readAsset(Native Method)
at android.content.res.AssetManager.access$700(AssetManager.java:35)
at android.content.res.AssetManager$AssetInputStream.read(AssetManager.java:573)
at dalvik.system.NativeStart.run(Native Method)

UPDATE:

To fix this issue, you have to load from the SD Card, or some other storage mechanism. The bug is permanent when a Javascript file is loaded, I don't know if it's fixed in Chrome, but it does not seem to be fixed in Browser in the newest OS.

like image 666
Alex Kelly Avatar asked Feb 14 '12 13:02

Alex Kelly


1 Answers

.destroy() --> Destroys the internal state of this WebView. This method should be called after this WebView has been removed from the view system.

like image 50
nurisezgin Avatar answered Oct 08 '22 22:10

nurisezgin