Search code examples
javaperformancejvmjvm-hotspot

Under which circumstance will JVM decide to grow size of heap?


A JVM application runs on Oracle Hotspot JVM, it starts up with default JVM settings, but with 100MB of initial heap size and 1GB of maximum heap size.

Under which circumstances will JVM decide to grow the current heap size, instead of trying GC?


Solution

  • HotSpot JVM continuously monitors allocation rates and objects lifetimes. It tries to achieve two key factors:

    1. let short-lived objects die in eden
    2. promote long-lived object to heap on time to prevent unnecessarily copying between survivor spaces

    In a nutshell you can describe it as the HotSpot have some configured threshold which indicates how much pecentage of total allocated heap have to by free after running garbage collector. For example if this threshold is configured for 70% and after running full GC heap usage will be 80%, then additional memory will be allocated to hit the threshold. Of course bigger heap means longer pauses while smaller heap means more frequent collections.

    But you have to remember that JVM is very complex, and you can change this behaviour, for example by using flags:

    • AdaptiveSizePausePolicy, which will pick heap size to achieve shortest pauses
    • AdaptiveSizeThroughPutPolicy, which will pick heap size to achieve highest throughtput
    • GCTimeLimit and GCTimeRatio, which sets time spent in application execution