Dalvik memory allocation

Go To StackoverFlow.com


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?

2012-04-04 05:48
by Waypoint
Have a look at this: http://stackoverflow.com/questions/2630158/detect-application-heap-size-in-android

Hope it helps.. - Swifty McSwifterton 2012-04-04 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 solutio - Waypoint 2012-04-04 06:04


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.

2012-04-04 06:10
by UVM
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 2012-04-04 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 2012-04-04 06:48