Rubin's JVM tuning notes
Published (last updated )
Version 1.1, collected notes and various clarifications about JVM memory by Rubin Simons.
A few notes about generational GC in Java 1.3 and up: Young generation represents all the objects which have a short lifetime. Young generation objects are in a specific location on the heap (NewSize), where the garbage collector will pass often. All new objects are created in a young generation region called “eden”. When an object survives a gc cleaning, it becomes a “survivor”. If survivors survive multiple gc cleanings, they are moved to tenured space, the regular old generation heap, which usually is the largest area of the total heap. In short: Objects are created in Eden (New Generation), if they survive a GC, they move to Survivor Space 1. If they survive a 2nd GC, they are moved to Survivor Space 2. If they survive a 3rd GC, they are moved to Tenured Space (Old Generation).
Java memory is divided in Heap, PermanentGeneration, CodeCache and StackPerThread.
The Heap has Eden, SurvivorSpace1, SurvivorSpace2 and Tenured areas which can be manually controlled or automatically sized by UseAdaptiveSizePolicy. Objects live in the heap.
PermanentGeneration contains metadata about classes and runtime, think of it as a Blueprint Space. The more classes you have, the more PermanentGeneration you use.
CodeCache is the compiler location for the JVM, the JIT’s work-area.
StackPerThread is 1mb by default on Java 6 x64. This means that every thread in Java has a memory requirement of 1mb outside of the Heap, PermanentGeneration or CodeCache spaces.
Set total heap to 3gb, permsize to 256m, codeache to 32m. Don’t set NewSize, NewRatio, or SurvivorRatio because we use UseAdaptiveSizePolicy.
-Xms3072m -Xmx3072m -XX:MaxPermSize=256m -XX:PermSize=256m
The Parallel scavenge collector (said to be the throughput version of the Parallel copying (Low Pause) collector, performs better when used with the -XX:+UseAdaptiveSizePolicy. This automatically sizes the young generation and chooses an optimum survivor ratio to maximize performance. We set amount of GC threads to two so on our four core machines we always have at least 2 processors free for other than GC java jobs. Note that this is heavily different on machines where you run multiple JVM’s on a single machine. AggressiveOpts attempts to make maximum use of physical memory for the heap and inspects computer resources (memory, cpus) and sets parameters optimal for long running memory allocation intensive jobs. Note that the AggressiveOpts option is an update of the AggressiveHeap option which was available in older JVM environments.
For single JVMs on machines with 4 cores, parallelize GC.
-XX:+AggressiveOpts -XX:+UseAdaptiveSizePolicy -XX:ParallelGCThreads=2
For multiple JVMs on machines with 4 cores, incremental GC.
-XX:+AggressiveOpts -XX:+UseAdaptiveSizePolicy -XX:+UseConcMarkSweepGC -XX:+CMSIncrementalMode
Avoid possible deadlock with Spring OSGi class loader on J2SE 6.
-XX:+UnlockDiagnosticVMOptions -XX:+UnsyncloadClass
The gcInterval settings below have to do with applications that participate in remote method invocation (RMI). The “server” setting applies to applications that act as RMI servers (JBossAS qualifies as a server due to JNDI, EJBs deployed, and I think even running the JMS server and web services). The “client” setting applies to app that run as an RMI client - that is, they make calls to an RMI server. This includes any deployed apps that call another server.
The idea behind both settings is that RMI causes both the server and the client to create temporary objects on the heap, and those objects need to be garbage collected. So these timers automatically force a garbage collection at the given interval. The bad news is that they force a major collection, which is usually very costly. Besides, the temp rmi objects these settings are meant to collect are collected anyway during normal minor and major collections. Therefore, the act of running a collection specifically for rmi objects in an app server is ridiculous, as the objects that it attempted to clean up are probably already out of the heap. It is recommended to set these values as high as possible.
-Dsun.rmi.dgc.client.gcInterval=3600000 -Dsun.rmi.dgc.server.gcInterval=3600000
These values are “Sun-implementation specific” Java system properties, which are associated with the java.net.URLConnection object. They are usually set to avoid timeout issues with various HTTP type objects that use the native URLConnection classes.
-Dsun.net.client.defaultConnectTimeout=3600000 -Dsun.net.client.defaultReadTimeout=3600000