{"id":2089,"date":"2013-06-21T13:40:38","date_gmt":"2013-06-21T05:40:38","guid":{"rendered":"http:\/\/rmohan.com\/?p=2089"},"modified":"2013-11-07T11:41:34","modified_gmt":"2013-11-07T03:41:34","slug":"java-tunning","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=2089","title":{"rendered":"Java tunning"},"content":{"rendered":"<p>The general tuning guidelines for java VM.<br \/>\nHere are some general tunning guideline to help you categorize the kinds of java tunning you will perform:<br \/>\n1. Be aware of ergonomics settings<br \/>\nThe -server: server compiler<br \/>\nThe -XX:+useParallelGC: parallel garbage collector<br \/>\nThe -Xms: initial heap size is 1\/64th of the machine&#8217;s physical memory<br \/>\nThe -Xmx: maximum heap size is 1\/4th of the machine&#8217;s physical memory ( up to 1 GB max)<\/p>\n<p>2. Heap sizing<br \/>\nThe mazimum heap size of a java application is limited by the factors:<br \/>\nThe process daata model (32-bit or 64-bit) on the system.<br \/>\nThe amount of physical memory available on the system.<br \/>\nThe size of the java heap for a paricular application can never exceed or even reach the maximum<br \/>\nvirtual address space of the process data model.<\/p>\n<p>3. Garbage collector policy<br \/>\nThe -XX:+UseParallelGC: parallel garbage collector<br \/>\nThe -XX:+UseConcMarkSweepGC: concurrent garbage collector (CMS)<br \/>\nThe -XX:+UseSerialGC: serial garbage collector ( for smaller applications and systems)<\/p>\n<p>4. Other tunning parameters<br \/>\nThe -XX:+UseLargePages: large memory pages.<br \/>\nThe -XX:LargePageSizeInBytes:<\/p>\n<p>Examples:<br \/>\n1. Tunning for throughput<br \/>\nHere is an example of specific command line tuning for a server application<br \/>\nrunning on system with 4 GB of memory and capable of running 32 threads simultaneously<br \/>\n(CPU&#8217;s and cores or contexts).<br \/>\njava -Xmx3800m -Xms3800m -Xmn2g -Xss128k -XX:+UseParallelGC -XX:ParallelGCThreads=20<\/p>\n<p>2. Try the parallel old generation collector<br \/>\nSimilar to example 1 we here want to test the impact of the parallel old generation collector.<br \/>\njava -Xmx3550m -Xms3550m -Xmn2g -Xss128k -XX:+UseParallelGC -XX:ParallelGCThreads=20 -XX:+UseParallelOldGC<\/p>\n<p>3. Try 256 MB pages<br \/>\nThis tuning example is specific to those Solaris-based systems that would support the huge page size of 256 MB.<br \/>\njava -Xmx2506m -Xms2506m -Xmn1536m -Xss128k -XX:+UseParallelGC -XX:ParallelGCThreads=20<br \/>\n-XX:+UseParallelOldGC -XX:LargePageSizeInBytes=256m<\/p>\n<p>4. Try -XX:+AggressiveOpts<br \/>\nThis tuning example is similar to Example 2, but adds the AggressiveOpts option.<br \/>\njava -Xmx3550m -Xms3550m -Xmn2g -Xss128k -XX:+UseParallelGC -XX:ParallelGCThreads=20<br \/>\n-XX:+UseParallelOldGC -XX:+AggressiveOpts<\/p>\n<p>5. Try Biased Locking<br \/>\nThis tuning example is builds on Example 4, and adds the Biased Locking option.<br \/>\njava -Xmx3550m -Xms3550m -Xmn2g -Xss128k -XX:+UseParallelGC -XX:ParallelGCThreads=20<br \/>\n-XX:+UseParallelOldGC -XX:+AggressiveOpts -XX:+UseBiasedLocking<\/p>\n<p>6.Tuning for low pause times and high throughput<br \/>\nThis tuning example similar to Example 2, but uses the concurrent garbage collector<br \/>\n(instead of the parallel throughput collector).<br \/>\njava -Xmx3550m -Xms3550m -Xmn2g -Xss128k -XX:ParallelGCThreads=20 -XX:+UseConcMarkSweepGC<br \/>\n-XX:+UseParNewGC -XX:SurvivorRatio=8 -XX:TargetSurvivorRatio=90 -XX:MaxTenuringThreshold=31<\/p>\n<p>7. Try AggressiveOpts for low pause times and high<br \/>\nThis tuning example is builds on Example 6, and adds the AggressiveOpts option.<br \/>\njava -Xmx3550m -Xms3550m -Xmn2g -Xss128k -XX:ParallelGCThreads=20 -XX:+UseConcMarkSweepGC<br \/>\n-XX:+UseParNewGC -XX:SurvivorRatio=8 -XX:TargetSurvivorRatio=90 -XX:MaxTenuringThreshold=31<br \/>\n-XX:+AggressiveOpts<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>A good start to understand JVM, Heap, Perm Gen and GC collector<\/p>\n<p>JDK 6 Performance Features and Update<br \/>\nGC Collector:<\/p>\n<p>Serial Collector (-XX:+UseSerialGC)<br \/>\n\u2022 Throughput Collectors<br \/>\n&gt; Parallel Scavanging Collector for Young Gen<br \/>\n? -XX:+UseParallelGC<br \/>\n&gt; Parallel Compacting Collector for Old Gen<br \/>\n? -XX:+UseParallelOldGC (on by default with ParallelGC in JDK 6)<br \/>\n\u2022 Concurrent Collector<br \/>\n&gt; Concurrent Mark-Sweep (CMS) Collector<br \/>\n? -XX:+UseConcMarkSweepGC<br \/>\n&gt; Concurrent (Old Gen) and Parallel (Young Gen) Collectors<br \/>\n? -XX:+UseConcMarkSweepGC -XX:+UseParNewGC<br \/>\n\u2022 The new G1 Collector as of Java SE 6 Update 14 (-XX:+UseG1GC)<\/p>\n<p>Sample JVM Parameters:<\/p>\n<p>Performance Goals and Exhibits<br \/>\nA) High Throughput (e.g. batch jobs, long transactions)<br \/>\nB) Low Pause and High Throughput (e.g. portal app)<br \/>\n\u2022 JDK 6<br \/>\nA) -server -Xms2048m -Xmx2048m -Xmn1024m -XX:+AggressiveOpts<br \/>\n-XX:+UseParallelGC -XX:ParallelGCThreads=16<br \/>\nB) -server -Xms2048m -Xmx2048m -Xmn1024m -XX:+AggressiveOpts<br \/>\n-XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:ParallelGCThreads=16<br \/>\n\u2022 JDK 5<br \/>\nA) -server -Xms2048m -Xmx2048m -Xmn1024m -XX:+AggressiveOpts<br \/>\n-XX:+UseParallelGC -XX:ParallelGCThreads=16 -XX:+UseParallelOldGC<br \/>\n-XX:+UseBiasedLocking<br \/>\nB) -server -Xms2048m -Xmx2048m -Xmn1024m -XX:+AggressiveOpts<br \/>\n-XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:ParallelGCThreads=16<br \/>\n-XX:+UseBiasedLocking<br \/>\nRule of Thumb on Best selection of<\/p>\n<p>&gt; Garbage Collector (Make sure to override GCThreads)<br \/>\n&gt; Heap Size (-Xms == 1\/64 Max Memory or Max Heap and<br \/>\n-Xmx == \u00bc Max Memory or Max Heap)<br \/>\n&gt; Runtime Compiler (-server vs -client)<br \/>\n\u2022 Desired Goals (This is a hint, not a guarantee)<br \/>\n&gt; Maximum Pause Time (-XX:MaxGCPauseMillis=)<br \/>\n&gt; Application Throughput (-XX:GCTimeRatio= where<br \/>\nApplication time = 1 \/ (1 + n))<\/p>\n<p>Do check out 1.5 paper as it comes with sample parameters for high thoughput and low latency application<br \/>\nJava 1.5 Tuning White Paper<br \/>\nIf you are using 1.6, check out the difference and improvement from 1.6 paper, take note that 1.5 parameters still apply to 1.6.<\/p>\n<p>Java SE 6 Performance White Paper<\/p>\n<p>A very practical and easy to understandable slide which walking you through the Sun HotSpot GC tuning tip and take-away parameters.<\/p>\n<p>HotSpot JVM Tuning<\/p>\n<p>Wondering whether java thread stack space (Xss) and perm gen (MaxPermSize) part of heap space. The answer is NO. That is why when u saw actually linux memory consumption is bigger than your &#8220;-Xmx&#8221; settings.<br \/>\nUsing -Xss to adjust Java default thread stack size to save memory and prevent StackOverflowError<\/p>\n<p>One working jvm parameters with JBoss server:<\/p>\n<p>JAVA_OPTS=&#8221;-server -Xms3072m -Xmx3072m -Xmn2048m -XX:MaxPermSize=256m -Dorg.jboss.resolver.warning=true -Dsun.rmi.dgc.client.gcInterval=3600000 -Dsun.rmi.dgc.server.gcInterval=3600000 -XX:ParallelGCThreads=8 -XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:SurvivorRatio=8 -XX:TargetSurvivorRatio=90 -XX:MaxTenuringThreshold=31 -XX:+AggressiveOpts -XX:+PrintHeapAtGC -XX:+PrintGCTimeStamps -XX:+PrintGCDetails -XX:+PrintGCApplicationStoppedTime -Xloggc:\/opt\/jbos\/server\/default\/log\/jvmgc.log&#8221;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>Advanced JVM Tuning for Low Pause<br \/>\nThe standard Java Virtual Machine (JVM) is configured to optimize for throughput. But some systems are more interested in low pause\/reduced latency and GC (garbage collection) might be one source of pausing.<br \/>\n(you can read an interesting article about what latency means to your business)<\/p>\n<p>I have found a post on GigaSpaces forum providing some possible JVM configurations to optimize on latency:<\/p>\n<p>-Xms2g -Xmx2g -Xmn150m<br \/>\n-XX:+UseConcMarkSweepGC -XX:+CMSIncrementalMode<br \/>\n-XX:+CMSIncrementalPacing -XX:CMSIncrementalDutyCycleMin=10<br \/>\n-XX:CMSIncrementalDutyCycle=50 -XX:ParallelGCThreads=8<br \/>\n-XX:+UseParNewGC -XX:MaxGCPauseMillis=2000<br \/>\n-XX:GCTimeRatio=10 -XX:+DisableExplicitGC<br \/>\nPlease note that -XX:+UseConcMarkSweepGC has the heaviest impact on performance \u2013 decrease of 40%.<\/p>\n<p>The following set of parameters shows 20% better performance than with -XX:+UseConcMarkSweepGC while the pause size still is below 100msec in embedded test with payload 10KB and 100 threads:<\/p>\n<p>-Xms2g -Xmx2g -Xmn150m<br \/>\n-XX:GCTimeRatio=2 -XX:ParallelGCThreads=8<br \/>\n-XX:+UseParNewGC -XX:MaxGCPauseMillis=2000<br \/>\n-XX:+DisableExplicitGC<br \/>\nWhile I\u2019m pretty sure that most of the applications do no need such an advanced VM configuration, it is interesting to see what strategies are employed when low latency is needed.<\/p>\n<p>Option Details<br \/>\n-XX:+UseConcMarkSweepGC Sets the garbage collector policy to the concurrent (low pause time) garbage collector (also known as CMS)<br \/>\n-XX:+CMSIncrementalMode Enables the incremental mode. (works only with -XX:+UseConcMarkSweepGC)<br \/>\n-XX:+CMSIncrementalPacing Enables automatic adjustment of the incremental mode duty cycle based on statistics collected while the JVM is running<br \/>\n-XX:CMSIncrementalDutyCycleMin The percentage (0-100) which is the lower bound on the duty cycle when CMSIncrementalPacing is enabled<br \/>\n-XX:CMSIncrementalDutyCycle The percentage (0-100) of time between minor collections that the concurrent collector is allowed to run. If CMSIncrementalPacing is enabled, then this is just the initial value.<br \/>\n-XX:ParallelGCThreads Sets the number of garbage collector threads<br \/>\n-XX:+UseParNewGC Enables multi threaded young generation collection.<br \/>\n-XX:MaxGCPauseMillis A hint to the throughput collector that it\u2019s desirable that the maximum pause time is lowed than the given value. (n.b. it looks like this value can also be used with the CMS garbage collector)<br \/>\n-XX:GCTimeRatio A hint to the virtual machine that it\u2019s desirable that not more than 1 \/ (1 + GCTimeRation) of the application execution time be spent in the collector<br \/>\n-XX:+DisableExplicitGC Disables explicit garbage collection calls (System.gc())<br \/>\nThere is no need to learn all these flags by heart as you can find them covered in various documents:<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>Custom settings was:<\/p>\n<p>-Xms768m<br \/>\n-Xmx768m<br \/>\n-XX:NewSize=128m<br \/>\n-XX:MaxNewSize=128m<br \/>\n-XX:+UseParNewGC<br \/>\n-XX:ParallelGCThreads=8<br \/>\n-XX:MaxTenuringThreshold=1<br \/>\n-XX:SurvivorRatio=8<br \/>\n-XX:+UseConcMarkSweepGC<br \/>\n-XX:+CMSParallelRemarkEnabled<br \/>\n-XX:CMSInitiatingOccupancyFraction=65<br \/>\n-XX:+CMSScavengeBeforeRemark<br \/>\n-XX:+UseCMSInitiatingOccupancyOnly<br \/>\n-XX:MaxPermSize=250m<br \/>\n-XX:ReservedCodeCacheSize=64m<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The general tuning guidelines for java VM. Here are some general tunning guideline to help you categorize the kinds of java tunning you will perform: 1. Be aware of ergonomics settings The -server: server compiler The -XX:+useParallelGC: parallel garbage collector The -Xms: initial heap size is 1\/64th of the machine&#8217;s physical memory The -Xmx: maximum [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/2089"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2089"}],"version-history":[{"count":6,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/2089\/revisions"}],"predecessor-version":[{"id":2635,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/2089\/revisions\/2635"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2089"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2089"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2089"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}