Java garbage collection Optimization

Source: Internet
Author: User
1. Materials
  • Optimization of jdk5.0 garbage collection-Don't pause)
  • Compile code that is GC friendly and does not leak (for years)
  • JVM tuning Summary
  • All JDK 6 options and default values
2 GC log Printing

GC tuning is a very hands-on task of Galileo. GC logs are a prerequisite for data reference and final verification:

-XX: + printgc details-XX: + printgctimestamps (GC occurrence time)-XX: + printgcapplicationstoppedtime (GC consumed time)-XX: + printgcapplicationconcurrenttime (how long does the GC run)

 

3. Select the CMS collector as the Collector. The pause time takes precedence.

Configuration parameters:-XX: + useconcmarksweepgc no configuration is required by default. parameters:-XX: + useparnewgc (parallel )-XX: + cmspermgensweepingenabled (CMS collection permanent generation)-XX: usecmscompactatfullcollection (full GC compression for the old generation)

Initial effect: the new generation of 1 GB heap memory is about 60 MB, minor GC is about 5-20 milliseconds, and full GC is about 130 milliseconds.

Parallel COLLECTOR: throughput first

Configuration Parameter:-XX: + useparallelgc-XX: + useparalleloldgc (parallel collects the old generation, which is supported since jdk6.0)

The parameter-XX: + useadaptivesizepolicy (dynamically adjust the new generation size) is not required by default)

Initial effect: the new generation of 1 GB heap memory is about 90-110 MB (dynamically adjusted), minor GC is about 5-20 milliseconds, and the full GC has useparalleloldgc parameters of 1.3/1.1 seconds, respectively, with little difference.

In addition,-XX: maxgcpausemillis = 100 sets the expected maximum time of minor GC. JVM will adjust the size of the new generation. However, in this test environment, the object is too fast to die, so this parameter has little effect.

4. tuning practices

Parallel collects paused time up to 1 second is basically intolerable, so select CMS collector.

In the mule 2.0 application under stress testing, there are about MB of short-lived objects generated every second:

  1. Because the new generation of 60 m by default is too small, frequent occurrence of minor GC occurs, it takes about 0.2 seconds.
  2. In the CMS collector, maxtenuringthreshold (the number of times that the generation object has been supported by minor GC before entering the old generation setting) defaults to 0, and the surviving temporary object directly enters the old generation without going through the same vor area, it was not long before full GC occurred in the old generation.

The optimization of these two parameters should not only improve the above two cases, but also avoid too many new generations. Too many replications may cause the pause time of minor GC to be too long.

  1. Use-xmn to adjust the total memory to 1/3. After observation, set-xmn500m, and the new generation is actually about 460 m. (-XX: The newratio setting is invalid. Only-xmn can be used ).
  2. Add the-XX: + printtenuringdistribution parameter to observe the total object size of each age, and then set-XX: maxtenuringthreshold = 5.

After optimization, a minor GC occurs in about 1.1 seconds, and the speed remains between 15-20 ms. At the same time, the growth rate of the old generation has slowed down significantly, and a full GC occurred for a long time,

Final parameters:

 -server -Xms1024m -Xmx1024m -Xmn500m -XX:+UseConcMarkSweepGC   -XX:MaxTenuringThreshold=5  -XX:+ExplicitGCInvokesConcurrent

 

Finally, the service processing speed has increased from 1180 TPS to 1380 TPS. It is very cost-effective to adjust the two parameters to Improve the Performance by 17%.

In addition, jdk6 update 7 comes with a visualvm tool, which is the netbean profiler that has been used before. It can be used like jconsole to view the thread status, an important reference for optimizing the CPU time of objects and methods in the memory. Free bundle. Sun is doing this. Other companies that do profiler are about to close their doors.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.