Home » Android » java – How to simulate killing of application by Android GC

java – How to simulate killing of application by Android GC

Posted by: admin May 14, 2020 Leave a comment

Questions:

I need to test serialization\deserialization of application in next cases:

  • app was in background a lot of time (idle mode) and was killed by GC;
  • app was in background and was killed by GC by reason of resources (memory\cp) lack;

On some devices it can be simulated by launching 1-2 games.
But on quad-core devices with 1gb memory it’s very-very hard with 4-10 heavy games and takes a lot of time.

I try to implement some demo where emulating loading on resources:

  • create bitmaps arrays
  • create objects arrays
  • launch a lot of services
  • launch a lot of activities

But no result, application still works (even on old devices) and my demo is crashed with OutOfMemoryException.

How can i simulate high load in demo application?

Thanks!

How to&Answers:

Well, the “GC” is actually abused “Out Of Memory Killer” and that kills the applications as if by signal 9. In rooted device you should be able to invoke kill(1) command from shell or kill(2) function from native library (I am not sure whether it’s bound to Java) and kill your application whenever you want.

The system normally calls onStop in the Activity when it’s going to background and than kills the application without further warning and without chance to react. So if you leave the application and kill it, it’s appropriate simulation of it being OOM-killed.

Answer:

Install any memory cleaner on play store. I install this : easymemorycleaner

once u cleaned your memory. Your stored variable inside memory will be gone. Unless you stored data in Parcelable. It will be persistent.