Friday, April 6, 2012

Apache Tomcat / Java SE 6 HotSpot[tm] Virtual Machine Garbage Collection Tuning

http://www.oracle.com/technetwork/java/javase/gc-tuning-6-140523.html

Sample for 2G Memory Machine with 1.5G HugePage for short session jsp

JAVA_OPTS="$JAVA_OPTS -server -XX:+UseLargePages -Xms1280M -Xmx1344M -Xss256K -XX:PermSize=128M -XX:MaxPermSize=192M -XX:NewSize=1024M -XX:TargetSurvivorRatio=90 -verbose:gc -XX:+PrintGCTimeStamps -XX:+PrintGCDetails -XX:+PrintTenuringDistribution -XX:+PrintHeapAtGC -XX:+UseTLAB -XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:+CMSClassUnloadingEnabled -XX:MaxTenuringThreshold=23 -XX:SurvivorRatio=10 -XX:+ExplicitGCInvokesConcurrent -XX:+DisableExplicitGC -XX:CMSInitiatingOccupancyFraction=70 -XX:CMSInitiatingPermOccupancyFraction=90 -XX:+UseCMSInitiatingOccupancyOnly -XX:+CMSIncrementalPacing -XX:+HeapDumpOnOutOfMemoryError -XX:+CMSIncrementalMode -XX:ParallelGCThreads=2 -XX:+CMSConcurrentMTEnabled -XX:ConcGCThreads=3 -Dfile.encoding=UTF8 -XX:+UseCMSCompactAtFullCollection -XX:+CMSParallelRemarkEnabled  -XX:ReservedCodeCacheSize=32M -XX:MaxGCPauseMillis=20 -XX:MaxGCMinorPauseMillis=4"

Sample for 4G Memory Machine with 3G HugePage for short session jsp

JAVA_OPTS="$JAVA_OPTS -server -XX:+UseLargePages -Xms2048M -Xmx2560M -Xss256K -XX:PermSize=384M -XX:MaxPermSize=512M -XX:NewSize=1536M -XX:TargetSurvivorRatio=90 -verbose:gc -XX:+PrintGCTimeStamps -XX:+PrintGCDetails -XX:+PrintTenuringDistribution -XX:+PrintHeapAtGC -XX:+UseTLAB -XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:+CMSClassUnloadingEnabled -XX:MaxTenuringThreshold=23 -XX:SurvivorRatio=10 -XX:+ExplicitGCInvokesConcurrent -XX:+DisableExplicitGC -XX:CMSInitiatingOccupancyFraction=70 -XX:CMSInitiatingPermOccupancyFraction=90 -XX:+UseCMSInitiatingOccupancyOnly -XX:+CMSIncrementalPacing -XX:+HeapDumpOnOutOfMemoryError -XX:+CMSIncrementalMode -XX:ParallelGCThreads=2 -XX:+CMSConcurrentMTEnabled -XX:ConcGCThreads=3 -Dfile.encoding=UTF8 -XX:+UseCMSCompactAtFullCollection -XX:+CMSParallelRemarkEnabled  -XX:ReservedCodeCacheSize=32M -XX:MaxGCPauseMillis=20 -XX:MaxGCMinorPauseMillis=4"

Sample for 4G Memory Machine with 3G HugePage for long session jsp

JAVA_OPTS="$JAVA_OPTS -server -XX:+UseLargePages -Xms1536M -Xmx2048M -Xss256K -XX:PermSize=512M -XX:MaxPermSize=768M -XX:NewSize=256M -XX:MaxNewSize=768M -XX:TargetSurvivorRatio=90 -verbose:gc -XX:+PrintGCTimeStamps -XX:+PrintGCDetails -XX:+PrintTenuringDistribution -XX:+PrintHeapAtGC -XX:+UseTLAB -XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:+CMSClassUnloadingEnabled -XX:MaxTenuringThreshold=8 -XX:SurvivorRatio=6 -XX:+ExplicitGCInvokesConcurrent -XX:+DisableExplicitGC -XX:CMSInitiatingOccupancyFraction=70 -XX:CMSInitiatingPermOccupancyFraction=90 -XX:+UseCMSInitiatingOccupancyOnly -XX:+CMSIncrementalPacing -XX:+HeapDumpOnOutOfMemoryError -XX:ParallelGCThreads=2 -XX:+CMSConcurrentMTEnabled -XX:ConcGCThreads=3 -Dfile.encoding=UTF8 -XX:+UseCMSCompactAtFullCollection -XX:+CMSParallelRemarkEnabled -XX:MaxGCPauseMillis=20 -XX:MaxGCMinorPauseMillis=4"




CATALINA_OPTS="$CATALINA_OPTS -Dcom.sun.management.jmxremote=true -Dcom.sun.management.jmxremote.port=9001 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false -Djava.rmi.server.hostname=127.0.0.1 -Xloggc:/var/opt/apache-tomcat/logs/gc.log"


pkill -f jstatd

jstatd -J-Djava.security.policy=$CATALINA_HOME/conf/catalina.policy -J-Djava.rmi.server.hostname=127.0.0.1 &




If you get this error on Linux
Java HotSpot(TM) 64-Bit Server VM warning: Failed to reserve shared memory (errno = 12)

Add the following to /etc/sysctl.conf

kernel.shmmax = 3221225472
vm.nr_hugepages = 1536
vm.hugetlb_shm_group = 1000



Add the following to /etc/security/limits.conf

*    soft    memlock unlimited
*    hard    memlock unlimited