Java Performance Optimization-sun hotspot jdk jvm parameter settings

Source: Internet
Author: User
Tags xms

This document is based on this PDF (gctuningguidelines.

The first is the new feature ergonomic settings introduced by jdk1.5 (automatically optimized parameters)

For servers with 2 CPUs, 2 GB memory, and better configuration, sun's hotspot JDK sets the following parameters by default:

  • -Server: Server Mode Compilation
  • -XX: + useparallelgc parallel collection
  • -Set XMS to 1/64 of the server's physical memory
  • -Set xmx to 1/4 of the server's physical memory (up to 1 GB)

I found this article on the Internet, which was also written based on the pdf. It was basically translated, so I will no longer create a wheel. Some of your ideas are marked in red.

From http://unixboy.javaeye.com/

Original article-Summary of JVM Optimization-XMS-xmx-xmn-XSS

  1. Heap size settingsThe 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 (in this example, the heap allocation is relatively large. Pay attention to the restrictions on your own platform, as shown in the following article ):

    • 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 prevent JVM from re-allocating memory after each garbage collection (that is, shrinking and expansion, generation-based collection plus sun Memory Allocation Algorithm, this avoids the same defects of the ibm jdk minimum heap and the maximum heap, but this requires higher-XMS and-xmx settings, it should be multiple tests to determine a proper size ).

      -Xmn2g: Set the young generation size to 2 GB.Total heap size = Young Generation size + Old Generation size + persistent generation size. 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. From, you can seeTotal heap size = Young Generation size + Old Generation size, XMS and xmx do not include perm size.

    • -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 under 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 increase the probability that the young generation will be recycled. The CMS (concurrent) Collector, vorratio 1024 by default maxtenuringthreshold 0, can be manually adjusted to reduce the recycling pressure of the older generation, that is, taking care of the throughput, pay attention to the corresponding time.

  2. Collector SelectionJVM 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 firstAs described above, the parallel collector is mainly used to reach a certain throughput, and is suitable for Scientific and Technical 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. It should be equal to or less than the number of CPUs (cores), otherwise it does not make sense. When the number of CPUs on the server is less than or equal to 2, concurrent collection is the same as the serial collection efficiency.

      • 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. -XX: gctimeratio = <NNN> indicates the ratio of GC time to running time. GC time occupies 1/(1 + <NNN>) of the whole running time ). The pausemillis parameter has a higher priority than gctimeratio and is only valid for parallel collectors. But from "Additionally, as an implicit goal the throughput collector will try to meet the other goals in the smallest heap that it can. I think the JVM is not an automatic adjustment of the young generation, but the size of the entire heap. I personally think that a range of heap size should be set at this time, and I cannot manually set-xmn2g; otherwise, how can I adjust it? Unfortunately, it is not clearly stated in the document, but it is fortunate that the general adjustment does not have to be so detailed.
      • 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. In version 1.5, http://java.sun.com/j2se/1.5.0/docs/guide/vm/gc-ergonomics.htmlis enabled for details.
    2. Response time firstAs described above, the concurrent collector is mainly used to ensure the system response time and reduce 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.

  3. Auxiliary InformationJVM provides a large number of command line parameters and prints information for debugging. There are mainly the following:
    • -XX: + printgcOutput Format: [GC 118250 K-> 113543 K (130112 K), 0.0094143 secs][Full GC 121376 K-> 10414 K (130112 K), 0.0650971 secs]
    • -XX: + printgcdetailsOutput 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 mixed with the preceding two output formats: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. The output format can be mixed with the above:Application Time: 0.5291524 seconds

       

    • -XX: + printgcapplicationstoppedtime: Print the time when the program is suspended during garbage collection. The output format can be mixed with the above:Total time for which application threads were stopped: 0.0468229 seconds

       

    • -XX: printheapatgc: Print the detailed stack information output form before and after GC:

      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.
  4. 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 heapThe heap is not compressed because the concurrent collector of the old generation uses the marking and clearing algorithm. 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 "Fragmentation" exists, you may need to configure it as follows:
    • -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?

5. Other parameters mentioned in PDF

-XX: + aggressiveopts: it is used as its name (aggressive). When this parameter is enabled, your JVM will use the latest optimization technology when the JDK version is upgraded (if any)

-XX: + usebiasedlocking: Not very understandable, please read the http://java.sun.com/performance/reference/whitepapers/tuning.html#section4.2.5 on your own

V. References

Java hotspot VM options

Java tuning White Paper

Diagnosing a garbage collection problem

J2se 5.0 performance White Paper

Tuning garbage collection with the 5.0 Java [Tm] Virtual Machine

Garbage Collector ergonomics

Can anyone help me understand useadaptivesizepolicy flag?

 

 

 

Note:

This article Reprinted from: http://www.hashei.me/2009/05/tuning-the-sun-hotspot-jdk.html

Related Article

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.