15

I am trying to get a handle on some issues my Android app is having which I think are related to memory pressure when I am running in explicit "foreground" mode (Service.startForeground).

In order to debug this I need to exert memory pressure on my app, and I can do this in various ways such as started various other apps such as Firefox with lots of web pages. However this is less than ideal as it still is rather time consuming and inexact. So my question is, is there a way to force memory pressure using the debugger (e.g. under Eclipse) or perhaps a special app specifically for this purpose? I would rather not detour to write one myself, and obviously it won't work to just allocate memory in my own app.

Update: changed title for reflect that I need actual memory pressure on a device, not in emulation.

Michael
  • 9,060
  • 14
  • 61
  • 123
  • 1
    Read (http://stackoverflow.com/questions/1483047/how-do-you-simulate-low-memory-in-the-android-emulator) or (http://stackoverflow.com/questions/3656594/simulate-low-battery-low-memory-in-android) and enjoy your question closed as a duplicate. – phoeagon Mar 23 '13 at 15:44
  • 1
    Those questions appear to be related to simulating low memory in the emulator. I am not using the emulator (due to the fact that I am logging various sensors and it is not suitable for emulation at this stage), I need to simulate actual low memory on an actual device. (Perhaps simulate is the wrong word, I actually need to create a low memory condition). – Michael Mar 23 '13 at 15:48
  • 1
    then why not cross compile a small binary that eats memory? One that invokes `malloc` then sort of loops. – phoeagon Mar 23 '13 at 15:49
  • I guess I may have to. I was hoping something would already exist so as to save some time. – Michael Mar 23 '13 at 15:50
  • I'm not set up to cross-compile binaries for the command line, so I tried to do this using a standard Android app to give me more control over allocation, but it appears that I'm not allow to allocate enough memory in an app to even start pressuring anything (about 25MB on a 512MB system seems ridiculous to me...) How are other apps even managing this? – Michael Mar 23 '13 at 17:00
  • try stress: https://github.com/m-ric/stress-android – m-ric Apr 16 '15 at 03:03
  • 1
    isn't pressuring memory with a java app a non-sense? Considering the JVM sandbox, and GC? – m-ric Apr 16 '15 at 03:07

1 Answers1

1

EDIT: (taking into account Christoph's comment)

You can create artificial memory pressure. To do so, create a button in your MainActivity. Then, in the button's onClick method, create 10,000 objects (experiment with different amounts of objects). Then, while your app is running, click that button numerous times which should create a lot of memory pressure.

The code could look like this:

class MainActivity {
    ArrayList<String> stringArray = new ArrayList<>();

    ...

    public void click(View view) {
        // Add 10,000 String objects to Array List

        for (int i = 0; i < 10000; i++) {
        stringArray.add("A string");
        }
    }
}

Assuming that you set the button's onClick XML attribute to click.

Izak
  • 909
  • 9
  • 24
  • By itself this isn't quite the same as a browser as Firefox gets around the limit on heap size and can pressure memory a lot more than a single app can. However, this gives me an idea... – Michael Apr 21 '15 at 15:09
  • You will need to allocate much larger arrays to create serious memory pressure. This approach will use ~44 bytes for each String element of the array, or about 44 kB each time the button is pressed. In my case, I needed to create an array of length ~10^8 to generate an `OutOfMemoryError` in a single click. – abeboparebop May 15 '15 at 20:45
  • 2
    This is not the worst idea - However, it will not work this way. You don't keep any reference to your array, therefor it will be Garbage collected. To make this work: Have a ArrayList list; in you activity. Have a for loop, which loops 200 times In each loop, add new String(100000) to the list 4-5 Clicks = 200 MB = OutOfMemory on a M7 (HTC) – Christoph Aug 25 '15 at 08:19