Jstat is a lightweight gadget that comes with the JDK. The full name is "Java Virtual Machine statistics monitoring tool", which is located in the bin directory of java. It mainly uses the built-in instructions of the JVM to monitor the resources and performance of Java applications in real-time on the command line, including Heap size and garbage collection status. monitoring. It can be seen that Jstat is a lightweight, JVM-specific tool that is very suitable.
The jstat tool is particularly powerful, with numerous options to view the usage of various parts of the heap in detail, as well as the number of loaded classes. When using, you need to add the process id of the viewing process and the selected parameters.
Execution: Execute jstat in cd $JAVA_HOME/bin, pay attention to the parameters after jstat.
Syntax structure:
Usage: jstat -help|-options
jstat -<option> [-t] [-h<lines>] <vmid> [<interval> [<count>]]
Parameter explanation:
Options — options, we generally use -gcutil View gc situation vmid — VM process number
, that is, the currently running java process number
interval — interval time, in seconds or milliseconds information. -gcutil parameter S0 — Survivor space 0 area on the Heap percentage of used space S1 — Survivor space 1 area on the Heap percentage of used space
E — Percentage of used space in Eden space area on
Heap O — Percentage of used space in Old space area on Heap
P — Percentage of used space in Perm space area
YGC — Number of times Young GC occurs from application startup to sampling
YGCT – Time spent in Young GC from application startup to sampling time (in seconds)
FGC – Number of Full GCs
from application startup to sampling time FGCT – Time spent in Full GC from application startup to sampling time (in seconds)
GCT — The total time (in seconds) used for garbage collection from application startup to sampling
jstat is used as follows:
sudo jps is used to display the local java process, you can view several java programs running locally, and display their processes No.
1. Use the top command to view the pid of java, and find that there are 2 java processes, not sure which corresponds to the process number of the web application
- webserver@WS12:~$ top
- top - 11:34:00 up 112 days, 10:48, 2 users, load average: 0.77, 0.36, 0.26
- Tasks: 247 total, 1 running, 246 sleeping, 0 stopped, 0 zombie
- Cpu (s): 15.8%us, 1.5%sy, 0.0%ni, 82.5%id, 0.0%wa, 0.0%hi, 0.1%si, 0.0%st
- Mem: 12321892k total, 2440448k used, 9881444k free, 515004k buffers
- Swap: 31249400k total, 112k used, 31249288k free, 995320k cached
- PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
- 16183 root 20 0 1564m 175m 12m S 126 1.5 0:03.82 java
- 15581 root 20 0 305m 54m 9988 S 1 0.5 0:05.13 java
- 15689 www-data 20 0 55084 28m 956 S 1 0.2 0:01.51 nginx
- 15688 www-data 20 0 55332 28m 956 S 0 0.2 0:04.63 nginx
- 16182 webserver 20 0 19356 1528 1064 R 0 0.0 0:00.02 top
- 1 root 20 0 23704 1932 1296 S 0 0.0 0:02.24 init
- 2 root 20 0 0 0 0 S 0 0.0 0:00.01 kthreadd
- 3 root RT 0 0 0 0 S 0 0.0 0:00.02 migration/0
2. Using ps -ef | grep java , you can see the process number corresponding to the java application. ps -ef | grep tomcat sees less things, it's easier to find
- webserver@WS11:/usr/local/resin$ ps -ef | grep java
- root 17778 1 0 13:10 pts/0 00:00:16 /usr/lib/jvm/java-6-sun-1.6.0.20/bin/java -Djava.util.logging.manager=com.caucho.log.LogManagerImpl -Djavax.management.builder.initial=com.caucho.jmx.MBeanServerBuilderImpl -Djava.awt.headless=true -Dresin.home=/usr/local/resin -Dresin.root=/usr/local/resin/ -Xrs -Xss256k -Xmx32m -Dcom.sun.management.jmxremote -d64 com.caucho.boot.WatchdogManager -resin-home /usr/local/resin -conf /usr/local/resin/conf/resin-8080.conf start
- root 18293 17778 8 13:56 pts/0 00:07:14 /usr/lib/jvm/java-6-sun-1.6.0.20/bin/java -Djava.util.logging.manager=com.caucho.log.LogManagerImpl -Djava.system.class.loader=com.caucho.loader.SystemClassLoader -Djavax.management.builder.initial=com.caucho.jmx.MBeanServerBuilderImpl -Djava.awt.headless=true -Dresin.home=/usr/local/resin [color=red]-Xmx4096m -Xms4096m -Xmn1024m -Xss1m -Xdebug -Dcom.sun.management.jmxremote -XX:PermSize=512M -XX:MaxPermSize=1024M[/color] -Djava.util.logging.manager=com.caucho.log.LogManagerImpl -Djavax.management.builder.initial=com.caucho.jmx.MBeanServerBuilderImpl -Djava.awt.headless=true -Dresin.home=/usr/local/resin -Dresin.root=/usr/local/resin/ -Dcom.sun.management.jmxremote -Djava.util.logging.manager=com.caucho.log.LogManagerImpl -Djavax.management.builder.initial=com.caucho.jmx.MBeanServerBuilderImpl -Djav .awt.headless=true -Dresin.home=/usr/local/resin -Dresin.root=/usr/local/resin/ -Dcom.sun.management.jmxremote com.caucho.server.resin.Resin --root-directory /usr/local/resin -conf /usr/local/resin/conf/resin-8080.conf -socketwait 29006 -resin-home /usr/local/resin start
- webserver 18867 14532 0 15:21 pts/0 00:00:00 grep --color=auto java
3. Execute sudo jstat -gcutil process number 5s (check jvm garbage collection every 5 seconds)
- webserver@WS12:~$ sudo jstat -gcutil 16183 5s
- S0 S1 E O P YGC YGCT FGC FGCT GCT
- 5.02 0.00 49.75 30.01 65.49 14 0.421 1 0.121 0.542
- 5.02 0.00 88.80 30.01 65.62 14 0.421 1 0.121 0.542
- 0.00 96.66 32.79 30.44 65.81 15 0.428 1 0.121 0.549
- 0.00 96.66 72.60 30.44 65.93 15 0.428 1 0.121 0.549
- 1.20 0.00 38.76 30.61 66.01 16 0.439 1 0.121 0.560
- 1.20 0.00 63.20 30.61 66.08 16 0.439 1 0.121 0.560
- 0.00 1.28 30.78 30.71 66.45 17 0.446 1 0.121 0.567
- 0.00 1.28 67.98 30.71 66.57 17 0.446 1 0.121 0.567
- 1.65 0.00 8.04 30.81 66.67 18 0.452 1 0.121 0.573
- 1.65 0.00 47.42 30.81 66.82 18 0.452 1 0.121 0.573
- 1.65 0.00 90.38 30.81 66.91 18 0.452 1 0.121 0.573
- 0.00 1.41 37.02 30.94 66.97 19 0.458 1 0.121 0.578
- 0.00 1.41 75.75 30.94 66.98 19 0.458 1 0.121 0.578
- 1.19 0.00 18.04 31.04 67.13 20 0.465 1 0.121 0.585
Other available options:
You can use the command: jstat -options option to see the supported option parameters
-class (class loader)
-compiler (JIT) -gc
(GC heap status)
-gccapacity (size of each area)
-gccause (last GC Statistics and reasons)
-gcnew (new area statistics)
-gcnewcapacity (new area size)
-gcold (old area statistics)
-gcoldcapacity (old area size)
-gcpermcapacity (permanent area size)
-gcutil (GC statistics summary)
-printcompilation (HotSpot compilation statistics)
jstat -gcutil pid: Statistics gc information statistics.
jstat -gccapacity pid: You can display the usage and size of the three generations (young, old, perm) objects in the VM memory -
gccapacity parameters:
NGCMN: The size of the initialization (minimum) in the young generation (young) (bytes)
NGCMX: young The maximum capacity of the generation (young) (bytes)
NGC: the current capacity (bytes) in the young generation (young)
S0C: the capacity of the first survivor (survivor area) in the young generation (bytes)
S1C: in the young generation The capacity (bytes) of the second survivor (survivor area)
EC: The capacity of Eden (Garden of Eden) in the young generation (bytes)
OGCMN: The size of the initialization (minimum) in the
old generation (bytes) OGCMX: The maximum capacity of the old generation (bytes)
OGC: The current newly generated capacity of the old generation (bytes)
OC: the capacity of the Old generation (bytes)
PGCMN: the initialized (minimum) size of the perm generation (bytes)
PGCMX: the maximum capacity of the perm generation (bytes)
PGC: the current newly generated capacity of the perm generation ( Bytes)
PC: Perm (persistent generation) capacity (bytes)
YGC: GC times in the young generation from application startup to sampling time
FGC: GC times from application startup to sampling time old generation (full gc)
jps -mlVv localhost
jps [ options ] [ hostid ]
where options can be used:
-q (quiet)
-m (output parameters passed to the main method)
-l (display full path)
-v (display command line parameters passed to the JVM)
- V (display the parameters passed to the JVM through the flag file)
-J (similar to other Java tools for passing parameters to the java process to be invoked by the command itself);
hostid is the host id, the default is localhost.
Jstack Usage:
jstack [-l] <pid>
(to connect to running process)
jstack -F [-m] [-l] <pid>
(to connect to a hung process)
jstack [-m] [-l] < executable> <core>
(to connect to a core file)
jstack [-m] [-l] [server_id@]<remote server IP or hostname>
(to connect to a remote debug server to a remote machine)
Options:
-F to force a thread dump. Use when jstack <pid> does not respond (process is hung)
-m to print both java and native frames (mixed mode)
-l long listing. Prints additional information about locks
-h or -help to print this help message
jstack 3713;
jstack -F 3713
jstack -l -m /opt/jdk1.6.0/bin/java core.659
can observe the running status and current status of all threads in jvm
- Attaching to process ID 3713, please wait...
- Debugger attached successfully.
- Server compiler detected.
- JVM version is 20.4-b02
- Deadlock Detection:
- No deadlocks found.
- Thread 12172: (state = BLOCKED)
- - sun.misc.Unsafe.park(boolean, long) @bci=0 (Interpreted frame)
jmap: Display information about the memory usage of the java process
jmap pid #Print the summary information of memory usage
jmap –heap pid #java heap information
jmap -histo:live pid #Statistical object count, live means using
jmap -histo pid >mem.txt #Print the relatively simple information about how many objects each occupies and how much memory, generally redirected files
jmap -dump:format=b,file=mem.dat pid #Output the details of memory usage to the mem.dat file
jvm Configure and print GC related information
-XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintHeapAtGC
The corresponding log will be output to the catalina log
jinfo -flag HeapDumpBeforeFullGC 29167 #View HeapDumpBeforeFullGC
jinfo: View and modify JVM parameters
Usage:
jinfo < option> <pid>
(to connect to a running process)
where <option> is one of:
-flag <name> to print the value of the named VM flag
-flag [+|-]<name> to enable or disable the named VM flag
-flag <name>=<value> to set the named VM flag to the given value
-h | -help to print this help message