The GC tuning

Preliminary knowledge:

  • GC related VM parameters: official documentation

  • Command to view VM parameters:
"C: \ Program Files \ Java \ jdk1.8.0 _201 \ bin \ Java" -XX:+PrintFlagsFinal -version | findstr "GC"
Copy the code

D:\Note\ Note\ JDK source code learning \IDEA-workspace\jdk8>"C: \ Program Files \ Java \ jdk1.8.0 _201 \ bin \ Java" -XX:+PrintFlagsFinal -version | findstr "GC"
    uintx AdaptiveSizeMajorGCDecayTimeScale         = 10                                  {product}
    uintx AutoGCSelectPauseMillis                   = 5000                                {product}
     bool BindGCTaskThreadsToCPUs                   = false                               {product}
    uintx CMSFullGCsBeforeCompaction                = 0                                   {product}
    uintx ConcGCThreads                             = 0                                   {product}
     bool DisableExplicitGC                         = false                               {product}
     bool ExplicitGCInvokesConcurrent               = false                               {product}
     bool ExplicitGCInvokesConcurrentAndUnloadsClasses  = false{product} uintx G1MixedGCCountTarget = 8 {product} uintx GCDrainStackTargetSize = 64 {product} uintx GCHeapFreeLimit = 2  {product} uintx GCLockerEdenExpansionPercent = 5 {product} bool GCLockerInvokesConcurrent =false                               {product}
    uintx GCLogFileSize                             = 8192                                {product}
    uintx GCPauseIntervalMillis                     = 0                                   {product}
    uintx GCTaskTimeStampEntries                    = 200                                 {product}
    uintx GCTimeLimit                               = 98                                  {product}
    uintx GCTimeRatio                               = 99                                  {product}
     bool HeapDumpAfterFullGC                       = false                               {manageable}
     bool HeapDumpBeforeFullGC                      = false                               {manageable}
    uintx HeapSizePerGCThread                       = 87241520                            {product}
    uintx MaxGCMinorPauseMillis                     = 4294967295                          {product}
    uintx MaxGCPauseMillis                          = 4294967295                          {product}
    uintx NumberOfGCLogFiles                        = 0                                   {product}
     intx ParGCArrayScanChunk                       = 50                                  {product}
    uintx ParGCDesiredObjsFromOverflowList          = 20                                  {product}
     bool ParGCTrimOverflow                         = true                                {product}
     bool ParGCUseLocalOverflow                     = false                               {product}
    uintx ParallelGCBufferWastePct                  = 10                                  {product}
    uintx ParallelGCThreads                         = 8                                   {product}
     bool ParallelGCVerbose                         = false                               {product}
     bool PrintClassHistogramAfterFullGC            = false                               {manageable}
     bool PrintClassHistogramBeforeFullGC           = false                               {manageable}
     bool PrintGC                                   = false                               {manageable}
     bool PrintGCApplicationConcurrentTime          = false                               {product}
     bool PrintGCApplicationStoppedTime             = false                               {product}
     bool PrintGCCause                              = true                                {product}
     bool PrintGCDateStamps                         = false                               {manageable}
     bool PrintGCDetails                            = false                               {manageable}
     bool PrintGCID                                 = false                               {manageable}
     bool PrintGCTaskTimeStamps                     = false                               {product}
     bool PrintGCTimeStamps                         = false                               {manageable}
     bool PrintHeapAtGC                             = false                               {product rw}
     bool PrintHeapAtGCExtended                     = false                               {product rw}
     bool PrintJNIGCStalls                          = false                               {product}
     bool PrintParallelOldGCPhaseTimes              = false                               {product}
     bool PrintReferenceGC                          = false                               {product}
     bool ScavengeBeforeFullGC                      = true                                {product}
     bool TraceDynamicGCThreads                     = false                               {product}
     bool TraceParallelOldGCTasks                   = false                               {product}
     bool UseAdaptiveGCBoundary                     = false                               {product}
     bool UseAdaptiveSizeDecayMajorGCCost           = true                                {product}
     bool UseAdaptiveSizePolicyWithSystemGC         = false                               {product}
     bool UseAutoGCSelectPolicy                     = false                               {product}
     bool UseConcMarkSweepGC                        = false                               {product}
     bool UseDynamicNumberOfGCThreads               = false                               {product}
     bool UseG1GC                                   = false                               {product}
     bool UseGCLogFileRotation                      = false                               {product}
     bool UseGCOverheadLimit                        = true                                {product}
     bool UseGCTaskAffinity                         = false                               {product}
     bool UseMaximumCompactionOnSystemGC            = true                                {product}
     bool UseParNewGC                               = false                               {product}
     bool UseParallelGC                            := true                                {product}
     bool UseParallelOldGC                          = true                                {product}
java version "1.8.0 comes with _201"
Java(TM) SE Runtime Environment (build 1.8.0_201-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.201-b09, mixed mode)
     bool UseSerialGC                               = false                               {product}
Copy the code
  • You can query specific information according to the parameters:

\

1. Tune the domain

  • memory
  • Lock contention
  • CPU
  • IO
  • GC

\

2. Define your goals

Low latency/high throughput? Choose the appropriate GC

  • CMS G1 ZGC
  • ParallelGC
  • Zing

\

3. The fastest GC is no GC

First rule out reducing memory problems caused by self-written code:

  • To view the memory footprint before and after Full GC, consider the following:

    • Is there too much data?

      • ResultSet = statement.executeQuery("select * from large table ");
    • Is the data representation too bloated?

      • Object graph
      • Object size, Java new Object or wrapper type Object, at least 16 bytes.
    • Is there a memory leak?

      • static Map map = HashMap()When we continuously add objects to a static map without removing them, memory may become tight.
      • Soft and weak references can be used to solve the above problem, because they can be recycled periodically when memory is tight. You can also use third-party caching middleware to store the data in the map above.

\

4. New generation tuning

  • Characteristics of the New Generation:

    • When we new an object, we allocate it in Eden first, and all new operations allocate memory very cheaply and very fast

      • TLAB(Thread-location Allocation Buffer) : When wenewAn object is checked to see if there is any memory available in the TLAB buffer, and if so, objects are allocated in TLAB first.
    • Dead object recovery costs zero

    • Most subjects die after using it.

    • MInor GC takes much less time than Full GC

  • Is bigger better for the new generation?

    • Not:

      • The new generation memory is too small: Triggering Minor GC frequently will STW, which will degrade throughput.
      • Too much memory in the new generation: The memory ratio of the old generation is reduced, which triggers the Full GC more frequently. It also takes longer to clean up the new generation when the Minor GC is triggered.
    • The new generation memory is set to accommodate concurrent *(request-response) data.

\

5. Tuning the survival zone

  • The survival zone can hold the most active objects + objects that need to be promoted.
  • A proper promotion threshold is configured to promote objects that survive for a long time.

\

6. Tune the old age

Take CMS as an example:

  • In the old days of CMS the bigger the better.

  • Try not to tune first, if there is no Full GC then the current system does not need to be optimized, otherwise, try to tune the new generation first.

  • Observe the memory usage of the old age when Full GC occurs, and preset the old age memory by 1/4 to 1/3.

    • -XX:CMSInitiatingOccupancyFraction=percent

\

Case 7.

  • When the Full and Minor GC are called frequently.
  • When Full GC occurs at peak request times, single pause times are extremely long (CMS)
  • Full GC occurs when old age is abundant (1.7)

The above concentration is the need to tune!