Android app: 内存怎么用完了,只剩20-100MB

Android app: how can I use up memory, leaving only 20-100 MB

我正在开发一个压力测试工具,设计的场景之一是用完内存,只为被测应用程序和其他应用程序留下几兆。 通过在网上搜索有关此主题的信息,我了解到 Android Dalvik vm 将程序分配限制在 16MB、24MB 或 32MB。但我确实意识到某些应用程序可以保留多达 80 MB 的内存。那么这种想法合理吗?如果是这样,如何实现该目标。

提前致谢, 阿甘

我会使用一个或多个服务来消耗内存。 这篇文章告诉我们 在不需要时离开服务 运行 是最严重的内存管理错误之一:

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

这个告诉我们哪个进程最后被杀死:

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

此外,我会使用前台 activity 来杀死正在测试的应用程序(并查看数据是否存在):当内存已经分配给服务时,前台 activity会导致杀死后台活动。

Android 是 Linux,因此您可以使用 /proc/$PID/maps (see here).

之类的东西

知道你会用什么很有趣,所以请在解决问题后回答你自己的问题。

感谢关注我。现在我相信我得到了这个问题的答案。

一般来说,我们不能靠一个app/service来用完整个系统的RAM。 Andoid 应用程序在 Dalvik 虚拟机中运行,每个应用程序或服务都有一个上限。它也称为最大堆大小限制。不同的手机有不同的限制堆大小:

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

我们可以通过代码得到这个堆大小:

Runtime.getRuntime().maxMemory();

Android服务也属于这个规则,堆大小限制与普通应用程序相同。 最初,android os 给应用程序一个相对较小的堆,当应用程序达到容量时,内存管理器会自动增加堆大小。稍后,如果应用程序达到堆大小限制,您将得到 OutOfMemoryError。请参阅下面的日志(复制到文本编辑器以便更好地阅读):

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

另一个内存分配限制是一块内存(无碎片):16MB,24MB,或者更大,看这段代码演示,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();
}