JDK comes with tools
Command line tool
jps (query process)
List the virtual machine processes that are running on the current machine. JPS searches from the temporary directory of the operating system (so some information may not be displayed completely)
-q: show only the process
-m: output the parameters passed by the main function, the hello below is the parameters entered from the command line when the program is executed
-l: Output the complete package name or jar full name of the main class of the application
-v: List jvm parameters, -Xms20m -Xmx50m are the JVM parameters specified by the startup program
jstat (monitoring)
Command tool used to monitor various operating status information of virtual machines. It can display running data such as class loading, memory, garbage collection, JIT compilation, etc. in the local or remote virtual machine process. On a server that does not have a GUI graphical interface and only provides a plain text console environment, it will be a virtual location during runtime. The tool of choice for machine performance problems.
Common parameters:
-class (class loader)
-compiler(JIT)
-gc (GC heap status)
-gccapacity (size of each district)
-gcnew (new district statistics)
-gcnewcapacity (new area size)
-gcold (old district statistics)
-gcoldcapacity (old area size)
-gcpermcapacity (permanent area size)
-gcutil (GC statistics summary)
-printcompilation (HotSpot compilation statistics)
For example: we want to count GC, which is garbage collection. Then just use such a command.
jstat-gc 13616 (this 13616 is the JVM process, obtained through the JPS command), so the statistics are real-time values. So in many cases, we can play like this in order to see the change value.
First use JPS to locate the process started by the main function
Assuming that it takes 250 milliseconds to query the garbage collection status of this process, a total of 10 queries are made. Then the command should be jstat -gc 4700 250 10. The query results are as follows:
S0C: The size of the first surviving area (From area)
S1C: The size of the second survival area (To area)
S0U: The used size of the first surviving area
S1U: The used size of the second survivor area
EC: The size of the Eden area
EU: Use size of Eden area
OC: the size of the old age OU: the size used in the old age
MC: Method area size
MU: Method area used size
CCSC: Compressed class space size
CCSU: Compressed class space usage size
YGC: The number of young generation garbage collections
YGCT: Young generation garbage collection consumes time
FGC: Number of garbage collections in the old age
FGCT: Garbage collection in the old age consumes time
GCT: Total time consumed by garbage collection
jinfo (View and modify virtual machine parameters)
jinfo -sysprops can view the parameters obtained by System.getProperties()
jinfo -flag is not explicitly specified by the system default value of the parameter
jinfo -flags (note s) displays the parameters of the virtual machine
VM parameter classification
JVM command line parameter reference: https://docs.oracle.com/javase/8/docs/technotes/tools/unix/java.html
At the beginning of the standard, all virtual machines support
Ensure that all implementations of the Java Virtual Machine (JVM) support standard options. They are used to perform common operations, such as checking the JRE version, setting the classpath, enabling detailed output, etc.
HotSpot virtual machine exclusive parameters
Non-standard options are generic options specific to the Java HotSpot virtual machine, so there is no guarantee that all JVM implementations will support them, and they may change. These options begin with -X.
-Xms30m -Xmx30m -Xss1m
advanced options
These are developer options that are used to adjust specific areas of the Java HotSpot virtual machine operation. These areas usually have specific system requirements and may require privileged access to system configuration parameters. There is no guarantee that all JVM implementations support them, and they may change.
On windows, you can query all -XX parameters through the following java -XX:+PrintFlagsFinal --version
Note: The parameter of manageable means that it can be modified at runtime.
The demonstration example is as follows:
First of all, we know from the above figure that the XX parameter of PrintGC can be modified at runtime.
jinfo -flag -[parameter] pid can modify parameters
Case:
- When running the program, we did not print GC
- View parameters through jinfo and print GC details
- Modify parameters through jinfo and print GC details
- Go back to the program to view the console, the GC information has appeared, which proves that the parameters can be modified during the runtime
Summary: Through the jinfo command, we can temporarily open the GC log on the production environment, or configure some data. ( Without the need to restart the application ), it is also a key command for us to troubleshoot problems.
jmap (generate heap dump snapshot dump)
Used to generate heap dump snapshots (generally called heapdump or dump files). The role of jmap is not only to obtain dump files, it can also query the finalize execution queue, Java heap and permanent generation detailed information, such as space usage, which collector is currently used, etc. Like the jinfo command, many functions of jmap are restricted under the Windows platform, except for the -dump option for generating dump files and the -histo option for viewing the instance and space usage statistics of each class. Except provided, the other options can only be used under Linux/Solaris.
-heap print heap
jmap –heap <pid>
Heap Configuration: ##Heap configuration, which is the result of JVM parameter configuration [Usually said tomcat configures JVM parameters, which is configuring these]
MinHeapFreeRatio = 40 ##Minimum heap usage ratio
MaxHeapFreeRatio = 70 ##Maximum heap available ratio
MaxHeapSize
= 2147483648 (2048.0MB) ##Maximum heap size
NewSize
= 268435456 (256.0MB) ##New generation allocation size
MaxNewSize
= 268435456 (256.0MB) ##Maximum new generation allocation size
OldSize
= 5439488 (5.1875MB) ##Old age size
NewRatio
= 2 ##Proportion of new generation
SurvivorRatio
= 8 ##The ratio of the new generation to the suvivor
PermSize
= 134217728 (128.0MB) ##perm area permanent generation size
MaxPermSize
= 134217728 (128.0MB) ##The maximum perm area that can be allocated is the permanent generation size
Heap Usage: ##Heap usage [the actual usage of heap memory]
New Generation (Eden + 1 Survivor Space): ##New Generation (Eden District + Survior (1+2) Space)
capacity = 241631232 (230.4375MB) ##Eden District Capacity
used
= 77776272 (74.17323303222656MB) ##Already used size
free
= 163854960 (156.26426696777344MB) ##Remaining capacity
32.188004570534986% used ##Use ratio
Eden Space: ##伊登区
capacity = 214827008 (204.875MB) ##Eden District Capacity
used
= 74442288 (70.99369812011719MB) ##Used in Eden District
free
= 140384720 (133.8813018798828MB) ##Current remaining capacity of Eden District
34.65220164496263% used ## Eden District usage
From Space: ##survior1 区
capacity = 26804224 (25.5625MB) ##survior1 District capacity
used
= 3333984 (3.179534912109375MB) ##surviror1 Area has been used free
= 23470240 (22.382965087890625MB) ##surviror1 Area remaining capacity
12.43827838477995% used ##survior1 area usage ratio
To Space: ##survior2 区
capacity = 26804224 (25.5625MB) ##survior2 District capacity
used
= 0 (0.0MB) ##survior2 Area used
free
= 26804224 (25.5625MB) ##survior2 area remaining capacity
0.0% used ## survior2 area used ratio
PS Old Generation: ##Old Generation Usage
capacity = 1879048192 (1792.0MB) ##Old age capacity
used
= 30847928 (29.41887664794922MB) ##Used capacity in the old age
free
= 1848200264 (1762.5811233520508MB) ##Remaining capacity in the old age
1.6416783843721663% used ##Used ratio of old age
-histo Print the number of instances of each class , memory usage , and full name information of the class .
jmap –histo <pid>
jmap –histo:live <pid>
If the live sub-parameter is added, only the number of live objects will be counted
But there are too many displays like this, it is usually done on linux
jmap –histo 1196 | head -20
(This will only show the top 20 data) Less important parameters
-finalizerinfo prints information about objects waiting to be recycled, and the jmap -clstats command is best not to be used
-dump generated heap dump snapshot (more important)
jmap -dump:live,format=b,file=heap.bin <pid>
Sun JDK provides jhat (JVM Heap Analysis Tool) commands to be used with jmap to analyze heap dump snapshots generated by jmap
jhat (Analyze dump files with Jmap)
jhat dump file name
After the screen displays the prompt "Server is ready.", the user can access the details by typing http://localhost:7000/ in the browser
Use jhat to generate heap dump file analysis on the server (generally not recommended, after all, it takes up server resources, for example, if a file has 1 G, it needs about 1 G of memory resources)
jstack (generate a snapshot of the thread at the current moment of the virtual machine dump)
The (Stack Trace for Java) command is used to generate a thread snapshot of the virtual machine at the current moment. Thread snapshot is the collection of method stacks being executed by each thread in the current virtual machine. The main purpose of generating thread snapshot is to locate the cause of the long pause of the thread, such as deadlock between threads, endless loop, and long time caused by requesting external resources. Waiting and so on are common causes of long pauses in threads. In the code, you can use the getAllStackTraces() method of the java.lang.Thread class to obtain the StackTraceElement objects of all threads in the virtual machine. Use this method to pass a few simple lines
The code completes most of the functions of jstack. In the actual project, you may wish to call this method to be an administrator page, and you can use the browser to view the thread stack at any time.
Generally speaking, jstack is mainly used to check whether there is a deadlock situation
Command tool summary
Production server recommended to open
1. -XX:-HeapDumpOnOutOfMemoryError is turned off by default,
It is recommended to enable it and output a dump file when java.lang.OutOfMemoryError occurs.
Record the heap memory snapshot at that time.
2. -XX:HeapDumpPath=./java_pid<pid>.hprof
It is used to set the storage file path of heap memory snapshot. The default is the start location of the java process.
Turn on before tuning, turn off after tuning
3. -XX:+PrintGC
The debug trace only prints simple GC information parameters :
4. -XX:+PrintGCDetails, +XX:+PrintGCTimeStamps
Print detailed GC information
5. -Xlogger:logpath
Set the log path of gc, such as: -Xlogger:log/gc.log, set the path of gc.log to the current directory
Log directory.
Application scenario: Write the gc log independently to the log file, and compare the GC log with the system business log
Separation, convenient for developers to track and analyze.
Consider using
- -XX:+PrintHeapAtGC , print push information
Parameter setting: -XX : +PrintHeapAtGC
Application scenario: Get Heap usage before and after each garbage collection
2. -XX:+TraceClassLoading parameter method: -XX:+TraceClassLoading
Application scenario: See the class loading process and specific class information in the system console information, which can be used to
Analyze the loading order of classes and whether they can be streamlined.
3. -XX:+DisableExplicitGC prohibits explicitly calling System.gc( ) at runtime
Visualization tool
JMX (Java Management Extensions) is a framework for implanting management functions for applications, devices, systems, etc. JMX can flexibly develop seamlessly integrated system, network and service management applications across a series of heterogeneous operating system platforms, system architectures and network transmission protocols.
To manage remote processes, you need to add in the startup parameters of the remote program:
-Djava.rmi.server.hostname=…..
-Dcom.sun.management.jmxremote
-Dcom.sun.management.jmxremote.port=8888
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=false
Jconsole
visualvm
Plug-in center address
https://visualvm.github.io
But pay attention to the version problem. Different JDKs have different visualvm. You need to download the corresponding version when downloading the plug-in.
Generally speaking, this tool is used for local debugging. Generally speaking, you generally can't use it in production (unless remote connection is enabled)