Search code examples
javagarbage-collectionheap-memory

Can I have young gen bigger than old gen?


Is it ok to size young generation bigger than old generation if I see most activity in new gen and old gen space is under utilized . Is there any consequence in increasing new gen above old gen size ?

like -Xmx2g -Xmn1.5g


Solution

  • You can do this with -Xmn (this answer is based on java 8)

    From docs:

    Sets the initial and maximum size (in bytes) of the heap for the young generation (nursery). Append the letter k or K to indicate kilobytes, m or M to indicate megabytes, g or G to indicate gigabytes.

    The young generation region of the heap is used for new objects. GC is performed in this region more often than in other regions. If the size for the young generation is too small, then a lot of minor garbage collections will be performed. If the size is too large, then only full garbage collections will be performed, which can take a long time to complete. Oracle recommends that you keep the size for the young generation between a half and a quarter of the overall heap size.

    The following examples show how to set the initial and maximum size of young generation to 256 MB using various units:

    • -Xmn256m
    • -Xmn262144k
    • -Xmn268435456

    Instead of the -Xmn option to set both the initial and maximum size of the heap for the young generation, you can use -XX:NewSize to set the initial size and -XX:MaxNewSize to set the maximum size.

    So for example, with

    -Xms1024M -Xmx1024M -Xmn800m

    The spaces will look like this (using the visual gc plugin for visualvm) enter image description here

    You will have a total heap size of 1024m with 800m for young generation (eden + 2 survivor spaces) and 224m for old generation. You can tune survivor space with SurvivorRatio

    From docs:

    You can use the parameter SurvivorRatio can be used to tune the size of the survivor spaces, but this is often not important for performance. For example, -XX:SurvivorRatio=6 sets the ratio between eden and a survivor space to 1:6. In other words, each survivor space will be one-sixth the size of eden, and thus one-eighth the size of the young generation (not one-seventh, because there are two survivor spaces).