This is the second article in the "become a GC Expert Series. In the first article, we learned the processing process of several different GC Algorithms in Java garbage collection, the working method of GC, and the difference between the new generation and the old generation. So far, you have learned about the five GC types in JDK 7 and their impact on performance. In this article, I will introduce
How does JVM run GC in a real environment?.
What is GC monitoring?
GC monitoringIt refers to the process of tracing JVM running GC at runtime. For example, through GC monitoring, we can find out:
- When will new generation objects be moved to the old age and how many objects will be moved to the old age.
- When stop-the-world occurs and the duration.
Through GC monitoring, you can find out whether the JVM is effectively running GC and whether additional GC optimization is required. Based on this information, we can optimize the application or change the GC running mode (GC optimization) To improve application performance.
How to perform GC monitoring
There are many GC monitoring methods. The difference is that the GC operation information is displayed differently. GC is triggered by JVM. because the information displayed by the GC monitoring tool is provided by JVM, no matter which method is used for GC monitoring, the final information obtained is consistent. Therefore, there is no need to study every GC monitoring tool in depth. you only need to spend some time learning how to use each tool and select the appropriate tool on different occasions.
Because the JVM specification does not require standard methods to expose GC information, the tools or JVM options listed below cannot apply to all different JVM implementations. The introduction below is based onHotspot JVM(Oracle JVM. BecauseNHNOracle (Sun) JVM is used, so it is not too difficult to use the following tools or JVM options.
First, GC monitoring tools are dividedCUIAndGUI. The classic CUI tool can use a single CUI applicationJstat, You can also provide"-Verbosegc"Option.
The gui gc monitoring tool is implemented through a separate GUI application. The following describes three common gui gc tools: jconsole, jvisualvm, and Visual GC.
Next we will learn about each GC monitoring method:
Jstat
JstatIs a built-in monitoring tool for Hotspot JVM. Hotspot JVM also has other built-in monitoring tools suchJpsAndJstatd. These three tools are sometimes required to monitor the running of Java applications.
JstatIt not only provides information about GC operations, but also provides information about class loading and real-time compiler operations. However, this article will only involve the GC operations provided by jstat.
JstatLocated in$JDK_HOME/bin
Directory. if The java or javac command can run normally, the jstat command should also be able to run.
You can try it in the command line:
$> jstat –gc $
1000S0C S1C S0U S1U EC EU OC OU PC PU YGC YGCT FGC FGCT GCT3008.0 3072.0 0.0 1511.1 343360.0 46383.0 699072.0 283690.2 75392.0 41064.3 2540 18.454 4 1.133 19.5883008.0 3072.0 0.0 1511.1 343360.0 47530.9 699072.0 283690.2 75392.0 41064.3 2540 18.454 4 1.133 19.5883008.0 3072.0 0.0 1511.1 343360.0 47793.0 699072.0 283690.2 75392.0 41064.3 2540 18.454 4 1.133 19.588$>
As shown above, the actual data in each part of the memory is listed in the order of the following columns:
SOC S1C S0U S1U EC EU OU PC
Vmid(Virtual Machine id: Virtual Machine ID ).ID. Virtual machines running locally or remotely can be specified by vmid. The vmid of a Java application running on a local virtual machine is also calledLvmid(Local vmid), usually the same as the PID. Although you can usePsCommand or Windows Task Manager to view the PID value to obtain the lvmid, but we recommend that you useJpsBecause the PID and lvmid do not always correspond one by one.JpsJava PS. The ps command shows the PIDs and process names, and the vmids and main methods are displayed through jps.
Use jps to find the vmid of the Java application you want to monitor and use it as the jstat parameter. If multiple WAS instances run on the same device, you can only find information about the bootstrap program by using the jps command. At this timeps -ef | grep java
Command and jps command.
GC performance data needs to be continuously observed. Therefore, when running jstat, you need to periodically output GC monitoring information.
For example: Runjstat -gc 1000
(Or 1 s) the GC data is output every 1 s on the console.jstat -gc 1000 10
GC data will be output once every 1 s and 10 times in total.
Besides GC-related options-Gc, There are others, as shown in the following table:
Option name |
Description |
Gc |
Output the current size and usage of each partition in the heap space (Ede, batch VOR, Old, etc.), the total number of GC executions, and the cumulative execution duration. |
Gccapacity |
The minimum and maximum capacity of each partition in the output heap space, the current size, and the number of GC executions in each partition (the current usage and accumulated GC time are not output ). |
Gccause |
In addition to the information provided by-gcutil, the cause of the last GC and the current GC is also output. |
Gcnew |
GC performance data in the new generation. |
Gcnewcapacity |
Statistical information of the new generation capacity. |
Gcold |
GC performance data of the old generation. |
Gcoldcapacity |
The capacity statistics of the old generation. |
Gcpermcapacity |
The statistical information on the persistent generation (method area. |
Gcutil |
Output the usage of each partition in % format. At the same time, the total number of GC executions and accumulated time consumption are also output. |
If only GC frequency is concerned-Gcutil(Or-gccause ),-Gc,-GccapacityYou can.
- -GcutilIt is used to detect the usage, GC execution times, and accumulated time in each region,
- -GccapacityAnd other options can be used to output the actually allocated memory size.
Use-GcThe output of the options is as follows:
S0C S1C … GCT1248.0 896.0 … 1.2461248.0 896.0 … 1.246… … … …
Specifying different options for jstat lists different columns, as shown below. The jstat options that output this information are listed on the right of the table.
Data column |
Description |
Supported jstat options |
S0C |
Current capacity of worker vor0 |
-Gc -Gccapacity -Gcnew -Gcnewcapacity |
S1C |
Current capacity of S1 |
-Gc -Gccapacity -Gcnew -Gcnewcapacity |
S0U |
S0 usage |
-Gc -Gcnew |
S1U |
S1 usage |
-Gc -Gcnew |
EC |
Current capacity of Eden Zone |
-Gc -Gccapacity -Gcnew -Gcnewcapacity |
EU |
Eden Zone usage |
-Gc -Gcnew |
OC |
Current capacity of the old zone |
-Gc -Gccapacity -Gcnew -Gcnewcapacity |
OU |
Old zone usage |
-Gc -Gcnew |
PC |
Current capacity of the method area |
-Gc -Gccapacity -Gcold -Gcoldcapacity -Gcpermcapacity |
PU |
Method area usage |
-Gc -Gcold |
YGC |
Young GC count |
-Gc -Gccapacity -Gcnew -Gcnewcapacity -Gcold -Gcoldcapacity -Gcpermcapacity -Gcutil -Gccause |
YGCT |
Young GC cumulative time consumption |
-Gc -Gcnew -Gcutil -Gccause |
FGC |
Full GC count |
-Gc -Gccapacity -Gcnew -Gcnewcapacity -Gcold -Gcoldcapacity -Gcpermcapacity -Gcutil -Gccause |
FGCT |
Full GC cumulative time consumption |
-Gc -Gcold -Gcoldcapacity -Gcpermcapacity -Gcutil -Gccause |
GCT |
Total GC duration |
-Gc -Gcold -Gcoldcapacity -Gccapacity -Gcpermcapacity -Gcutil -Gccause |
Ngcen |
Minimum capacity of new generation |
-Gccapacity -Gcnewcapacity |
NGCMX |
Maximum capacity of the new generation |
-Gccapacity -Gcnewcapacity |
NGC |
Current capacity of the new generation |
-Gccapacity -Gcnewcapacity |
Ogcen |
Minimum capacity in old age |
-Gccapacity -Gcoldcapacity |
OGCMX |
Max capacity in old age |
-Gccapacity -Gcoldcapacity |
OGC |
Current capacity in old age |
-Gccapacity -Gcoldcapacity |
Pgcen |
Minimum capacity in the method area |
-Gccapacity -Gcpermcapacity |
PGCMX |
Maximum capacity in the method area |
-Gccapacity -Gcpermcapacity |
PGC |
Current capacity of the method area |
-Gccapacity -Gcpermcapacity |
PC |
Current capacity of the method area |
-Gccapacity -Gcpermcapacity |
PU |
Method area usage |
-Gccapacity -Gcold |
LGCC |
The cause of the last GC |
-Gccause |
GCC |
Current GC cause |
-Gccause |
TT |
Survival threshold. if the number of new-generation object movements exceeds this threshold, the object will be moved to the old age. |
-Gcnew |
MTT |
Maximum survival threshold value. if the number of new generations of objects moving exceeds this threshold value, the objects will be moved to the old age. |
-Gcnew |
DSS |
Ideal capacity of the region vor |
-Gcnew |
The unit of capacity in the table is KB.
JstatThe advantage is that you can use the jstat command to view GC operation-related data, whether it is a local or remote Java application, and output this information through the console. In use-GcutilThe following fields are output. Pay special attention to GC optimization.YGC,YGCT,FGC,FGCTAndGCT.
S0 S1 E O P YGC YGCT FGC FGCT GCT0.00 66.44 54.12 10.58 86.63 217 0.928 2 0.067 0.9950.00 66.44 54.12 10.58 86.63 217 0.928 2 0.067 0.9950.00 66.44 54.12 10.58 86.63 217 0.928 2 0.067 0.995
This information is very important. it counts the time consumption during GC operation and reflects GC performance indicators.
In the preceding example,YGCIt is 217 times,YGCTIt is 0.928, and every young GC time is 4 ms (0.004 s) on average ). The average time consumption of full GC can also be calculated as 33 ms.
However, the average value is not very helpful for the GC problems discovered, because each GC consumes a huge deviation (that is, if the average value of full GC is 0.067 s, it may mean that one GC took 1 ms, and the other GC lasted 134 ms ). To observe the independent time consumption rather than the average value of each GC, it is better to use-Verbosegc.
-Verbosegc
-VerbosegcIs a JVM option for running Java applications.JstatYou can monitor any JVM application without specifying startup parameters,-VerbosegcYou must specify this parameter when enabling the application, so it looks like-VerbosegcIt is not a necessary option (because jstat can be used to do the same job ). However, when GC occurs-VerbosegcThe output information is easier to understand, which is more advantageous for monitoring complicated GC information.
|
Jstat |
-Verbosegc |
Monitoring target |
Java applications that can output logs to the terminal or remote Java applications that can connect to the network through jstatd |
Java applications with the-verbosegc parameter specified when JVM is started |
Output information |
Heap status (usage, maximum capacity, GC count, and accumulated time) |
Capacity Change and GC time consumption of the new generation and old generation before and after each GC |
Output time |
Any specified time |
When any GC occurs |
Advantages |
Allows you to observe heap size continuously. |
Observe the impact of a single GC on the system |
In use-VerbosegcYou can also specify the following additional options:
- -XX: + PrintGCDetails
- -XX: + PrintGCTimeStamps
- -XX: + PrintHeapAtGC
- -XX: + PrintGCDateStamps (options introduced by JDK6U4)
If you only specify-VerbosegcBy default-XX: + PrintGCDetails. In addition,-VerbosegcCan be combined.
Use-VerbosegcWhen a minor GC occurs, the output data format is as follows:
[GC [
:
->
,
secs]
->
,
secs]
Field |
Description |
Collector |
Collector used |
Starting occupancy1 |
New generation size before GC occurrence |
Ending occupancy1 |
Size of the new generation after GC |
Pause time1 |
Java application pause duration when minor GC is executed |
Starting occupancy3 |
Total size of heap space before GC occurrence |
Ending occupancy3 |
Total heap space after GC |
Pause time3 |
Java application downtime when the overall GC (including Full GC) is executed |
The following is an example of Full GC output:
[Full GC [Tenured: 3485K->4095K(4096K), 0.1745373 secs] 61244K->7418K(63104K), [Perm : 10756K->10756K(12288K)], 0.1762129 secs] [Times: user=0.19 sys=0.00, real=0.19 secs]
If the [CMS recycle algorithm] () is used, information about CMS will be provided immediately.
Because-VerbosegcYou can output the information of each GC operation in log mode, so it is easy to observe the heap usage changes after GC is disabled.
(Java) VisualVM + Visual GC
Java Virsual VM is a GUI-based chart/monitoring tool provided by Oracle JDK.
: VirsualVM interface
Different from the version built in JDK, you can download Virsual VM separately on the website. For convenience, JDK's built-in version is called Java VirsualVM (jvisualvm), and Virsual VM (visualvm) is downloaded separately from the website ). The features of the two are not exactly the same, and there are slight differences in some aspects (such as installing plug-ins. Personally, I prefer to use a separately downloaded Virsual VM.
After Visual VM is started, if you select the application you want to monitor on the left panel, you will see the "Monitoring" column. You can obtain basic GC and memory heap information from the Monitoring column.
Although the basic status of GC can be obtained through the basic features of Visual VMJstatAnd-VerbosegcFor more detailed information.
If you want the same details as jstat, you need to install the corresponding Virsual VM plug-in. You can obtain the Virsual GC plug-in from the Tools menu.
: Virsual GC installation interface
By using Virsual GC, you can obtainJstatdProvided information.
: Virsual GC running interface
HPJMeter
HPJMeter is an analysis-VerbosegcA convenient tool for outputting results. If Visual GC is regarded as the GUI version of jstat, HPJMeter is the GUI version of-verbosegc. GC analysis is just one of the many features provided by HPJMeter. HPJMeter is a performance monitoring tool developed by HP that can be used on HP-UX, Linux, and MS Windows.
At first, it was justHPTuneProvides a GUI for analysis.-Verbosegc. HPTune has been integrated since HPJMeter 3.0, so you do not need to download HPTune separately.
During application running,-VerbosegcCan be redirected to a separate file.
You can use HPJMeter to open the file and use the GUI to analyze GC Data conveniently.
: HPJMeter