Optimizing GC Analysis
Principles
Each GC process is to clean up the JVM heap space. When the JVM executes the GC service, the process is suspended. The aim of GC optimization is to reduce the impact on services as much as possible. In general, GC optimization is to minimize the GC frequency and each GC pause duration. You can use GC logs and the jstat tool to monitor, analyze, and optimize the GC in real time.
The following is the GC information printed by running the jstat command every second. Only the columns related to GC frequency and duration are displayed. For details about the usage and output of the jstat command, see jstat.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | YGC YGCT FGC FGCT GCT 579 22.233 2 0.090 22.323 579 22.233 2 0.090 22.323 579 22.233 2 0.090 22.323 579 22.233 2 0.090 22.323 579 22.233 2 0.090 22.323 579 22.233 2 0.090 22.323 579 22.233 2 0.090 22.323 579 22.233 2 0.090 22.323 580 22.262 2 0.090 22.352 580 22.262 2 0.090 22.352 580 22.262 2 0.090 22.352 580 22.262 2 0.090 22.352 580 22.262 2 0.090 22.352 580 22.262 2 0.090 22.352 580 22.262 2 0.090 22.352 580 22.262 2 0.090 22.352 581 22.282 2 0.090 22.372 581 22.282 2 0.090 22.372 581 22.282 2 0.090 22.372 581 22.282 2 0.090 22.372 581 22.282 2 0.090 22.372 581 22.282 2 0.090 22.372 581 22.282 2 0.090 22.372 582 22.282 2 0.090 22.372 582 22.300 2 0.090 22.391 582 22.300 2 0.090 22.391 582 22.300 2 0.090 22.391 582 22.300 2 0.090 22.391 582 22.300 2 0.090 22.391 582 22.300 2 0.090 22.391 582 22.300 2 0.090 22.391 |
- According to the preceding information, YGC (young GC/minor GC) is executed for four times, that is, the 579th, 580th, 581st, and 582nd times. FGC (full GC) is not executed.
- Calculate the GC frequency: The monitoring information is printed every second. The YGC 580 is printed for eight times. That is, the YGC is triggered eight seconds after the last GC is complete. The next record is the YGC 581. That is, the YGC frequency is once every 8 seconds. The FGC frequency can be calculated in the same way.
- Calculate the GC duration (service pause duration): YGCT indicates the total duration of all YGC operations since the Java process is started. To calculate the duration for one YGC operation, subtract the current YGC time from the next YGC time. For example, the duration of the 580th YGC operation = 22.282 (YGCT 581) - 22.262 (YGCT 580), that is, 20 ms. The FGC duration can be calculated in the same way. The GC duration varies depending on the service running pressure.
There is no recognized reasonable range for the GC frequency and the GC duration. The values depend on the server hardware (such as the memory size and CPU processing capability) and software and vary according to services.
Suggestions:
- The average YGC duration is less than 50 ms.
- The YGC execution frequency is greater than or equal to once every 10 seconds.
- The average FGC duration is less than 1s.
- The FGC execution frequency is greater than or equal to once every 10 minutes.
Modification Method
Troubleshooting and optimization suggestions:
- If the FGC is triggered too frequently, check whether the service triggers the FGC by checking whether the service calls System.gc(). Delete unnecessary FGC call from the code or set the -XX:DisableExplicitGC parameter.
1# /path/java -XX:DisableExplicitGC <other parameters> - If the FGC is triggered too frequently, increase the size of the permanent generation or the old generation. For details, see Setting JVM Heap Size.
- If the YGC is triggered too frequently, increase the size of the young generation or the entire heap. For details, see Setting JVM Heap Size. If too many service application objects are generated and cannot be reused, you can optimize the service code to reduce the YGC frequency.
- If the YGC takes a long time, decrease the size of the young generation or the entire heap. For details, see Setting JVM Heap Size.
- If the FGC/YGC takes a long time and the system CPU resources are not a bottleneck, increase the number of GC threads. For details, see Optimizing JVM Threads.
- GC optimization needs to be performed based on service analysis and service characteristics.
- Increasing or decreasing the heap size brings both advantages and disadvantages, which can be determined based on service performance concerns.
- A larger young generation will decrease the size of the old generation. A larger young generation will decrease the YGC frequency and increase the YGC duration. A smaller old generation will increase the FGC frequency.
- A smaller young-generation space will make the old-generation space larger. A smaller young-generation space will cause frequent YGC, but the time required for each YGC will be reduced. A larger old-generation space will reduce the FGC frequency.