Android Simulator: Easy way to simulate a process restart due to low memory?
Asked Answered
M

4

17

As I just learned, Android reserves the right to kill a background application's process at any moment in order to recycle RAM. The application is still running and can be resumed nevertheless, but all of my static variables are gone (see this article).

I'd like to simulate my app's behavior in this scenario. What's the easiest way to do this? There certainly must be an easier and more predictable way than writing some additional apps that allocate lots of memory.

Mispronounce answered 3/1, 2012 at 10:12 Comment(3)
Does this differ from killing your own process android.os.Process.killProcess(android.os.Process.myPid())?Haircut
@tidbeck: Your guess is as good as mine. I am referring to this article: android-developers.blogspot.com/2010/04/…Mispronounce
possible duplicate of Android: How to force restart service (OS killing on low memory behavior)Waldowaldon
H
10

Found two similar questions Simulate killing of activity in emulator and Simulate low battery & low memory in Android.

Solutions from those questions:

  • Use adb shell and then kill the process with PID from ps
  • Kill it using DDMS
Haircut answered 3/1, 2012 at 22:44 Comment(0)
A
6

short answer: change orientation if you are using an emulator. if using actual device, change settings by going to settings --> developer options --> don't keep activities.

see my answer to this and this for detailed explanation.

Ancel answered 24/2, 2013 at 3:4 Comment(3)
Thanks! I'll check this next time I am updating my Android App.Mispronounce
this tests a configuration change - not a process restart. these are two different things.Instant
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 activityAsbury
C
3

To kill background processes (but still relaunch via history), you can just use activity manager shell command on simulator or device:

$ adb shell am kill com.my.package
Culbertson answered 5/5, 2017 at 14:44 Comment(0)
V
2

An alternative way is to build a super basic app that calls killBackgroundProcesses() for your package, because this method "is the same as the kernel killing those processes to reclaim memory; the system will take care of restarting these processes in the future as needed."

Velum answered 21/5, 2014 at 15:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.