Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to control memory usage when calling multiple WebView in Android?

My app's main activity is a Activity that contains a Webview to load web pages.

I override the method shouldOverrideUrlLoading(WebView view, String url) to make every URL request call up an Intent and load in a new same activity containing WebView.

Doing this is to provide a better experience when BACK key is pressed, will just finish the current activity and back to the former activity, need no time to render the page again comparing to use goBack() in single webview.

But now the problem is that, after I open many URLs, creating a long queue of activitys in the background, the memory it uses became large.

When I go back to launcher and check the progresses, I can see my app caches more than 200M data. This is not acceptable...

And it's interesting that I can see my app used up my memory, but in the Heap view of DDMS in Eclipse I can see the app allocated no more than 10M memory. So I guess the 200M is webStorage cached by Webview?

Is there any way to control the memory?

I'm considering just save maybe 5 layers of activities at a time and when go back 5 times just jump back to home page. But still don't know how to release memory beside the 5 activities I need, which I'll never use again?

Or if it's because the WebView is keeping web page cached automatically, how can I manager this manually? Such as setting a limit of maximum cache size or page count?

like image 385
Aloong Avatar asked Jan 17 '12 11:01

Aloong


People also ask

How do I reduce the memory usage on my phone?

Uninstall apps you no longer need. Disable preinstalled apps you don't use by going to Settings > Apps & notifications > See all X apps, locating the app, and tapping Disable. Install any software updates as they become available—both for apps and the Android system. Find alternative apps that use less memory.

Can we cache WebView in android?

You can use the WebView cache to enable caching in WebView.


1 Answers

Generally speaking I agree with Kevin's comment. I think keeping multiple Activity objects around to prevent reloading a WebView is counter-intuitive to a mobile environment with such limited resources.

That being said, you have a lot of different question and solution possibilities, so I don't have a single answer in mind. Look through these links to see if anything is helpful:

ActivityManager - has a ton of stuff you might be able to use, check out it's sub-classes.

ActivityManager.RunningTaskInfo - Never used it, but seems to have some useful stuff, especially with determining which Activity's are running.

ActivityManager.MemoryInfo - Can give you info on the available system memory as well as a bottom threshold of memory.

Application.onLowMemory() -Tells you when your app has been a memory hog. You could override this method and start destroying Activity's when this gets called. You probably need to call super.onLowMemory() to make sure the OS handles what it needs to.

One possible solution involving controlling the number of activities:

Override Application and create a public static ArrayList<Activity> that holds 5 Activity objects. Whenever you perform an onCreate() for an Activity you could add the Activity to the ArrayList and then check the size. If size is > 5 then send an intent to the Activity at position 0 that will cause it to handle the intent and call finish(). Then remove the object from the ArrayList.

Shouldn't be too much work, but the down-side is that you have to manually manage things. I am sure there is a more savvy solution possible.

like image 131
Jon Avatar answered Oct 24 '22 02:10

Jon