Chapter 26 Analyzing GC Logs

Chapter 26 Analyzing GC Logs

JVM explained by Song Hongkang from Shang Silicon Valley: bilibili link

Insert picture description here

1 GC log parameters

Insert picture description here

2 GC log format

2.1 GC classification

According to the implementation of HotSpot VM, the GC in it can be divided into the public type according to the recovery area: one is partial collection (Partial GC), the other is full collection (Full GC)

  • Partial collection: Garbage collection is not a complete collection of the entire Java heap. Which is divided into
    • Young GC (Minor GC / Young GC): Only the new generation (Eden\S0, S1) garbage collection
    • Old generation collection (Major GC / Old GC): Only old generation garbage collection.
      • Currently, only CMS GC will collect the old generation separately.
      • Note that in many cases Major GC will be confused with Full GC. It is necessary to distinguish whether it is an old generation or a whole heap collection.
    • Mixed collection (Mixed GC): Collect garbage collection of the entire young generation and part of the old generation
      • Currently, only G1 GC will have this behavior
  • Full GC: Collect garbage collection of the entire Java heap and method area.

  • Under what circumstances will Full GC start?
    • Insufficient space in the old age
    • Insufficient method area
    • Call System.gc() explicitly
    • The average size of the data entering the old generation in the Minor GC is greater than the available memory in the old generation
    • Large objects enter the old age directly, and the available space in the old age is insufficient

2.2 GC log classification

  • Minor GC

    Insert picture description here

  • Full GC

    Insert picture description here

2.3 Analysis of GC log structure

Garbage collector

  • The name of the new generation using the Serial collector is Default New Generation, so "[DefNew" is displayed
  • Using the ParNew collector, the name of the new generation will become "[ParNew", meaning: "Parallel New Generation"
  • The name of the Parallel Scavenage collector in the new generation is "[PSYoungGen", where JDK1.7 uses PSYoungGEN
  • The name of the Parallel Old Generation collector used in the old generation is "[ParOldGen"
  • If you use the G1 collector, "garbage-fisrt heap" will be displayed

Allocation Failure : Indicates that the cause of GC this time is because there is not enough space in the new generation to store new data

Before and after GC

  • Through the diagram, we can find that the law of GC log is generally: memory occupation before GC ----> memory occupation after GC (total memory size in this area)

    [PSYoungGen:5986K->696K(8704K)]5986K->704K(9216K)
    

    In brackets: the size of the memory occupied by the new generation before GC collection, and the size after the collection (the total size of the new generation heap)

    Outside the brackets: the size of the memory occupied by the new generation and the old generation before GC collection, and the size after the collection (the total size of the new generation and the old generation)

GC time

There are three times in the GC log: user, sys and real

  • user: The time used by the process to execute user-mode code (outside the core). This is the actual CPU time used to execute this process , other processes and the time this process is blocked are not included. In the case of garbage collection, it represents the total CPU time used by the GC thread to execute.
  • sys: The CPU time consumed by the process in the kernel mode, that is , the CPU time used by the kernel to execute system calls or wait for system events .
  • real: The clock time from the beginning to the end of the program. This time includes the time slice used by other processes and the time the process is blocked (for example, waiting for I/O to complete). For parallel gc, this number should be close to (user time + system time) / the number of threads used by the garbage collector.

Due to multi-core reasons, in general GC events, real time is less than sys+user time, because multiple threads are generally used to perform GC concurrently, so rea time is less than sys+user time. If real>sys+user time, your application may have the following problems: the IO load is very heavy or the CPU is not enough.

2.4 Minor GC log analysis

2020-11-20T17:19:43.265-0800:0.822:[GC (ALLOCATION FAILURE)[PSYOUNGGEN:76800K->8843k(89600K)]76800K->8449K(29400K), 0.008371 SECS] [TIMES:USER=0.02 SYS=0.01, REAL=0.01 SECS]

Insert picture description here

2.4 Full GC log analysis

2020-11-20T17:19:43.794-0800:1.352:[FULL GC (METADATA GC THREADHOLD)[PSYOUNGGEN:10082K->0K(89600K)][PAROLDGEN]:32K->9638K(204800K)]10114K->9638K(294400K),[METASPACE:20158K->20156K(1067008K)], 0.0285388 SECS][TIMES:USER=0.11 SYS=0.00, REAL=0.03 SECS]

Insert picture description here

3 GC log analysis tool

  • The previous section introduced the meaning of GC log printing, but the GC log looks troublesome. This section will introduce the GC log visual analysis tools GCEasy and GCViewer. Through the GC log visual analysis tool, we can easily see the memory usage, garbage collection times, garbage collection reasons, garbage collection time, throughput, etc. of each generation of JVM. These indicators are when we perform JVM tuning. Still very useful.

  • If you want to save the GC log to a file, it is the following parameter:

    -Xloggc:/path/to/gc.log
    

    Then you can use some tools to analyze these gc logs.

3.1 GCEasy

  • GCEasy-------A super easy-to-use website for online analysis of GC logs

  • GCEasy can use GC log analysis for memory leak detection, GC exhibition hall cause analysis, JVM configuration suggestion optimization and other functions, and it is free to use (some services are charged)

  • Official website

    Insert picture description here

3.2 GCViewer

  • This is an offline version of GC log analysis tool

  • GCViewer is a free, open source analysis tool that can be used to visually view the logs of garbage collectors generated by SUN/Oracle, IBM, HP and BEA Java virtual machines.

  • GCViewer can be used to visualize the data generated by the Java VM option -verbose:gc and .NET -Xloggc:<file>. It also calculates performance metrics related to garbage collection (throughput, cumulative pauses, longest pauses, etc.). This feature is very useful when adjusting the garbage collection of a specific application by changing the generation size or setting the initial heap size.

  • Download the GCViewer tool

    • Source download: URL
    • Run version download: URL

    Just double-click gcviewer-1.3x.jar or run java -jar gcviewer-1.3x.jar (it needs to run java 1.8 vm) to start GCViewer (gui)

3.3 Other tools

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_42638946/article/details/113836682