JVM parameter configuration Daquan

Several interview questions I encountered a while ago were all about Java memory control, so I found this article from the Internet, hoping to have a new understanding of Java memory allocation

/usr/local/jdk/bin/java -Dresin.home=/usr/local/resin -server -Xms1800M -Xmx1800M -Xmn300M -Xss512K -XX:PermSize=300M -XX:MaxPermSize=300M -XX:SurvivorRatio=8 -XX:MaxTenuringThreshold=5 -XX:GCTimeRatio=19 -Xnoclassgc -XX:+DisableExplicitGC -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:+UseCMSCompactAtFullCollection -XX:CMSFullGCsBeforeCompaction=0 -XX:-CMSParallelRemarkEnabled -XX:CMSInitiatingOccupancyFraction=70 -XX:SoftRefLRUPolicyMSPerMB=0 -XX:+PrintClassHistogram -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintHeapAtGC -Xloggc:log/gc.log

  1. Heap Size Setting
    the maximum heap size in the JVM has three limitations: the data model (32-bit or 64-bit) limit of the relevant operating system; the system's available virtual memory limit; the system's available physical memory limit. Under the 32-bit system, it is generally limited to 1.5G~2G; 64 means that the operating system has no limit on the memory. I tested under Windows Server 2003 system, 3.5G physical memory, JDK5.0, the maximum can be set to 1478m.
    Typical setup:
    • java  -Xmx3550m -Xms3550m -Xmn2g  -Xss128k
      - Xmx3550m : Set the maximum available memory of the JVM to 3550M.
      -Xms3550m : Set the JVM to promote memory to 3550m. This value can be set the same as -Xmx to avoid reallocation of memory by the JVM after each garbage collection is complete.
      -Xmn2g : Set the young generation size to 2G. Whole heap size = young generation size + old generation size + persistent generation size . The persistent generation is generally fixed at 64m, so after increasing the young generation, the size of the old generation will be reduced. This value has a great impact on system performance, and Sun officially recommends setting it to 3/8 of the entire heap.
      -Xss128k : Set the stack size per thread. After JDK5.0, the stack size of each thread is 1M, and the previous stack size of each thread is 256K. Adjust the memory size required by the threads of the application. In the same physical memory, reducing this value can generate more threads. However, the operating system still has a limit on the number of threads in a process, which cannot be generated indefinitely, and the experience value is around 3000~5000.
    • java -Xmx3550m -Xms3550m -Xss128k  -XX:NewRatio=4 -XX:SurvivorRatio=4 -XX:MaxPermSize=16m -XX:MaxTenuringThreshold=0
      -XX:NewRatio=4 : Set the young generation (including Eden and two Survivor areas) Ratio to old generation (excluding persistent generation). If set to 4, the ratio of the young generation to the old generation is 1:4, and the young generation accounts for 1/5 of the entire stack.
      -XX:SurvivorRatio=4 : Set the size ratio of the Eden area to the Survivor area in the young generation. If set to 4, the ratio of two Survivor areas to one Eden area is 2:4, and one Survivor area occupies 1/6 of the entire young generation
      -XX:MaxPermSize=16m : Set the persistent generation size to 16m.
      -XX:MaxTenuringThreshold=0 : Set the maximum age of garbage. If it is set to 0, the young generation objects directly enter the old generation without passing through the Survivor area . For applications with a large number of old generations, the efficiency can be improved. If this value is set to a larger value, the young generation object will be replicated multiple times in the Survivor area, which can increase the survival time of the object in the young generation and increase the generalization that it will be recycled in the young generation.
  2. Collector selection
    JVM gives three choices: serial collector, parallel collector, concurrent collector , but serial collector is only suitable for small data volume, so the choice here is mainly for parallel collector and concurrent collector . By default, JDK5.0 used to use serial collectors. If you want to use other collectors, you need to add corresponding parameters at startup. After JDK5.0, the JVM will judge according to the current system configuration .
    1. Throughput -first parallel collectors
      As mentioned above, parallel collectors mainly aim to achieve 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 the young generation. That is, under the above configuration, the young generation uses concurrent collection, while the old generation still uses serial collection.
        -XX:ParallelGCThreads=20 : Configure the number of threads for the parallel collector, that is, how many threads perform garbage collection together at the same time. 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 as parallel collection. JDK6.0 supports parallel collection of the old generation.
      • java -Xmx3550m -Xms3550m -Xmn2g -Xss128k -XX:+UseParallelGC  -XX:MaxGCPauseMillis=100
        -XX:MaxGCPauseMillis=100 : Set the maximum time for each young generation garbage collection. If this time cannot be met, the JVM will automatically adjust the young Generation size to satisfy this value.
      • java -Xmx3550m -Xms3550m -Xmn2g -Xss128k -XX:+UseParallelGC -XX:MaxGCPauseMillis=100 -XX:+UseAdaptiveSizePolicy
        -XX:+UseAdaptiveSizePolicy
        : After setting this option, the parallel collector will automatically select the young generation area size and the corresponding Survivor Area ratio to achieve the minimum corresponding time or collection frequency specified by the target system, etc. This value is recommended to be turned on when the parallel collector is used.
  3. Concurrent collectors with priority on response time
    As mentioned above, concurrent collectors mainly ensure the response time of the system and reduce the pause time during garbage collection. Applicable to application servers, telecommunication fields, etc.
    Typical configuration :
    • java -Xmx3550m -Xms3550m -Xmn2g -Xss128k -XX:ParallelGCThreads=20  -XX:+UseConcMarkSweepGC -XX:+UseParNewGC
      -XX:+UseConcMarkSweepGC : Set the old generation for concurrent collection. After configuring this in the test, the configuration of -XX:NewRatio=4 is invalid for unknown reasons. Therefore, the young generation size is best set with -Xmn at this time.
      -XX:+UseParNewGC : Set the young generation for parallel collection. Can be used concurrently with CMS collection. Above JDK5.0, the JVM will set itself according to the system configuration, so there is no need to set this value.
    • java -Xmx3550m -Xms3550m -Xmn2g -Xss128k -XX:+UseConcMarkSweepGC  -XX:CMSFullGCsBeforeCompaction=5 -XX:+UseCMSCompactAtFullCollection
      -XX:CMSFullGCsBeforeCompaction : Since the concurrent collector does not compress and organize the memory space, it will generate "" Fragmentation", which reduces operational efficiency. This value sets how many times to compress and organize the memory space after running GC.
      -XX:+UseCMSCompactAtFullCollection : Turn on compression for the old generation. May affect performance, but can eliminate fragmentation
  • Auxiliary information
    JVM provides a large number of command line parameters, printing information, for debugging. There are mainly the following:
    • -XX:+PrintGC
      output format : [GC 118250K->113543K(130112K), 0.0094143 secs]

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

    • -XX:+PrintGCDetails
      output format : [GC [DefNew: 8614K->781K(9088K), 0.0123035 secs] 118250K->113543K(130112K), 0.0124633 secs]

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

    • -XX:+PrintGCTimeStamps  -XX:+PrintGC:PrintGCTimeStamps can be mixed with the above two
      output formats: 11.851: [GC 98328K->93620K(130112K), 0.0082960 secs]
    • -XX:+PrintGCApplicationConcurrentTime: Print the uninterrupted execution time of the program before each garbage collection.
      The output form can be mixed with the above : Application time: 0.5291524 seconds
    • -XX:+PrintGCApplicationStoppedTime : Print the time the program was paused during garbage collection.
      The output form can be mixed with the above : Total time for which application threads were stopped: 0.0468229 seconds
    • -XX:PrintHeapAtGC:打印GC前后的详细堆栈信息
      输出形式:
      34.702: [GC {Heap before gc invocations=7:
      def new generation   total 55296K, used 52568K [0x1ebd0000, 0x227d0000, 0x227d0000)
      eden space 49152K, 99% used [0x1ebd0000, 0x21bce430, 0x21bd0000)
      from space 6144K, 55% used [0x221d0000, 0x22527e10, 0x227d0000)
      to   space 6144K,   0% used [0x21bd0000, 0x21bd0000, 0x221d0000)
      tenured generation   total 69632K, used 2696K [0x227d0000, 0x26bd0000, 0x26bd0000)
      the space 69632K,   3% used [0x227d0000, 0x22a720f8, 0x22a72200, 0x26bd0000)
      compacting perm gen total 8192K, used 2898K [0x26bd0000, 0x273d0000, 0x2abd0000)
         the space 8192K, 35% used [0x26bd0000, 0x26ea4ba8, 0x26ea4c00, 0x273d0000)
          ro space 8192K, 66% used [0x2abd0000, 0x2b12bcc0, 0x2b12be00, 0x2b3d0000)
          rw space 12288K, 46% used [0x2b3d0000, 0x2b972060, 0x2b972200, 0x2bfd0000)
      34.735: [DefNew: 52568K->3433K(55296K), 0.0072126 secs] 55264K->6615K(124928K)Heap after gc invocations=8:
      def new generation   total 55296K, used 3433K [0x1ebd0000, 0x227d0000, 0x227d0000)
      eden space 49152K,   0% used [0x1ebd0000, 0x1ebd0000, 0x21bd0000)
      from space 6144K, 55% used [0x21bd0000, 0x21f2a5e8, 0x221d0000)
      to   space 6144K,   0% used [0x221d0000, 0x221d0000, 0x227d0000)
      tenured generation   total 69632K, used 3182K [0x227d0000, 0x26bd0000, 0x26bd0000)
      the space 69632K,   4% used [0x227d0000, 0x22aeb958, 0x22aeba00, 0x26bd0000)
      compacting perm gen total 8192K, used 2898K [0x26bd0000, 0x273d0000, 0x2abd0000)
         the space 8192K, 35% used [0x26bd0000, 0x26ea4ba8, 0x26ea4c00, 0x273d0000)
          ro space 8192K, 66% used [0x2abd0000, 0x2b12bcc0, 0x2b12be00, 0x2b3d0000)
          rw space 12288K, 46% used [0x2b3d0000, 0x2b972060, 0x2b972200, 0x2bfd0000)
      }
      , 0.0757599 secs]
    • -Xloggc:filename : Used in conjunction with the above, log relevant log information to a file for analysis.
  • Summary of common configurations
    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 young generation to old generation. For example, it is 3, which means that the ratio of young generation to old generation is 1:3, and the young generation accounts for 1/4 of the sum of the young generation and the old generation.
      • -XX:SurvivorRatio=n : The ratio of the Eden area to the two Survivor areas in the young generation. Note that the Survivor area has two. For example: 3 means Eden: Survivor=3:2, a Survivor area occupies 1/5 of the entire young generation
      • -XX:MaxPermSize=n : Set persistent generation size
  • Collector settings
    • -XX:+UseSerialGC : Set serial collector
    • -XX:+UseParallelGC : set parallel collector
    • -XX:+UseParalledlOldGC : Set the parallel old generation collector
    • -XX:+UseConcMarkSweepGC : Set concurrent collector
  • Garbage Collection Statistics
    • -XX:+PrintGC
    • -XX:+PrintGCDetails
    • -XX:+PrintGCTimeStamps
    • -Xloggc:filename
  • Parallel collector settings
    • -XX:ParallelGCThreads=n : Set the number of CPUs used by the parallel collector for collection. Number of parallel collection threads.
    • -XX:MaxGCPauseMillis=n : Set the maximum pause time for parallel collection
    • -XX:GCTimeRatio=n : Set the percentage of garbage collection time to program running time. The formula is 1/(1+n)
  • Concurrent collector settings
    • -XX:+CMSIncrementalMode : Set to incremental mode. Suitable for single CPU situation.
    • -XX:ParallelGCThreads=n : Set the number of CPUs used when the young generation collection mode of the concurrent collector is parallel collection. Number of parallel collection threads.

4. Tuning summary

  1. Young Generation Size Selection
    • Response time priority applications : set as large as possible until it is close to the system's minimum response time limit (selected according to actual conditions). In this case, the frequency of young generation collections is also minimal. At the same time, reduce the objects that reach the old generation.
    • Applications with throughput priority : set as large as possible, possibly reaching the level of Gbit. Because there is no requirement for 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 priority applications : The old generation uses concurrent collectors, so its size needs to be set carefully, generally considering some parameters such as the concurrent session rate and session duration . If the heap setting is small, it can cause memory fragmentation, high collection frequency and application suspension and use the traditional mark-and-sweep method; if the heap is large, it will take a long time to collect. The optimal solution generally needs to be obtained by referring to the following data:
      • Concurrent garbage collection information
      • Persistent Generation Concurrent Collection Times
      • Legacy GC Information
      • Proportion of time spent on young and old collections
  3. Reducing the time spent in the young and old generation generally improves the efficiency of the application
  4. Throughput-first applications : Generally, throughput-first applications 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, reducing the medium-term objects, and the old generation can store long-lived objects.
  5. Fragmentation problems with smaller heaps
    Because the old generation concurrent collector uses a mark-and-sweep algorithm, the heap is not compacted. When the collector recycles, it merges adjacent spaces so that they can be allocated to larger objects. However, when the heap space is small, after running for a period of time, "fragmentation" will appear. If the concurrent collector cannot find enough space, the concurrent collector will stop and then use the traditional mark and clear method for recycling. If "fragmentation" occurs, the following configuration may be required:
    • -XX:+UseCMSCompactAtFullCollection : When using the concurrent collector, enable compaction of the old generation.
    • -XX:CMSFullGCsBeforeCompaction=0 : When the above configuration is enabled, how many times of Full GC are set here to compress the old generation
  6. jvm memory limit

        windows2003 is 1612M

 

Source: http://www.cnblogs.com/edwardlauxh/archive/2010/04/25/1918603.html

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326803938&siteId=291194637