0

I am trying to bullet proof my application so that if it's memory has been reclaimed by the Android OS because of low memory or non-usage when it is pulled to foreground again I can ensure it does not crash.

I have handled the problem of the application being killed and restarted with persisting the data, but I want to be able to recover from the background if the memory has be reclaimed and the app is still running, starting in the Activity it was last running in.

To do this I want to be able to force this situation on the device (and with the app still running). I have seen the SO items for emulating [How do you simulate low memory in the Android emulator?, by killing every Activity https://stackoverflow.com/a/10799988/4107331[1] but I want to have the real deal on the device.

So basically I would like to shove my app to the background, force its memory to be reclaimed, pull it to the Foreground so I can verify that it recovers correctly - all on the device. Simple, right?

Community
  • 1
  • 1
Stephen McCormick
  • 1,706
  • 22
  • 38
  • "I want to be able to recover from the background if the memory has be reclaimed and the app is still running" -- um, there is no such scenario, unless you are referring to your implementation of `onTrimMemory()` or something. The only way that Android will reclaim memory is by terminating your process, at which point your app is no longer running. Or, are you referring to something else? – CommonsWare Dec 31 '15 at 20:03
  • Open Facebook and scroll a couple times or open YouTube and play a video that normally makes the OS simulate this. – Xjasz Dec 31 '15 at 20:03
  • @CommonsWare Are you saying that if I have a application running in the background/or sleeping, that theAndroid OS will only kill the application and not just take memory? Then why am I seeing null pointer exceptions on global variables after the app sleeps or is forced to the background? And why a need for onSaveInstanceState() and onRestoreInstanceState? I probably am misunderstanding what is actually happening. – Stephen McCormick Dec 31 '15 at 21:12
  • "Are you saying that if I have a application running in the background/or sleeping, that theAndroid OS will only kill the application and not just take memory?" -- correct. "Then why am I seeing null pointer exceptions on global variables after the app sleeps or is forced to the background?" -- since I have never seen your code nor your stack trace, I cannot answer that. Probably your process was terminated. "And why a need for onSaveInstanceState() and onRestoreInstanceState?" -- they are for process termination and configuration changes. – CommonsWare Dec 31 '15 at 21:16
  • @CommonsWare So if my process is being killed, how are the users accessing it to cause the null pointer (referencing the global variables)? And if the entire app is killed, is that when I check for the existence of the bundle to re-create the Activity and global memory in the onRestoreInstanceState? thanks! – Stephen McCormick Dec 31 '15 at 21:49
  • "how are the users accessing it to cause the null pointer (referencing the global variables)?" -- recent-tasks list (a.k.a., overview screen), most likely. – CommonsWare Dec 31 '15 at 21:56

2 Answers2

5

I use an app called "Fill Ram Memory" (https://play.google.com/store/apps/details?id=me.empirical.android.application.fillmemory&hl=en_GB) to achieve this. It does what it says on the tin, a byproduct of which is the OS will be forced to kill non critical processes (i.e your app).

Note the OS will try to keep foreground processes running as long as possible so if your app is running a foreground service it is less likely to be killed. Also i would advise finding a device with a smaller amount of total memory to begin with as it will make the process quicker.

Martin Price
  • 629
  • 7
  • 7
  • Worked great for me on 2020. About filling 90% of memory partially killed my app. Hence on, I could tested what I wanted. – March3April4 Jan 21 '20 at 18:58
3

Use "DO not keep activities" from developer option in settings.

nkit
  • 156
  • 4
  • 1
    Not quite sure that does what I need. I set that but my global memory is still there when the returning from that killed activity. What I am seeing in production is a null pointer on some global variables. I assumed what was happening is that Android was reclaiming memory and that I would have to do something onSaveInstanceState to store that memory. I just want a mechanism to test that code. – Stephen McCormick Dec 31 '15 at 21:14
  • Whenever you go to another activity, it kills last activity. If you move back to last activity, then it is recreated. – nkit Dec 31 '15 at 21:19
  • DO not keep activities wont destroy the global app context, we want to be able to destroy the global app context so that at rerun it will try to start from application onCreate() and starts the last opened activity – Nishant Pardamwar May 23 '17 at 11:06