0

Is there any way, how to programatically (like allocating and filling some array) check, how much memory can Dalvik allocate before OutOfMemoryError appears? Is it possible to do it with Java?

Andrew Thompson
  • 163,965
  • 36
  • 203
  • 405
Waypoint
  • 15,705
  • 36
  • 110
  • 167
  • Have a look at this: http://stackoverflow.com/questions/2630158/detect-application-heap-size-in-android Hope it helps... – Swifty McSwifterton Apr 04 '12 at 06:00
  • Thanks, I know, there is one row solution, but I need to verify it manually... that's why I am asking for programatically solution – Waypoint Apr 04 '12 at 06:04

1 Answers1

5

Every Java application allocates memory when it runs. This is what we have done using

java -Xms<initial heap size> -Xmx<maximum heap size>. 

Also, in Throwable block, you can do the following way:

  1. Total Memory (heap) - Runtime.getRuntime().totalMemory();
  2. System.out.println("Used Memory:"
    + (runtime.totalMemory() - Runtime.runtime.freeMemory()) / mb);
  3. System.out.println("Free Memory:"
    + Runtime.runtime.freeMemory() / mb);
  4. System.out.println("Total Memory:" + runtime.totalMemory() / mb);

Maybe this will solve the problem.

Andrew Thompson
  • 163,965
  • 36
  • 203
  • 405
UVM
  • 9,526
  • 5
  • 39
  • 63
  • Thanks, this looks good, but problem is with field allocation. I am using String array to determine actual allocated memory, when I set big value, my program crashes. Do you know, how to create dynamic string array without need of implicit size declaration? – Waypoint Apr 04 '12 at 06:36
  • In Java, you can create an arraylist and that can be converted to an Array like this: List al = new ArrayList(); al.add("your string"); al.toArray(new String[al.size()]);. However, in this situation also, if memory is a constraint, it will crash. – UVM Apr 04 '12 at 06:48