Java – Android WebView – JavaScript memory leak

Android WebView – JavaScript memory leak… here is a solution to the problem.

Android WebView – JavaScript memory leak

I’m having Javascript problems in WebView (using webview.getSettings().setJavascriptEnabled(true); Enabled) The HTML page embedded in Javascript loads and displays correctly, however, what I’m also doing is constantly refreshing the screen every 10 or 30 seconds. When I get into the enclosure of my test device (MK808 mini TV), I use “dumpsys meminfo” to see the memory of the process. Displaying the memory of my application process every 5 seconds, I can see that the memory usage of my application jumps by (4-10) megabytes whenever the page refreshes. This will last for about 30 minutes or so until the application crashes with a fatal signal 11 error in WebViewCoreThread and no stack trace.

Increasing memory (starting at around 15000kb):

enter image description here


These memory issues or errors do not occur when JavaScript is not enabled.

Before each refresh, I clear the cache (true) and freeMemory(), as well as all other databases and caches.

I’ve also tried destroying the WebView after each refresh and creating a brand new one, but to no avail.

I can easily redo this issue by creating a new application project and creating a webview that controls the handler reload based on the time. This seems like this should be a general problem, but I can’t find anything on this topic. Could it be my device?

Thanks in advance for your help, I’d be happy to add more information as needed!

Solution

Try adding this:

  //Injects the supplied Java object into this WebView. The object is injected into the 
    JavaScript context of the main frame, using the supplied name. This allows the 
    Java object's public methods to be accessed from JavaScript.
    WebView.addJavascriptInterface(new JavaScriptInterface(this), "Android");

Related Problems and Solutions