How to increase Java memory: fix

How to increase memory for Java application using Xmx and XX:MaxPermSize Java options. Solution of out of memory error (OutOfMemoryError).

You can specify these options during application startup or in JAVA_OPTS parameter. Default or low settings of these Java options cause OutOfMemoryError.

  • During application startup: java -jar -Xmx1g -XX:MaxPermSize=256m application.jar
  • Using JAVA_OPTS parameter in Windows: set JAVA_OPTS=%JAVA_OPTS% -Xmx1g -XX:MaxPermSize=256m
  • Using JAVA_OPTS parameter in Linux: export JAVA_OPTS="$JAVA_OPTS -Xmx1g -XX:MaxPermSize=256m"

-Xmx defines a maximum heap size of JVM. Inside heap are all Java objects.

-XX:MaxPermSize defines a maximum size of separate heap space that is not garbage collected (that's why it's permanent) and it contains Java classes that are loaded in memory. Whatever is allocated to perm is in addition to the heap set with -Xmx.

Btw. there are a number of separate memory pools in the JVM, whose maximum sizes are set separately, so if you monitor your Java process, you may see the amount of memory you use exceed the total amount you have specified for –Xmx and -XX:MaxPermSize, because Java must also 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 + XX:MaxPermSize.