Quick Start jvm tuning: GC tuning parameters and ideas

Based on "jvm performance Definitive Guide", summed up the knowledge point gc tuning. Then hoping to help you get started quickly jvm gc tuning.
Details here principles garbage collection not only related to performance tuning.

First, the garbage collector

1. The main features of the garbage collector
The garbage collector main feature Enable parameters
Serial garbage collector Client mode default garbage collector;
single-threaded recovery;
MinorGC and FullGC will suspend the application thread.
-XX:+UseSerialGC
Parallel garbage collector Server mode is the default garbage collector (jdk9 ago);
the use of multi-threading recovery;
MinorGC and FullGC will suspend the application threads, FullGC years old will be compressed space consolidation.
-XX:+UseParallelGC,
-XX:+UseParallelOldGC
CMS garbage collector The use of multi-threading recovery;
MinorGC years old and concurrent application threads to suspend recovery stage, there will be space debris problem.
-XX:+UseParNewGC,
-XX:+UseConcMarkSweepGC
G1 garbage collector After jdk9 default garbage collector;
the use of non-contiguous blocks of management of heap memory;
multi-threaded parallel recovery;
MinorGC years old and part of the concurrent recovery stage suspend application threads.
-XX:+UseG1GC
2. How to choose the garbage collector
  • Small memory applications directly using Serial, stable and efficient
  • Most applications selected in Parallel and the CMS:
    • Parallel + Parallel old portfolio focuses on throughput for batch jobs
    • Use CMS under sufficient CPU resources (CMS using only the old year and in line with serial / parallel new)
    • CMS issues concurrent mode failure occurs when the CPU resources are limited, degraded to Serial Old
  • Under normal circumstances, when the stack space is less than 4G, CMS performance better than the G1, but a better performance of large Duixia G1

Two, GC base tune

1. Analysis Tools

All tuning are based on a certain understanding of the use of memory on the heap, so we first need to use some tools for our java application monitoring and analysis, to understand what went wrong in the end:

  • GC logs
    • -XX: + PrintGCDetails: Print GC logs
    • -XX: + PrintGCTimeStamps: GC log to print a timestamp
  • Heap real-time monitoring
    • Use jstat -gcutil PID 1000 Order: Print heap memory per second real-time information and GC case
2. Adjust the heap size
  • Consider dimensions:
    • GC Frequency: need to increase the heap size is too frequently
    • Single GC time: taking a long time to consider control heap size
    • GC percentage in the whole time
    • FullGC the remaining available space: 70% of the available common experience
    • Tuning parameters:
      • -Xms = N, -Xmx = N: initial and maximum heap
      • -Xmn = N: set a fixed size of the new generation
      • -XX: NewRatio = N: Set the new generation's footprint than the old (this value is the denominator)
      • -XX: NewSize = N, -XX: MaxNewSize = N: Set the new generation of the initial and maximum
3. Generation and permanent element space
  • Dimensional space by default will use as much space, so do not be too concerned about
  • Permanent Generation (jdk7 and before)
    • -XX: PermSize = N, -XXMaxPermSize = N: setting a fixed maximum size and generation of a permanent
  • Element space (jdk8 later)
    • -XX: MetaspaceSize = N, -XX: MaxMetaspaceSize = N: setting a fixed maximum size and the space element
4. Concurrent Control
  • Number control thread: -XX: ParallelGCThreads = N, impact multithreading:
    • Parallel to collect the new generation and the old year
    • CMS: the new generation of the collection (ParNewGC), concurrent collection STW stage (non-FullGC)
    • G1 young generation collections, STW stage (non-FullGC)
  • The default number of threads calculation (n is the number of threads cpu): ParallelGCThreads = 8 + ((N-8) * 5/8)
    • When the machine CPU threads are more then the default number of threads will be relatively large, but jvm to run multiple instances simultaneously, then the need to manually control the number of threads, to avoid excessive garbage collection threads run concurrently.
5. adaptively

JVM will own performance parameter adjustment based on past performance history

  • -XX: -UseAdaptiveSizePolicy: Close adaptive adjustment function (enabled by default)
  • -XX: + PrintAdaptiveSizePolicy: print automatic adjustment information

Third, the Advanced Tuning

1. Parallel GC
  • Parallel collector heap size will adjust based on the index of adaptive
    • -XX: MaxGCPauseMillis = N: set the maximum pause time
      • Tend to reduce the size of the old year, it may trigger frequent FullGC
    • -XX: GCTimeRatio = N: Set the application thread runs than the garbage collection time period (default 99)
      • N = the proportion of the application time / (l - time accounting application)
      • 99 N =, Time represents 1% of the total GC
      • Tend to increase the size of the heap
2. CMS
  • Degenerate into a situation FullGC:
    • Concurrent mode failure: Cenozoic garbage collection, while years old there is not enough space to accommodate objects promotion
    • Promotion failed: there is enough space to accommodate old's promotion target, but due to space debris leads to promotion failure
    • Permanent Generation space exhaustion
  • Concurrency control cycle time to start
    • -XX: + UseCMSInitiatingOccupancyOnly: CMS does not automatically adjust the garbage collection cycle (default false)
    • -XX: CMSInitiationOccupancyFraction = N: trigger concurrent collection cycle years old footprint than the threshold (default 70)
      • Do not set the threshold too low, the number of active data within a stack of at least 10% more than 20%, frequent concurrent cycle STW will lead to an overall halt excessive
  • CMS adjust background thread
    • -XX: ConcGCThreads = N: the number of background thread
      • The default value ParallelGCThreads calculated according to: N = (3 + ParallelGCThreads) / 4
3. G1
  • Degenerate into a situation FullGC:
    • Concurrent mode failure :( reference cms)
    • Promotion failed :( reference cms)
    • Evacuation failure: When the new generation garbage collection, Suvivor's old space and space is not enough space to accommodate the surviving objects
    • Allocation failure huge objects: Object required too much memory, memory size than a single block (or other restrictions)
  • Adjust the number of background threads G1
    • -XX: ConcGCThreads = N: the number of background thread
      • The default value ParallelGCThreads calculated according to: N = (2 + ParallelGCThreads) / 4
  • Adjust the frequency of garbage collection
    • -XX: InitiationHeapOccupancyPercent = N: triggering simultaneous collection heap memory usage period than a threshold value (default 45)
  • Adjustment hybrid garbage collection cycle
    • -XX: G1MixedGCLiveThresholdPercent = N: Trigger partition recovery partition labeled junk threshold proportion
    • -XX: G1MixedGCCountTarget = N: maximum number of hybrid GC period (default 8)
      • Decreasing this value can help solve the problem failed promotion, the cost of hybrid GC cycle pause longer
    • -XX: MaxGCPauseMillis = N: Set the maximum pause time (default 200ms)
4. Survivor space and promotion:
  • Set Survivor space
    • -XX: InitialSurvivorRatio = N: initial accounting Survivor space (N is the denominator)
      • survivor space = new_size / (InitialSurvivorRatio + 2)
    • -XX: MinSurvivorRatio = N: Survivor maximum size (note that since N is the denominator, although the parameters min, but it is set to the maximum value)
  • Automatic adjustment
    • -XX: TargetSurvivorRatio = N: the proportion of free space garbage collection
  • Promotion threshold: After how many cycles GC object moves back and forth in the space between the Survivor's promotion to the old
    • -XX: InitialTenuringThreshold = N: initial promotion threshold
    • -XX: MaxTenuringThreshold = N: maximum promotion threshold
    • The JVM continuous calculation, select the appropriate value in a threshold value and a maximum promotion
Published 35 original articles · won praise 104 · Views 100,000 +

Guess you like

Origin blog.csdn.net/wk52525/article/details/94899432