JVM Performance Parameters and tuning

Source: Internet
Author: User
Tags xms

The parameters of this article and complete, read http://www.cnblogs.com/redcreen/archive/2011/05/04/2037057.html related articles written very well, you can take a look at

1.JVM Heap Memory Introduction

GC mainly works on the heap, the following is a brief introduction to the composition of the heap

The heap is divided into two parts, the Cenozoic and the old and the persistent, and the new generation of the newly generated objects, and the older ones are the objects and larger objects that have survived several rounds of GC. The persistence zone holds some class information, JVM internal objects, and so on.

The Cenozoic region is composed of the Eden region and two survival districts.

The newly created object will be placed in the Eden area as much as possible, with only one survival area to work, and the other survival should be empty. When the GC is executed, the object that does not need to be reclaimed is placed in the empty survival area (if not, it is placed in the old age area). The original two extents are then cleaned to achieve the release of the memory. The age of the objects in the survival area is 1, and when the age is sufficient (controlled by Maxtenuringthreshold), it will be moved to the elderly area.

The GC performed in the young area is a copy move. In the old-age area, the tag is removed or the concurrent collector is used, and when the old age zone produces more fragments, cannot draw a space that is needed, or after a specified number of GCS (which can also be configured, as described below), traditional tagging and GC removal is performed.

Whenever we start a Java program, a JVM virtual machine is started, and if we do not configure it at startup, the JVM takes the default parameters. Let's take a look at what parameters are configurable.

2.JVM parameters

Heap Settings

-XMS: initial Heap Size

-XMX: Maximum Heap Size

-xmn/-xx:newsize=n: Setting the young generation size

-XSS: Set the stack size for each thread

-xx:newratio=n: Sets the ratio of the younger generation to the older generation. such as: 3, the ratio of the young generation and the old generation is 1:3, the young generation of the entire young generation of old generation and 1/4

-xx:survivorratio=n: The ratio of Eden in the young generation to the two survivor districts. Note that there are two survivor districts. such as: 3, indicating Eden:survivor=3:2, a Survivor area accounted for the entire young generation of 1/5

-xx:maxpermsize=n: Setting the persistent generation size

-xx:maxtenuringthreshold=0: Sets the maximum age for garbage.

3.GC category

The JVM gives three choices: the serial collector, the parallel collector, the concurrent collector, but the serial collector is only available for small amounts of data, so the choice here is primarily for the parallel collector and the concurrency collector. By default, JDK5.0 used a serial collector before, and if you want to use a different collector, you need to add the appropriate parameters at startup. After JDK5.0, the JVM is judged based on the current system configuration.

3.1 Throughput-First parallel collector
As mentioned above, the parallel collector is mainly aimed at reaching a certain throughput, and is suitable for science and technology and background processing.
Typical configuration:

Java-xmx3800m-xms3800m-xmn2g-xss128k-xx:+useparallelgc-xx:parallelgcthreads=20
-XX:+USEPARALLELGC: Select the garbage collector as the parallel collector. This configuration is only valid for younger generations. In this configuration, the younger generation uses concurrent collection, while the older generation still uses serial collection.
-XX:PARALLELGCTHREADS=20: Configures the number of threads for a parallel collector, that is, how many threads are garbage collected together. This value is best configured to be equal to the number of processors.

Java-xmx3550m-xms3550m-xmn2g-xss128k-xx:+useparallelgc-xx:parallelgcthreads=20-xx:+useparalleloldgc
-XX:+USEPARALLELOLDGC: Configure the old Generation garbage collection method for parallel collection. JDK6.0 supports parallel collection of older generations.

java-xmx3550m-xms3550m-xmn2g-xss128k-xx:+useparallelgc-xx:maxgcpausemillis=100
-XX:MAXGCPAUSEMILLIS=100: Sets the maximum time for each young generation of garbage collection, and if this time is not met, the JVM automatically adjusts the younger generation size to meet this value.

Java-xmx3550m-xms3550m-xmn2g-xss128k-xx:+useparallelgc-xx:maxgcpausemillis=100-xx:+useadaptivesizepolicy
-xx:+useadaptivesizepolicy: When this option is set, the parallel collector automatically selects the size of the younger generation and the corresponding Survivor area scale to achieve the minimum corresponding time specified by the target system or the collection frequency, etc., which is recommended to be turned on when using the parallel collector.

Parallel collector settings

-xx:parallelgcthreads=n: Sets the number of CPUs to use when the parallel collector is collected. The number of parallel collection threads.

-xx:maxgcpausemillis=n: Set maximum pause time for parallel collection

-xx:gctimeratio=n: Sets the percentage of time that garbage collection takes to run the program. The formula is 1/(1+n)

3.2 Concurrency collector with response time precedence
As mentioned above, the concurrency collector is mainly to ensure the response time of the system, reduce the time of the garbage collection downtime. It is suitable for application server, telecom field and so on.
Typical configuration:

Java-xmx3550m-xms3550m-xmn2g-xss128k-xx:parallelgcthreads=20-xx:+useconcmarksweepgc-xx:+useparnewgc
-XX:+USECONCMARKSWEEPGC: Set old age on behalf of concurrent collection. After configuring this in the test, the configuration of the-xx:newratio=4 is invalid for unknown reasons. Therefore, at this time the younger generation size is best set with-XMN.
-XX:+USEPARNEWGC: Set Young on behalf of the parallel collection. Can be used concurrently with CMS collection. JDK5.0 above, the JVM will set itself according to the system configuration, so it is no longer necessary to set this value.

java-xmx3550m-xms3550m-xmn2g-xss128k-xx:+useconcmarksweepgc-xx:cmsfullgcsbeforecompaction=5-xx:+ Usecmscompactatfullcollection
-xx:cmsfullgcsbeforecompaction: Because the concurrent collector does not compress and defragment the memory space, it can produce "fragmentation" after a period of time, which makes the operation less efficient. This value sets how many times a GC is run to compress and defragment the memory space.
-xx:+usecmscompactatfullcollection: Turn on compression for older generations. Performance may be affected, but fragmentation can be eliminated

Concurrent collector Settings

-XX:+USECONCMARKSWEEPGC using CMS Memory collection
-XX:+AGGRESSIVEHEAP Special Instructions: (I feel helpful for doing Java cache application)

    • Trying to use a lot of physical memory
    • Optimized for long-time large memory usage, can check compute resources (memory, number of processors)
    • Requires at least 256MB of memory
    • A large amount of cpu/memory, (which has been shown to have increased in 1.4.1 on a 4CPU machine)

-XX:+USEPARNEWGC allows multithreading to collect new generation
-xx:+cmsparallelremarkenabled Decrease Mark Pause

-xx+usecmscompactatfullcollection in full GC, compressed memory, CMS will not move memory, so this is very easy to produce fragmentation, resulting in insufficient memory, so the memory compression will be enabled at this time. It's a good habit to add this parameter.

-xx:+cmsincrementalmode: Set to incremental mode. Applies to single CPU conditions.

-xx:parallelgcthreads=n: Set the concurrency collector the number of CPUs used by the young generation collection method for parallel collection. The number of parallel collection threads.

The precondition for using a CMS is that you have long life objects that you compare, such as the old heap occupancy of more than 200M. Then this power is very strong, can greatly improve the FGC of the collection capacity. If your old occupies very little, do not use, absolutely reduce your performance, because the CMS collects 2 stop world behavior. Old less clear situation, according to my test, the use of parallel collection parameters will be better.

4. Auxiliary

Allows the JVM to print recycled logs to help debug

-xx:+printgc

-xx:+printgcdetails

-xx:+printgctimestamps

-xloggc:filename

5. Optimization

Young Generation Size Selection

Response Time-First application: as large as possible until the minimum response time limit of the system is approached (depending on the actual situation). In such cases, the frequency at which the young generation collects occurs is also minimal. At the same time, reduce the reach of older generations of objects.

Throughput-First application: as large as possible, may reach Gbit degree. Because there is no requirement for response time, garbage collection can be done in parallel, generally suitable for applications over 8CPU.

5.2-year-old-size choice

Response Time-First application: The older generation uses the concurrent collector, so its size needs to be set carefully, and some parameters, such as the concurrency session rate and the session duration, are generally considered. If the heap setting is small, it can result in memory fragmentation, high recovery frequency, and application pauses using traditional markup cleanup, and if the heap is large, a longer collection time is required. The most optimized scenario is generally referred to the following data:

Concurrent garbage Collection Information

Persistent generation concurrent Collection times

Traditional GC Information

The proportion of time spent in the collection of young and old generations

5.3 Throughput-First applications:

General throughput priority applications have a very large young generation and a smaller old generation. The reason for this is that the majority of short-term objects can be recovered as much as possible, reducing medium-term objects, while older generations store long-lived objects.

5.4 Fragmentation problems caused by smaller heaps

The heap is not compressed because the old generation of concurrent collectors uses tags and clears the algorithm. When the collector recycles, he merges the adjacent space so that it can be assigned to a larger object. However, when the heap space is small, after a period of time, "fragmentation" occurs, if the concurrent collector does not find enough space, then the concurrent collector will stop, and then use the traditional token, purging method for recycling. If "Fragmentation" occurs, you may need to configure the following:

-xx:+usecmscompactatfullcollection: When using the concurrency collector, the compression of older generations is turned on.

-xx:cmsfullgcsbeforecompaction=0: When the above configuration is on, how many times the full GC is set, the old generation is compressed

5.5XMX as large as XMS settings

MaxPermSize is as large as the Minpermsize setting, which reduces the pressure on the size of the scaling heap

5.6 Benefits of using CMS

With as few new generations as possible, the experience value is 128m-256m, and then the Laosheng generation uses CMS to collect in parallel, which ensures the system's low-latency throughput efficiency. In fact, the CMS collection pause time is very short, 2G of memory, about 20-80MS application pause time

5.7 When the system pauses may be the GC problem may be the problem of the program, more with Jmap and Jstack view, or killall-3 Java, and then view the Java console log, can see a lot of problems. (The use of related tools will be described in the following blog)

5.8 Careful understanding of their own applications, if the use of the cache, then the older generation should be larger, the cache hashmap should not be unlimited long, it is recommended to use the LRU algorithm map cache, the maximum length of lrumap should be set according to the actual situation.

5.9 in the case of concurrent recycling, the younger generation of small, older generations to big, because the eldest brother with the use of concurrent recovery, even if the time is long will not affect the other programs continue to run, the site will not pause

The setting of the 6.0JVM parameter (especially the setting of –xmx–xms–xmn-xx:survivorratio-xx:maxtenuringthreshold parameter is not a fixed formula, it needs to be measured according to the actual data ygc number of PV old area. To avoid promotion faild may cause xmn settings to be small, it also means that the number of YGC will increase, and the ability to handle concurrent access is reduced. The tuning of each parameter requires detailed performance testing to find the best configuration for a particular application.

JVM Performance Parameters and tuning

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.