Setting Java Memory Options

Default situation

By default Tomcat starts up with a memory allocation of just 512 megabytes in many situations. This is not enough. Other JVM configuration options may be important as well.

What are your Java settings? The quickest way to check is using the option “System Information” in the xDM Application Builder:

 

Solution

Warning: setting optimal memory options for a Java Virtual Machine is complex. It will be different depending on your exact usage patters. There is no single answer which can be ideal for all environments.

First: determine how to set CATALINA_OPTS or JAVA_OPTS in your environment.

  • In VMs and AMIs provided by Semarchy on Azure and AWS Marketplace, this is done in the file /etc/default/tomcat8.

  • In “Semarchy xDM - Preconfigured with Apache Tomcat” JAVA_OPTS are set in the file setenv.sh or setenv.bat. Default value in xDM 5.1:
    CATALINA_OPTS="$CATALINA_OPTS -Xmx512m -server -XX:+UseG1GC -Dorg.apache.cxf.Logger=org.apache.cxf.common.logging.Slf4jLogger -Duser.language=en -Duser.country=US -Djava.awt.headless=true"

  • In many Linux environments these options set in the file /etc/tomcat8/tomcat8.conf.

Second: experiment with different values to see what works best in your environment.

  1. In many circumstances, the maximum Java heap is the most important setting. Try 4GB as a reasonable first guess for many environments:
    -Xmx4g
    Adjust this value up or down depending on your results.
    It is vital that your operating system actually has this much memory available. If you allocate 4GB to Java but the system only has 3GB available to be allocated, then Tomcat will crash.

  2. Semarchy recommends the Garbage-First (G1) collector for xDM. It meets garbage collection (GC) pause time goals with a high probability, while achieving high throughput.
    -XX:+UseG1GC

  3. Semarchy recommends running the JVM in server mode. In very rough terms, "using ‘-server’ starts slower but should run faster". Your JVM might default to using -server, but it might not. Explicitly setting it is recommended:
    -server

Related articles