Increase Heap Size in Java

How to fix java.lang.OutOfMemoryError: Java heap space

Increase Heap Size in Java
  1. Get default java heap size

    Look for the options MaxHeapSize (for -Xmx) and InitialHeapSize for -Xms

    java -XX:+PrintFlagsFinal -version

    On a Unix/Linux system, you can do

    java -XX:+PrintFlagsFinal -version | grep HeapSize
    java -XX:+PrintFlagsFinal -version 2>&1 | grep MaxHeapSize
    C:\>java -XX:+PrintFlagsFinal -version 2>&1 | findstr MaxHeapSize
  2. Increase heap size in java

    java -Xns10m -Xms512m -Xmx512m HelloWorld
    java -Xmx6g HelloWorld
    Option Task Description
    -Xns Setting the Nursery Optimally, you should try to make the nursery as large as possible while still keeping the garbage collection pause times acceptably low. This is particularly important if your application is creating a lot of temporary objects.

    The maximum size of a nursery cannot exceed 95% of the maximum heap size.

    -Xms Setting initial and minimum heap size Oracle recommends setting the minimum heap size (-Xms) equal to the maximum heap size (-Xmx) to minimize garbage collections.
    -Xmx Setting maximum heap size Setting a low maximum heap value compared to the amount of live data decrease performance by forcing frequent garbage collections.
    -Xgc:parallel Setting garbage collection
    -XXaggressive:memory Performs adaptive optimizations as early as possible in the Java application run. To do this, the bottleneck detector will run with a higher frequency from the start and then gradually lower its frequency. This options also tells JRockit to use the available memory aggressively.


Popular posts from this blog