Reputation: 1796
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?
Upvotes: 0
Views: 5877
Reputation: 639
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.
Upvotes: 5