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?
Generally speaking I agree with Kevin’s comment. I think keeping multiple
Activityobjects around to prevent reloading aWebViewis 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 whichActivity‘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 destroyingActivity‘s when this gets called. You probably need to callsuper.onLowMemory()to make sure the OS handles what it needs to.One possible solution involving controlling the number of activities:
Override
Applicationand create apublic static ArrayList<Activity>that holds 5Activityobjects. Whenever you perform anonCreate()for anActivityyou could add the Activity to theArrayListand then check the size. If size is > 5 then send anintentto theActivityat position 0 that will cause it to handle the intent and callfinish(). Then remove the object from theArrayList.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.