Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the objective of setting the minimum heap size in an android app?

In google's Calendar app for Android OS, you will encounter this line in the onCreate method of CalendarActivity.

// Eliminate extra GCs during startup by setting the initial heap size to 4MB.
VMRuntime.getRuntime().setMinimumHeapSize(INITIAL_HEAP_SIZE)

Can someone explain why setting it to 4MB will eliminate GCs?

like image 625
Jacques René Mesrine Avatar asked Oct 05 '09 03:10

Jacques René Mesrine


People also ask

What is heap size android?

The Heap is used for dynamic memory allocation. To provide a smooth user experience, Android sets a hard limit on the heap size for each running application. The heap size limit varies among devices and is based on how much RAM a device has.

What is min heap size?

Initial heap size is 1/64th of the computer's physical memory or reasonable minimum based on platform (whichever is larger) by default. The initial heap size can be overridden using -Xms. Maximum heap size is 1/4th of the computer's physical memory or 1 GB (whichever is smaller) by default.

Does heap size affect performance?

A too small heap size may affect performance if your system also does not have enough cores, so that the garbage collectors do compete over the CPU with the business threads. At some point, the CPU spends a significant time on garbage collection.

What is the meaning of heap size?

The heap size is the amount of memory allocated to objects that are being defined in your Apex code.


1 Answers

A JVM typically starts by allocating a relatively small heap. Then after each GC run it checks to see how much free heap memory there is. If the ratio of free heap to total heap is too small, the JVM will then add more memory to the heap (up to the maximum configured heap size).

A second relevant fact is that GC runs most efficiently when there is lots of memory to reclaim. Provided that you don't run into overall system resource limits (e.g. triggering paging or swapping), you get better application performance by running with a large heap than a small one.

Suppose that the application writer knows that the app most likely needs a given amount of heap (e.g. 4Mb) to run comfortably. By setting that size as the minimum heap size means that the JVM does not need to run the GC when the heap fills at (say) 1Mb, 2Mb and 3Mb. As a result, the JVM runs the garbage collector fewer times during application startup and normal running, the app starts up faster and the user sees fewer GC pauses.

like image 135
Stephen C Avatar answered Nov 06 '22 14:11

Stephen C