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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With