3

I need a test to be able to see if I can somehow use up the system's entire memory so that the system invokes the onLowMemory()-event.

I need this to be able to test the behaviour of an application when the memory is running low. Any tips?

Lukas Knuth
  • 25,449
  • 15
  • 83
  • 111
michelle
  • 2,759
  • 4
  • 31
  • 46

1 Answers1

1
List<Integer> memoryHog = new ArrayList<Integer>();
while(true){
   memoryHog.add(new Integer(5));
}

Would something as simple as that work? Basically loop and add objects until any memory allocated to your program gets used. Or are you looking for a way to literally use all the systems memory?

DeeV
  • 35,865
  • 9
  • 108
  • 95
  • exactly, I want a way to use all the system memory. Every process has allocated 16mb/24mb limit. I need a way to know when the system has low memory. – michelle Oct 24 '11 at 12:46
  • 1
    You should do this in the activity which currently has focus (not belonging to your app) inside an asynctask. That way, Android will try to kill other processes first and those processes are more likely to receive the onLowMemory event. – user999717 Oct 24 '11 at 12:47
  • Check the comment of michelle in this [answer](http://stackoverflow.com/a/7674650/1708390). `onLowMemory()` is called when the whole system memory becomes low, not just your app – Bugs Happen Nov 17 '16 at 05:43