Turn: JVM Tuning Parameter Summary

Source: Internet
Author: User

There are many GC (garbage collection) algorithms in JVM, such as Mark clearing collector, compression collector, and generational collector. For details, see the types of hotspot vm gc.

Currently, generation-based collection is commonly used (generational collection, which is also used by Sun Vm and introduced after j2se1.2). Memory is divided into several areas and objects of different lifecycles are placed in different areas: young Generation, tenured generation, and permanet generation. Most objec objects are allocated to young generation (short lifecycle), and most objects are die here. When young generation is full, minor collection (ygc) is triggered ). Objects that survive after minor collection will be moved to tenured generation (long lifecycle ). Finally, the major collection is triggered when tenured generation is full. Major collection (full GC) triggers the collection of the entire heap, including the collection of young generation. The permanet generation area is relatively stable and mainly stores classloader information.

Young Generation consists of Eden and two region vor regions. One of the primary vor regions is always empty, and is the destination of the Eden region and the other primary vor region living after the next copy collection. Objects are copied in the survivo region until they are transferred to the tenured region.

We should minimize the number of full GC times (tenured generation is generally relatively large, the collection time is long, and frequent full GC will cause serious impact on application performance ).

Heap memory GC

JVM (using the generational collection policy), ygc is performed on young objects (Young Generation) at a high frequency, and tenured generation is performed on old objects (tenured generation) full GC is performed only when tenured generation is full. In this way, you do not need to check all objects in the memory every GC.

Non-heap memory is not GC

GC does not clean up permgen space during the main program running period. Therefore, if your application contains many classes (especially dynamic generation classes, of course, permgen space stores more than classes, the permgen space error may occur.

Memory application, object aging process

I. Memory application process

• JVM will try to initialize a memory area for the relevant Java object in Eden;

• The Memory application ends when the Eden space is sufficient. Otherwise, go to the next step;

• JVM tries to release all inactive objects (minor collection) in Eden. If the Eden space is not enough to add new objects, it tries to put some active objects in the same vor area;

• The primary vor area is used as the intermediate swap area for Eden and old. When the old area has sufficient space, objects in the primary vor area will be moved to the old area; otherwise, objects in the primary vor area will be retained;

• When the space in the old area is insufficient, the JVM will perform a major collection in the old area;

• After full garbage collection, if the primary VOR and old areas still cannot store some objects copied from Eden, the JVM cannot create a memory area for new objects in the Eden area, the error "out of memory" appears ";

Ii. Object aging process

• The memory of the newly created object is allocated from Eden. The minor collection process is to copy the Eden and the live objects in the consumer vor space to the idle consumer vor space. After the minor collection object in young generation goes through a certain number of times (you can configure the parameter), it will be moved to old generation, called tenuring.

• GC trigger conditions

 

GC type Trigger Condition What happened when the trigger Note: View Method
Ygc Insufficient Eden Space

Clear the memory occupied by all No ref objects in Eden + from external vor.
Copy all surviving objects in Eden + from sur to Sur.
Some objects will be promoted to old:
To sur cannot be placed
The number of surviving times exceeds that in turning threshold
Re-calculate tenuring threshold (this will be triggered by serial parallel GC)

Re-adjust the size of Eden and from (parallel GC will trigger this item)

Suspend application throughout the process
Multi-threaded processing is determined by the specific GC.
Jstat-gcutil
GC log
FGC

Insufficient old space
Insufficient perm Space
Displays scheduled calls to system. GC, RMI, and so on.
Pessimistic strategy in ygc
Dump live memory information (jmap-dump: Live)

Clear No ref objects in heap
Class information loaded in the uninstalled classloader in permgen

If collectgenofirst is configured, ygc is triggered first (for serial GC)
If scavengebeforefullgc is configured, ygc is triggered first (for serial GC)

 
Suspend application throughout the process
Multi-threaded processing is determined by the specific GC.

Whether to compress depends on the specific GC configuration.
Jstat-gcutil
GC log

 

If the permanent generation space is insufficient, full GC is triggered, and the permgen space error is still caused.

 

  1. Heap size settings
  2. The maximum heap size in JVM has three restrictions: the data model (32-BT or 64-bit) of the relevant operating system; the available virtual memory limit of the system; and the available physical memory limit of the system. Generally, 32-bit systems are limited to Gbps ~ 2 GB; 64 indicates that the operating system has unlimited memory. In Windows Server 2003, 1478 GB physical memory, and JDK, the maximum value is MB.
    Typical settings:
    • Java-Xmx3550m-xms3550m-xmn2g-Xss128k
      -Xmx3550m: Set the maximum available JVM memory to 3550 MB.
      -Xms3550m: Set JVM to make the memory 3550 MB. This value can be set to the same as-xmx to avoid JVM re-allocating memory after each garbage collection.
      -Xmn2g: Set the young generation size to 2 GB.Memory size of the entire JVM = size of the young generation + size of the old generation + size of the persistent generation. The permanent generation usually has a fixed size of 64 M. Therefore, increasing the size of the young generation will reduce the size of the old generation. This value has a great impact on the system performance. Sun officially recommends 3/8 of the total heap configuration.
      -Xss128k: Set the stack size for each thread. After JDK 256, the size of each thread stack is 1 MB, and the size of each previous thread stack is K. The memory size required for more application threads is adjusted. Reduce this value to generate more threads in the same physical memory. However, the operating system still has a limit on the number of threads in a process. It cannot be generated infinitely. The experience is between 3000 and ~ About 5000.
    • Java-xmx3550m-xms3550m-xss128k-XX: newratio = 4-XX: Export vorratio = 4-XX: maxpermsize = 16 m-XX: maxtenuringthreshold = 0
      -XX: newratio = 4: Set the ratio of the young generation (including Eden and two primary vor regions) to the old generation (excluding the permanent generation ). If this parameter is set to 4, the ratio of the young generation to the old generation is, and the young generation accounts for 1/5 of the entire stack.
      -XX: Required vorratio = 4: Set the ratio of Eden to vor in the young generation. If this parameter is set to 4, the ratio of two vor zones to One Eden zone is, and one vor zone accounts for 1/6 of the young generation.
      -XX: maxpermsize = 16 m: Sets the persistent generation size to 16 Mb.
      -XX: maxtenuringthreshold = 0: Sets the maximum age of spam.If it is set to 0, the young generation object directly enters the old generation without going through the VOR area.. For applications with many older generations, the efficiency can be improved.If this value is set to a greater value, the young generation object will be copied multiple times in the same vor area, which can increase the survival time of the young generation object.To add an introduction to recycling in the young generation.
  3. Collector Selection
    JVM provides three options:Serial collector, parallel collector, and concurrent collectorBut the serial collector is only applicable to small data volumes. Therefore, the options here are mainly for parallel collectors and concurrent collectors. By default, jdk5.0 used a serial collector before. To use another collector, you must add the relevant parameters at startup. After jdk5.0, the JVM will judge based on the current system configuration.
    1. Throughput firstParallel collector
      As described above, parallel collectors are mainly aimed at reaching a certain throughput and are 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 young generations. In the preceding configuration, the young generation uses concurrent collection, while the old generation uses serial collection.
        -XX: parallelgcthreads = 20: Configure the number of parallel collector threads, that is, the number of threads at the same time for garbage collection. It is best to set this value to be equal to the number of processors.
      • Java-xmx3550m-xms3550m-xmn2g-xss128k-XX: + useparallelgc-XX: parallelgcthreads = 20-XX: + useparalleloldgc
        -XX: + useparalleloldgc: Configure the garbage collection method for the old generation to 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 garbage collection on the young generation. If this time cannot be met, the JVM automatically adjusts the size of the young generation to meet this value.
      • Java-xmx3550m-xms3550m-xmn2g-xss128k-XX: + useparallelgc-XX: maxgcpausemillis = 100-XX: + useadaptivesizepolicy
        -XX: + useadaptivesizepolicy
        : After this option is set, the parallel collector automatically selects the young generation zone size and the corresponding proportion of the region vor zone to achieve the minimum corresponding time or collection frequency specified by the target system, we recommend that you enable the parallel collector all the time.
    2. Response time firstConcurrency collector
      As described above, the concurrent collector mainly ensures the system response time and reduces the pause time during garbage collection. Applicable to application servers and telecommunications fields.
      Typical Configuration:
      • Java-xmx3550m-xms3550m-xmn2g-xss128k-XX: parallelgcthreads = 20-XX: + useconcmarksweepgc-XX: + useparnewgc
        -XX: + useconcmarksweepgc: Set the age for concurrent collection. After this is configured in the test, the-XX: newratio = 4 configuration is invalid because the cause is unknown. Therefore, it is best to set the size of the young generation with-xmn.
        -XX: + useparnewgc: Set the young generation for parallel collection. It can be used together with CMS collection. Jdk5.0 or above, JVM will be set based on the system configuration, so you do not need 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 organize the memory space, "fragments" are generated after running for a period of time, reducing the running efficiency. This value sets the number of GC operations to compress and organize the memory space.
        -XX: + usecmscompactatfullcollection: Enable compression for the old generation. Performance may be affected, but fragments can be eliminated.
  4. Auxiliary Information
    JVM provides a large number of command line parameters and prints information for debugging. There are mainly the following:
    • -XX: + printgc
      Output Format: [GC 118250 K-> 113543 K (130112 K), 0.0094143 secs]

      [Full GC 121376 K-> 10414 K (130112 K), 0.0650971 secs]

    • -XX: + printgcdetails
      Output Format: [GC [defnew: 8614 K-> 781 K (9088 K), 0.0123035 secs] 118250 K-> 113543 K (130112 K), 0.0124633 secs]

      [GC [defnew: 8614 K-> 8614 K (9088 K), 0.0000665 secs] [tenured: 112761 K-> 10414 K (121024 K ), 0.0433488 secs] 121376 K-> 10414 K (130112 K), 0.0436268 secs]

    • -XX: + printgctimestamps-XX: + printgc: printgctimestamps can be used together with the above two
      Output Format:11.851: [GC 98328 K-> 93620 K (130112 K), 0.0082960 secs]
    • -XX: + printgcapplicationconcurrenttime:Print the execution time of the program before each garbage collection. Can be used together with the above
      Output Format:Application Time: 0.5291524 seconds
    • -XX: + printgcapplicationstoppedtime: Print the time when the program is suspended during garbage collection. Can be used together with the above
      Output Format:Total time for which application threads were stopped: 0.0468229 seconds
    • -XX: printheapatgc: Print detailed stack information before and after GC
      Output Format:
      34.702: [GC {heap before GC invocations = 7:
      Def New Generation total 55296 K, used 52568 K [0x1ebd0000, 0x227d0000, 0x227d0000)
      Eden space 49152 K, 99% used[0x1ebd0000, 0x21bce430, 0x21bd0000)
      From space 6144 K, 55% used[0x221d0000, 0x22527e10, 0x227d0000)
      To space 6144 K, 0% used [0x21bd0000, 0x21bd0000, 0x221d0000)
      Tenured generation total 69632 K, used 2696 K [0x227d0000, 0x26bd0000, 0x26bd0000)
      The space 69632 K, 3% used[0x227d0000, 0x22a720f8, 0x22a72200, 0x26bd0000)
      Compacting perm Gen total 8192 K, used 2898 K [0x26bd0000, 0x273d0000, 0x2abd0000)
      The space 8192 K, 35% used [0x26bd0000, 0x26ea4ba8, 0x26ea4c00, 0x273d0000)
      RO space 8192 K, 66% used [0x2abd0000, 0x2b12bcc0, 0x2b12be00, 0x2b3d0000)
      RW space 12288 K, 46% used [0x2b3d0000, 0x2b972060, 0x2b972200, 0x2bfd0000)
      34.735: [defnew: 52568 K-> 3433 K (55296 K), 0.0072126 secs] 55264 K-> 6615 K (124928 K)Heap after GC invocations = 8:
      Def New Generation total 55296 K, used 3433 K [0x1ebd0000, 0x227d0000, 0x227d0000)
      Eden space 49152 K, 0% used[0x1ebd0000, 0x1ebd0000, 0x21bd0000)
      From space 6144 K, 55% used [0x21bd0000, 0x21f2a5e8, 0x221d0000)
      To space 6144 K, 0% used [0x221d0000, 0x221d0000, 0x227d0000)
      Tenured generation total 69632 K, used 3182 K [0x227d0000, 0x26bd0000, 0x26bd0000)
      The space 69632 K, 4% used[0x227d0000, 0x22aeb958, 0x22aeba00, 0x26bd0000)
      Compacting perm Gen total 8192 K, used 2898 K [0x26bd0000, 0x273d0000, 0x2abd0000)
      The space 8192 K, 35% used [0x26bd0000, 0x26ea4ba8, 0x26ea4c00, 0x273d0000)
      RO space 8192 K, 66% used [0x2abd0000, 0x2b12bcc0, 0x2b12be00, 0x2b3d0000)
      RW space 12288 K, 46% used [0x2b3d0000, 0x2b972060, 0x2b972200, 0x2bfd0000)
      }
      , 0.0757599 secs]
    • -Xloggc: Filename: Used in combination with the above, the relevant log information is recorded in the file for analysis.
  5. Common configuration Summary
    1. Heap settings
      • -XMS: Initial heap size
      • -Xmx: Maximum heap size
      • -XX: newsize = N: Set the young generation size
      • -XX: newratio = N:Set the ratio of the young generation to the old generation. For example, if the value is 3, the ratio of the young generation to the old generation is. The young generation accounts for 1/4 of the young generation and the old generation.
      • -XX: Required vorratio = N: Ratio of the Eden region in the young generation to the two vor regions. Note that there are two vor zones. For example, 3 indicates EDEN: Primary vor =. One primary vor zone accounts for 1/5 of the young generation.
      • -XX: maxpermsize = N: Sets the persistent generation size.
    2. Collector settings
      • -XX: + useserialgc: Set the serial collector
      • -XX: + useparallelgc: Set parallel collectors
      • -XX: + useparalledloldgc: Set the parallel elder generation collector
      • -XX: + useconcmarksweepgc: Set concurrent collectors
    3. Garbage collection statistics
      • -XX: + printgc
      • -XX: + printgcdetails
      • -XX: + printgctimestamps
      • -Xloggc: Filename
    4. Parallel collector settings
      • -XX: parallelgcthreads = N: Set the number of CPUs used for parallel collector collection. Number of parallel collection threads.
      • -XX: maxgcpausemillis = N: Sets the maximum pause time for parallel collection.
      • -XX: gctimeratio = N: Set the percentage of the garbage collection time to the running time. The formula is 1/(1 + n)
    5. Concurrent collector settings
      • -XX: + cmsincrementalmode: Set to incremental mode. Applicable to a single CPU.
      • -XX: parallelgcthreads = N: Sets the number of CPUs used by the concurrent collector when the collection mode of the young generation is parallel collection. Number of parallel collection threads.


Iv. Optimization Summary

    1. Young Generation Size Selection
      • Response time-first applications:Set as large as possible until it is close to the minimum response time limit of the system(Select based on the actual situation ). In this case, the collection frequency of the young generation is also the smallest. At the same time, reduce the number of objects that reach the old generation.
      • Throughput-first applications: Set as large as possible, possibly to Gbit level. Because there is no requirement on the response time, garbage collection can be performed in parallel, which is generally suitable for applications with more than 8 CPUs.
    2. Old Generation Size Selection
      • Response time-first applications: The concurrency collector is used in the old generation, so the size must be carefully set.Concurrent session RateAndSession durationAnd other parameters. If the heap settings are small, it may cause memory fragmentation, high recovery frequency, and application suspension. The traditional mark clearing method is used. If the heap is large, it takes a long time to collect. For the optimal solution, you generally need to obtain the following data:
        • Concurrent garbage collection information
        • Number of persistent generation concurrent collection times
        • Traditional GC Information
        • Ratio of time spent on the recovery of young and old generations
        Reducing the time spent by the young and old generations will generally improve application efficiency.
      • Throughput-first applications: Generally, applications with high throughput have a large young generation and a small old generation. The reason is that most of the short-term objects can be recycled as much as possible to reduce the medium-term objects, while the old generation will store the long-term surviving objects as much as possible.
    3. Fragmentation problem caused by a small heap
      The heap is not compressed because the concurrent collector of the old generation uses the marking and clearing algorithms. When the collector recycles the object, it merges the adjacent space and assigns it to a large object. However, when the heap space is small and runs for a period of time, "fragments" will appear. If the concurrent collector cannot find enough space, the concurrent collector will stop, then, use the traditional marking and clearing methods for recycling. If "fragments" appear, you may need to configure the following:
      • -XX: + usecmscompactatfullcollection: Enable compression for the old generation when concurrent collectors are used.
      • -XX: cmsfullgcsbeforecompaction = 0: When the above configuration is enabled, how many full GC times are set here to compress the old generation?

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.