英文:
Application to fully Fill Android RAM
问题
需要编写能够使用所有RAM的Android应用程序。我创建了一个应用程序,在工作线程内无限循环中创建多个MemoryFile。当运行应用程序时,我注意到Android会限制RAM分配,直到达到某个神奇的阈值,同时试图通过关闭其他一些应用程序来释放RAM。最终,要么操作系统终止我的应用程序,要么继续保持RAM接近神奇的阈值。少于一半的空闲RAM保持可用。如何使用所有RAM?
我尝试过:
- 尝试在已Root的设备上使用
sudo mount -t tmpfs tmpfs /sdcard/tmp
。然而,在我这个特定的情况下,搭载了Oreo 8.1的LG Nexus只是在无效后开始重新启动。 - 尝试通过
echo 1,1,1,1,1,1 > /sys/module/lowmemorykiller/parameters/minfree
关闭lmkd
。结果是我上面提到的神奇阈值发生了变化,允许我使用更多的RAM,但仍然有将近1/3的RAM保持空闲。
请帮助我使用掉所有的RAM!
英文:
Need to write Android application which is capable to consume all the RAM. Did app which creates multiple MemoryFile-s in infinitive loop inside worker thread. When run application I see that Android restricts RAM allocation till some magic threshold simultaneously trying to free RAM by closing some other apps. Finally, either OS kills my application or continues to keep RAM close to magic threshold. Slightly less then half of free RAM stays available. How to consume all?
What I tried:
- Tried to use
sudo mount -t tmpfs tmpfs /sdcard/tmp
on rooted device. However, in my particular case LG Nexus with Oreo 8.1 just starts to reboot with no effect after. - Tried to turn off
lmkd
byecho 1,1,1,1,1,1 > /sys/module/lowmemorykiller/parameters/minfree
. As result the magic threshold I was talking about above changes to allow me to consume more RAM, but still almost 1/3 of RAM stays free.
Please, help me to consume all of it!
答案1
得分: 0
我不确定为什么你会想要有意地像这样故意地自己“射击”自己的脚,这么说,但我岔开话题……
Android应用程序使用类似Java的运行时(art或dalvik,参见https://source.android.com/devices/tech/dalvik)。每个应用在自己的进程空间中运行,并且每个应用都有自己的最大堆设置 - 就像任何Java应用一样。您可以通过启用largeheap模式(https://developer.android.com/guide/topics/manifest/application-element#largeHeap)来调整堆空间的大小。但是在耗尽设备内存之前,您仍然会达到此限制。
您可以通过直接从本机代码分配内存来绕过堆和垃圾收集(但您必须使用NDK编写和集成本机代码到您的应用程序中,这并不是完全简单的任务)。
然而,Linux内核在您实际上可以消耗设备上的所有内存之前仍然很可能会关闭您的应用程序 - 不确定在进一步调整OOM终结者(https://www.percona.com/blog/2019/08/02/out-of-memory-killer-or-savior/)方面是否会有太多的运气。
英文:
I'm not sure why you'd want to intentionally shoot yourself in the foot like this, so to speak, but I digress...
Android apps use a java-like runtime (art or dalvik, see https://source.android.com/devices/tech/dalvik). Each app runs in it own process space, and each has it's own max heap setting - just like any java app. You can tweak the size of that heap space by enabling largeheap mode (https://developer.android.com/guide/topics/manifest/application-element#largeHeap). However you will still hit this limit before you exhaust device memory.
You can bypass the heap and garbage collection and by allocating memory directly from native code (but you have to write and integrate the native code into your application using the NDK, which is not exactly trivial).
However the linux kernel will still likely shutdown your application before you can actually consume all the memory on the device - not sure if you'd have much luck tuning the OOM killer (https://www.percona.com/blog/2019/08/02/out-of-memory-killer-or-savior/) further.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论