0

I'm working on a stress testing tool, one of designed scenario is to use up memory, and leave only a few mega for the app under test and others. By searching internet about this topic, I learned that Android Dalvik vm limit program allocation in 16MB, 24MB, or 32MB. But I do realized some app can retain up to 80 MB memory. So is that idea reasonable? if so, how to achieve that goal.

thanks in advance, Forrest

forrestg
  • 327
  • 5
  • 13

2 Answers2

0

I would use one or several services to consume memory. This article tells us that Leaving a service running when it’s not needed is one of the worst memory-management mistakes:

https://developer.android.com/training/articles/memory.html#Services

and this one tells us which process is killed last:

http://developer.android.com/guide/components/processes-and-threads.html#Lifecycle

In addition, I would use a foreground activity to cause killing the app under test (and see if the data survive): when the memory is already allocated to services, a foreground activity would cause killing of the background activities.

Android is a Linux, so you can use stuff like /proc/$PID/maps (see here).

It's interesting to know what you will use, so please answer your own question after you solve the problem.

Community
  • 1
  • 1
18446744073709551615
  • 16,368
  • 4
  • 94
  • 127
0

thanks following up with me. Now I believe I get an answer to this question.

Generally speaking, we cannot rely on one app/service to use up the whole system RAM. Andoid app runs in Dalvik vm and there's an upper limit for each application or service. It's also known as maximum heap size limit. Different phones has different limit heap size:

  • HUAWEI U9508: 256MB android 4.0.4
  • Meizu MX3: 192MB android 4.2.1* ll

We can get this heap size by code:

Runtime.getRuntime().maxMemory();

Android Service fall into this rule as well, the heap size limit is the same as normal app. Initially, android os give the app a relatively small heap, when the app reach the capacity, memory manager will grow the heap size automatically. Later, if app reaches the heap size limit, you'll get OutOfMemoryError. See below log(copy to text editor for better reading):

02-15 09:22:25.438: D/dalvikvm(9992): GC_FOR_ALLOC freed 140K, 11% free 2943K/3280K, paused 11ms, total 11ms 02-15 09:22:25.478: I/dalvikvm-heap(9992): Grow heap (frag case) to 27.059MB for 25165840-byte allocation 02-15 09:22:25.478: D/dalvikvm(9992): threadid=1: still suspended after undo (sc=1 dc=1) 02-15 09:22:25.488: D/dalvikvm(9992): threadid=1: still suspended after undo (sc=1 dc=1) 02-15 09:22:25.488: D/dalvikvm(9992): GC_CONCURRENT freed 15K, 2% free 27504K/27860K, paused 1ms+2ms, total 13ms 02-15 09:22:25.488: D/dalvikvm(9992): WAIT_FOR_CONCURRENT_GC blocked 12ms 02-15 09:22:43.758: I/Choreographer(9992): Skipped 1100 frames! The application may be doing too much work on its main thread. 02-15 09:22:43.913: D/dalvikvm(9992): GC_FOR_ALLOC freed 2K, 2% free 27504K/27856K, paused 25ms, total 25ms 02-15 09:22:43.963: I/dalvikvm-heap(9992): Grow heap (frag case) to 51.044MB for 25165840-byte allocation 02-15 09:22:43.978: D/dalvikvm(9992): GC_FOR_ALLOC freed <1K, 1% free 52080K/52436K, paused 14ms, total 14ms 02-15 09:22:43.998: D/dalvikvm(9992): GC_CONCURRENT freed <1K, 1% free 52080K/52436K, paused 2ms+4ms, total 21ms 02-15 09:22:52.578: D/dalvikvm(9992): GC_FOR_ALLOC freed 4K, 1% free 52080K/52432K, paused 12ms, total 12ms 02-15 09:22:52.613: I/dalvikvm-heap(9992): Grow heap (frag case) to 75.044MB for 25165840-byte allocation 02-15 09:22:52.633: D/dalvikvm(9992): GC_CONCURRENT freed 0K, 1% free 76656K/77012K, paused 3ms+4ms, total 20ms 02-15 09:22:52.633: D/dalvikvm(9992): WAIT_FOR_CONCURRENT_GC blocked 17ms 02-15 09:22:52.633: D/dalvikvm(9992): WAIT_FOR_CONCURRENT_GC blocked 17ms 02-15 09:22:55.198: D/dalvikvm(9992): GC_FOR_ALLOC freed 1K, 1% free 76656K/77012K, paused 13ms, total 13ms 02-15 09:22:55.233: I/dalvikvm-heap(9992): Grow heap (frag case) to 99.044MB for 25165840-byte allocation 02-15 09:22:55.253: D/dalvikvm(9992): GC_CONCURRENT freed 0K, 1% free 101232K/101592K, paused 1ms+5ms, total 17ms 02-15 09:22:55.253: D/dalvikvm(9992): WAIT_FOR_CONCURRENT_GC blocked 17ms 02-15 09:22:55.253: D/dalvikvm(9992): WAIT_FOR_CONCURRENT_GC blocked 17ms 02-15 09:22:56.438: D/dalvikvm(9992): GC_FOR_ALLOC freed <1K, 1% free 101232K/101592K, paused 17ms, total 17ms 02-15 09:22:56.478: I/dalvikvm-heap(9992): Grow heap (frag case) to 123.044MB for 25165840-byte allocation 02-15 09:22:56.493: D/dalvikvm(9992): GC_FOR_ALLOC freed 0K, 1% free 125808K/126172K, paused 18ms, total 18ms 02-15 09:22:56.523: D/dalvikvm(9992): GC_CONCURRENT freed <1K, 1% free 125808K/126172K, paused 2ms+7ms, total 27ms 02-15 09:22:57.458: D/dalvikvm(9992): GC_FOR_ALLOC freed <1K, 1% free 125808K/126172K, paused 17ms, total 18ms 02-15 09:22:57.493: I/dalvikvm-heap(9992): Grow heap (frag case) to 147.044MB for 25165840-byte allocation 02-15 09:22:57.513: D/dalvikvm(9992): GC_FOR_ALLOC freed 0K, 1% free 150384K/150752K, paused 18ms, total 18ms 02-15 09:22:57.543: D/dalvikvm(9992): GC_CONCURRENT freed <1K, 1% free 150384K/150752K, paused 3ms+7ms, total 29ms 02-15 09:22:58.668: D/dalvikvm(9992): GC_FOR_ALLOC freed <1K, 1% free 150384K/150752K, paused 17ms, total 17ms 02-15 09:22:58.708: I/dalvikvm-heap(9992): Grow heap (frag case) to 171.044MB for 25165840-byte allocation 02-15 09:22:58.733: D/dalvikvm(9992): GC_FOR_ALLOC freed 0K, 1% free 174960K/175332K, paused 24ms, total 24ms 02-15 09:22:58.768: D/dalvikvm(9992): GC_CONCURRENT freed <1K, 1% free 174960K/175332K, paused 2ms+11ms, total 36ms 02-15 09:22:59.718: D/dalvikvm(9992): GC_FOR_ALLOC freed <1K, 1% free 174960K/175332K, paused 35ms, total 35ms 02-15 09:22:59.718: I/dalvikvm-heap(9992): Forcing collection of SoftReferences for 25165840-byte allocation 02-15 09:22:59.778: D/dalvikvm(9992): GC_BEFORE_OOM freed 10K, 1% free 174950K/175332K, paused 58ms, total 58ms 02-15 09:22:59.778: E/dalvikvm-heap(9992): Out of memory on a 25165840-byte allocation. 02-15 09:22:59.778: I/dalvikvm(9992): "main" prio=5 tid=1 RUNNABLE 02-15 09:22:59.778: I/dalvikvm(9992): | group="main" sCount=0 dsCount=0 obj=0x40dd19a0 self=0x40bc6010 02-15 09:22:59.778: I/dalvikvm(9992): | sysTid=9992 nice=0 sched=0/0 cgrp=apps handle=1074504668 02-15 09:22:59.778: I/dalvikvm(9992): | state=R schedstat=( 0 0 0 ) utm=295 stm=49 core=0 02-15 09:22:59.778: I/dalvikvm(9992): at com.jdmobile.stresstestingutility.MainActivity$2.onClick(MainActivity.java:~148) 02-15 09:22:59.778: I/dalvikvm(9992): at android.view.View.performClick(View.java:4203) 02-15 09:22:59.778: I/dalvikvm(9992): at android.view.View$PerformClick.run(View.java:17556) 02-15 09:22:59.778: I/dalvikvm(9992): at android.os.Handler.handleCallback(Handler.java:725) 02-15 09:22:59.778: I/dalvikvm(9992): at android.os.Handler.dispatchMessage(Handler.java:92) 02-15 09:22:59.778: I/dalvikvm(9992): at android.os.Looper.loop(Looper.java:137) 02-15 09:22:59.778: I/dalvikvm(9992): at android.app.ActivityThread.main(ActivityThread.java:5105) 02-15 09:22:59.778: I/dalvikvm(9992): at java.lang.reflect.Method.invokeNative(Native Method) 02-15 09:22:59.778: I/dalvikvm(9992): at java.lang.reflect.Method.invoke(Method.java:511) 02-15 09:22:59.778: I/dalvikvm(9992): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 02-15 09:22:59.778: I/dalvikvm(9992): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 02-15 09:22:59.778: I/dalvikvm(9992): at dalvik.system.NativeStart.main(Native Method) 02-15 09:23:52.643: D/AndroidRuntime(9992): Shutting down VM

The other memory allocation limit is a chunk of memory(no fragment): 16MB, 24MB, or bigger, see this piece of code demonstration, ActivityManager:

/**
 * Return the approximate per-application memory class of the current
 * device.  This gives you an idea of how hard a memory limit you should
 * impose on your application to let the overall system work best.  The
 * returned value is in megabytes; the baseline Android memory class is
 * 16 (which happens to be the Java heap limit of those devices); some
 * device with more memory may return 24 or even higher numbers.
 */
public int getMemoryClass() {
    return staticGetMemoryClass();
}
forrestg
  • 327
  • 5
  • 13