Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I force memory pressure for Android debugging?

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.

like image 544
Michael Avatar asked Mar 23 '13 15:03

Michael


1 Answers

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.

like image 165
Izak Avatar answered Nov 09 '22 21:11

Izak