Stresstest Memory on Android

2020-03-31 05:30发布

问题:

Is it possible to stresstest an Android system to check what happens if available memory decreases (and therefor which applications are killed, ...).

I currently have a testing application that periodically allocates 10MB in a vector. But an OutOfMemory exception is thrown reaching 500MB. But the system under test was not influenced sufficiently. I need to aquire even more memory.

Are there any known libraries/programs to use or is there any other method that let an application use more memory than 500MB.

Add: I'm already using android:largeHeap="true" in my manifest.

回答1:

Other than rooting the device and hacking the way Android allocates a memory limit to running applications, there is no way to increase the limit of memory using native Java code.

However(!), using a C++ library via JNI allows you to circumvent this limitation. All memory allocated on this layer has access to the overall memory pool of the device, rather than the sandboxed pool native applications get.

As an example for this, look at OpenGL. Normally, you could maybe hold 4-5 bitmaps sized 1024x1024 in memory before getting an OutOfMemory exception. However, if each time you create a bitmap, you transform it into an OpenGL texture (which is managed in the native C++ library) and then destroy the bitmap, you'll find that you can hold a huge amount of pictures before reaching the device's memory limit.

Similarly, if you create such a C++ library, and link it to your project via JNI, you could theoretically hold a massive buffer inside the library, and use the Java code to append more data, or extract a portion of the data when needed.



回答2:

You could try this app

https://play.google.com/store/apps/details?id=derekwilson.net.rameater

I am bias (I wrote it) but it works fine and is being used by a number of devs. No special permissions are required.

Android restricts each process to a maximum amount of memory (usually 500 MB but it can vary). Rameater gets around this by spawning a number of background services each of which can allocate memory.