Android Question Ram usage with webview and jpegs

JoeR

Member
Licensed User
Longtime User
Can anybody advise on how I can "reclaim" ram when using
webview to display many screens of locally-stored html.

It is as though memory for webviews is never released until the app is killed.

Each webview screen typically contains text and 1 jpeg of around 50K.

Speed of displaying screens and of scrolling becomes progressively slower
as more pages are viewed. After 85 webviews, scrolling becomes unusable.
Screen display time is still tolerable, but gradually slowing.

I used task manager to monitor ram usage:
On initial loading it is 7.8mb. After displaying 4 webview screens it is 10.72mb
This rises to 17mb after 36 "webviews" and to 28mb after 85 webviews.
If I go backwards/forwards displaying the same screen repeatedly, ram usage continues to rise.

My app has 300 jpegs (10mb of storage).
My apk is around 10mb
 

JoeR

Member
Licensed User
Longtime User
Thanks. I think I may have solved the problem another way.

I have repositioned my dim and initialise statements for the webview. They were being run each time instead of once.
Sorry if a newbie has wasted some of your time.
----------------------------------------------------------------------
However - when I tried your suggested code (using JavaObject version 1.00 to get it to compile)
I could not get it to work. Hopefully I do not need it now, but it would be nice to learn how to make it work for the future.
I am testing on Android version 2.3.6 Gingerbread.

I received error java.lang.RuntimeException: Method: clearCache Not matched.

Do I have to include some other code to make clearcache available to my program?
Any of the following which are shown in the documentation?
B4X:
 class JsObject {
    @JavascriptInterface
    public String toString() { return "injectedObject"; }
}
webView.addJavascriptInterface(new JsObject(), "injectedObject");
webView.loadData("", "text/html", null);
webView.loadUrl("javascript:alert(injectedObject.toString())");
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Maybe clear_cache is not available on Android 2.3? I Don´t know.

You can try webviewextras lib. There is a documented way to clear cache from a webview.

B4X:
www.LoadUrl("http://www.domain.com") ' www = WebView
MyWebViewExtras.clearCache(www,True) ' MyWebViewExtras = WebViewExtras

Your
webView.addJavascriptInterface(new JsObject(), "injectedObject");

let´s me directly think about webviewextras cause i knew webviewextras has a addjavascriptinterface... So i have had a look whether it has a clear cache too... I have not tried it but i found out that there is such a sub in webviewextras. :)
 
Last edited:
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Maybe clear_cache is not available on Android 2.3? I Don´t know.

Sorry my fault, the boolean should be that and not a string:

B4X:
Dim JO As JavaObject = WebView
JO.RunMethod("clearCache",Array As Object(True)) 'False to exclude disk files
 
Upvote 0
Top