Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
329 views
in Technique[技术] by (71.8m points)

java - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...