Heap is too small

If the allocated heap is too small, the new generation and old generation will fill up faster, resulting in faster GC, and you may end up spending most of the program’s time on GC. Either the Minor OR Full GC.

Heap is too large

If the allocation is too large, we also need to take into account that the time consumed by GC pauses depends on the heap size, i.e., if the heap size increases, the JVM will scan either the new generation or the entire heap when A GC occurs. In the four garbage collection algorithms, due to the defragmentation mechanism, thread space-time pause is required to apply, and the heap is too large, resulting in a longer JVM defragmentation time and the duration of the pause. In this case, the pauses become less frequent, but their duration slows down the overall performance of the program.

There is also a risk that the operating system uses the virtual memory mechanism to manage the physical memory of the machine. A machine might have 8 gigabytes of physical memory, but the operating system might make you feel like you have more available.

The amount of virtual memory depends on the operating system setup; for example, the operating system may feel like it has 16 gigabytes of memory. The OS passes through the name “swap” (Considerations) (or just paging, although there are differences between the two technologies).

You can load an application that requires 16 gigabytes of memory, and the operating system will copy data from memory to disk that is inactive when the application is running when it needs it. When the contents of this portion of memory are needed again, the operating system reloads them from disk into memory (to make space, it usually copies the contents of another portion of memory to disk first).

This process works well when there are a large number of different applications running on the system, because most of them are not active at the same time. However, it doesn’t work that well for Java applications. If a Java application uses about 12 gigabytes of heap on the system, the operating system may have allocated 8 gigabytes of heap space on RAM, with another 4 gigabytes on disk. This will require the operating system to swap a significant portion of the data from disk to memory, and if a Full GC occurs, since the JVM must access the entire heap, the pause time will be longer if the system swaps. Even a Concurrent (G1) collector does not work at this point, where the JVM itself waits for the system to copy data from disk to memory (possibly partially in memory and partially in disk).


From the Definitive Guide to Java Performance