Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to simulate killing of application by Android GC

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!

like image 660
nister Avatar asked Nov 06 '12 14:11

nister


People also ask

How do you end an app on Android?

To Close the Application, you can also take "System. exit(0)" 0 is standard or use any exit code.

How garbage collector works in Android?

Garbage collection A managed memory environment, like the ART or Dalvik virtual machine, keeps track of each memory allocation. Once it determines that a piece of memory is no longer being used by the program, it frees it back to the heap, without any intervention from the programmer.

How to Check low memory in Android?

The Memory Profiler in Android Studio helps you find and diagnose memory issues in the following ways: See how your app allocates memory over time. The Memory Profiler shows a realtime graph of how much memory your app is using, the number of allocated Java objects, and when garbage collection occurs.

How to Check memory usage of Android app programmatically?

Yes, you can get memory info programmatically and decide whether to do memory intensive work. Get Native Allocated Memory by calling: Debug. getNativeHeapAllocatedSize();


1 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.

like image 74
Jan Hudec Avatar answered Nov 15 '22 21:11

Jan Hudec