JVM Tuning: Heapsize, Stacksize and Garbage Collection Fundamental

Last updated
App Shah
Crunchify » Java and J2EE Tutorials » JVM Tuning: Heapsize, Stacksize and Garbage Collection Fundamental

java-jvm-tuning-crunchify-tips

Heap Size:

When a Java program starts, Java Virtual Machine gets some memory from Operating System. Java Virtual Machine or JVM uses this memory for all its need and part of this memory is call java heap memory. Whenever we create object using new operator or by any another means object is allocated memory from Heap and When object dies or garbage collected, memory goes back to Heap space in Java.

Tutorial on Increase Eclipse Memory Size to avoid OOM on Startup.

JVM option Meaning
-Xms initial java heap size
-Xmx maximum java heap size
-Xmn the size of the heap for the young generation

It is good practice for big production projects to set the minimum -Xms and maximum -Xmx heap sizes to the same value.

For efficient garbage collection, the -Xmn value should be lower than the -Xmx value. Heap size does not determine the amount of memory your process uses.

If you monitor your java process with an OS tool like top or task manager, you may see the amount of memory you use exceed the amount you have specified for -Xmx. -Xmx limits the java heap size, java will allocate memory for other things, including a stack for each thread. It is not unusual for the total memory consumption of the VM to exceed the value of -Xmx.

Stack Size:

Each thread in the VM get’s a stack. The stack size will limit the number of threads that you can have, too big of a stack size and you will run out of memory as each thread is allocated more memory than it needs.

JVM option Meaning
-Xss the stack size for each thread

-Xss determines the size of the stack: Xss1024k. If the stack space is too small, eventually you will see an exception class java.lang.StackOverflowError.

Garbage Collection:

There are essentially two GC threads running. One is a very lightweight thread which does “little” collections primarily on the Young generation of the heap. The other is the Full GC thread which traverses the entire heap when there is not enough memory left to allocate space for objects which get promoted from the Young to the older generation(s).

If there is a memory leak or inadequate heap allocated, eventually the older generation will start to run out of room causing the Full GC thread to run (nearly) continuously.

Since this process stops the world, Java application won’t be able to respond to requests and they’ll start to back up or OOM.

The amount allocated for the Young(Eden) generation is the value specified with -Xmn. The amount allocated for the older generation is the value of -Xmx minus the -Xmn.

Generally, you don’t want the Eden to be too big or it will take too long for the GC to look through it for space that can be reclaimed.

16 thoughts on “JVM Tuning: Heapsize, Stacksize and Garbage Collection Fundamental”

  1. one question, is it possible that jvm with 16 gb max heap does a full gc but jvm with 8gb (for same number of users )does not? if yes,what could be the factors that does trigger a GC…i am using parallel gc both for young and old generations

    Reply
  2. Hi…

    Read your article which is vary help full to get the gc concept clear ..
    I am facing problem of heap space in my multi module maven project
    not getting wats going wrong .
    could you suggest me some some ways to tackle it .

    Reply
      • Hi App Shah –

        Thanks for the reply

        I am working on spring Hibernate project in MAVEN . Currently what is going wrong with me is that i am getting
        java.lang.OutOfMemoryError: Java heap space error .

        so i want to ask you that could u suggest me some solution to detect error cause in project and any do or don’t to avoid it .

        Thanks in adv.

        Reply

Leave a Comment