Java Best Practices

Java is not designed for cluster environments and as such it requires some tuning to run effectively on HPC systems. Here are some recommended settings with explanations.

Memory Parameters

-Xmx4g -Xms4g -Xmn2g -Xss256k
  • -Xmx4g tells the JVM to use at most 4GB of memory (adjust this for particular job requirements)
  • -Xms4g specifies the initial amount of heap memory for the JVM. Setting this equal to the value for -Xmx causes the JVM to grab a huge block of memory and use pieces of if for objects as needed. This reduces malloc calls.
  • -Xmn2g configures a 2GB heap for the new objects (which can be garbage collected in parallel). Having a large heap minimizes the number of objects marked as "old", for which garbage collection is more expensive.
  • -Xss256k sets the JVM stack size. Minimizing this allows more of the virtual memory address space assigned to the process to be used by the Java heap.

Optimization Parameters

-XX:ParallelGCThreads=3 -XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:SurvivorRatio=8 -XX:TargetSurvivorRatio=90 -XX:MaxTenuringThreshold=31
  • -XX:ParallelGCThreads=3 specifies that 3 garbage collection threads should be used. By default, Java starts one thread per processor on the host. This is overkill and can significantly overload a host with Java threads.
  • -XX:+UseConcMarkSweepGC -XX:+UseParNewGC selects the Concurrent Mark Sweep collector.
  • -XX:SurvivorRatio=8 sets survivor space ratio to 1:8, resulting in larger survivor spaces (the smaller the ratio, the larger the space). Larger survivor spaces allow short lived objects a longer time period to die in the young generation.
  • -XX:TargetSurvivorRatio=90 allows 90% of the survivor spaces to be occupied instead of the default 50%, allowing better utilization of the survivor space memory.
  • -XX:MaxTenuringThreshold=31 allows short lived objects a longer time period to die in the young generation (and hence, avoid promotion). A consequence of this setting is that minor GC times can increase due to additional objects to copy. This value and survivor space sizes may need to be adjusted so as to balance overheads of copying between survivor spaces versus tenuring objects that are going to live for a long time.

Experimental Optimizations

  • -XX:+AggressiveOpts turns on point performance optimizations that are expected to be on by default in upcoming releases. The changes grouped by this flag are minor changes to JVM runtime compiled code and not distinct performance features (such as BiasedLocking and ParallelOldGC). This is a good flag to try the JVM engineering team's latest performance tweaks for upcoming releases. Note: this option is experimental!

Note: Most of this information comes from the Oracle Java Tuning Guide.