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?
Asked
Active
Viewed 645 times
0

Andrew Thompson
- 168,117
- 40
- 217
- 433

Waypoint
- 17,283
- 39
- 116
- 170
-
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 Answers
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:
- Total Memory (heap) - Runtime.getRuntime().totalMemory();
- System.out.println("Used Memory:"
+ (runtime.totalMemory() - Runtime.runtime.freeMemory()) / mb); - System.out.println("Free Memory:"
+ Runtime.runtime.freeMemory() / mb); - System.out.println("Total Memory:" + runtime.totalMemory() / mb);
Maybe this will solve the problem.

Andrew Thompson
- 168,117
- 40
- 217
- 433

UVM
- 9,776
- 6
- 41
- 66
-
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 – UVM Apr 04 '12 at 06:48(); al.add("your string"); al.toArray(new String[al.size()]);. However, in this situation also, if memory is a constraint, it will crash.