The opening

When locating problems for a system, knowledge and experience are the key basis, data are the basis, and tools are the means of using knowledge to process data. The data in question today includes, but is not limited to, exception stacks, JVM run logs, garbage collection logs, thread snapshots, heap snapshots, and more. How to use analysis tools can improve our efficiency in analyzing data and locating and solving problems. This article reviews common JVM troubleshooting commands and case studies.

Common commands

The JDK provides a series of tools for monitoring and diagnosing Java processes. They are in the JDK installation directory bin. How can we use it to get useful information and analyze system problems and performance bottlenecks? Details below.

JPS: JVM process status tool

We must be used under Linux ps – ef | grep Java command to see a running Java process, and also provides similar in Java command, this command can list the virtual machine is running process, and display the virtual machine to perform the main class name and process ID only.

JPS Help Documentation:

usage: jps [-help]
       jps [-q] [-mlvV] [<hostid>]

Definitions:
    <hostid>:      <hostname>[:<port>]
Copy the code

Execution Cases:

jps -l
1228 sun.tools.jps.Jps
Copy the code

Option parameter description:

  • -q: displays the ID of the local VM process without the class name.
  • -m: Prints the arguments passed to the main() method of the main class at startup.
  • -l: displays the name of the entire class. If the JAR package is executed, the path of the JAR package is displayed.
  • -v: Outputs JVM configuration parameters of the process.

Jstat: JVM statistics monitoring tool

This command displays the RUNNING status of the JVM. You can display runtime data, such as classloading, memory, garbage collection, just-in-time compilation, from local or remote JVM processes.

Jstat help documentation:

invalid argument count
Usage: jstat -help|-options
       jstat -<option> [-t] [-h<lines>] <vmid> [<interval> [<count>]]

Definitions:
  <option>      An option reported by the -options option
  <vmid>        Virtual Machine Identifier. A vmid takes the following form:
                     <lvmid>[@<hostname>[:<port>]]
                Where <lvmid> is the local vm identifier for the target
                Java virtual machine, typically a process id; <hostname> is
                the name of the host running the target Java virtual machine;
                and <port> is the port number for the rmiregistry on the
                target host. See the jvmstat documentation for a more complete
                description of the Virtual Machine Identifier.
  <lines>       Number of samples between header lines.
  <interval>    Sampling interval. The following forms are allowed:
                    <n>["ms"|"s"]
                Where <n> is an integer and the suffix specifies the units as 
                milliseconds("ms") or seconds("s"). The default units are "ms".
  <count>       Number of samples to take before terminating.
  -J<flag>      Pass <flag> directly to the runtime system.
Copy the code

Execution Cases:

Jstat -gc 1441 S0C S1C S0U S1U EC EU OC OU MC MU CCSC CCSU YGC YGCT FGC FGCT GCT 10752.0 10752.0 0.0 0.0 65536.0 9175.5 175104.0 0.0 4480.0 780.7 384.0 76.6 0 0.000 0 0.000 0.000Copy the code

Option parameter description:

  • -class: monitors the number of classes loaded, unloaded, total space, and class loading time.
  • – Compiler: indicates the compiled methods and time used by the real-time compiler.
  • – GC: monitors the Java heap status, including Eden area, two Survivor areas, old age, capacity of permanent generation, used space, and total garbage collection time.
  • – gcCapacity: the output is similar to gc, but focuses on the maximum and minimum space used by each area of the Java heap.
  • – gCCause: performs the same function as gcutil, but outputs the last time the garbage collection occurred.
  • -gcmetacapacity: statistics on metadata space in JDK8.
  • -gcnew: monitors new generation garbage collection information.
  • – gcnewCapacity: similar to gcnew, the maximum and minimum space used by the output.
  • -gcold: Monitor old age garbage collection information.
  • – gcoldCapacity: similar to gcold, the maximum and minimum capacity used by output.
  • -gcutil: Monitors the percentage of the used space in the total space.
  • -printcompilation: Outputs methods that have been compiled in real time.

Jinfo: Java configuration information tool

Can be used to view extended parameters of running Java programs, including Java System properties and JVM command-line parameters; You can also dynamically modify running JVM parameters.

Jinfo Help documentation:

Usage: jinfo [option] <pid> (to connect to running process) jinfo [option] <executable <core> (to connect to a core file) jinfo  [option] [server_id@]<remote server IP or hostname> (to connect to remote debug server) 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 -flags to print VM flags -sysprops to print Java system properties <no option> to print both of the above -h | -help to print this help messageCopy the code

Execution Cases:

jinfo -flags 18378
Attaching to process ID 18378, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.261-b12
Non-default VM flags: -XX:CICompilerCount=3 -XX:InitialHeapSize=262144000 -XX:MaxHeapSize=4164943872 -XX:MaxNewSize=1388314624 -XX:MinHeapDeltaBytes=524288 -XX:NewSize=87031808 -XX:OldSize=175112192 -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseParallelGC 
Copy the code

Option parameter description:

  • No option Displays all parameters and system properties
  • -flag name Displays parameters corresponding to the name
  • – flag [+ | -] open or close the name corresponding to the name of the parameter
  • -flag name=value Sets parameters of the corresponding name
  • -flags Displays all parameters
  • -sysprops Outputs system attributes

Jmap: Java memory mapping tool

The jamp command is used to generate heap dump snapshots. You can also query finalize execution queues, Java heap, and method areas for details, such as space usage, which collector is currently in use, etc.

Jmap Help documentation:

Usage:
    jmap [option] <pid>
        (to connect to running process)
    jmap [option] <executable <core>
        (to connect to a core file)
    jmap [option] [server_id@]<remote server IP or hostname>
        (to connect to remote debug server)

where <option> is one of:
    <none>               to print same info as Solaris pmap
    -heap                to print java heap summary
    -histo[:live]        to print histogram of java object heap; if the "live"
                         suboption is specified, only count live objects
    -clstats             to print class loader statistics
    -finalizerinfo       to print information on objects awaiting finalization
    -dump:<dump-options> to dump java heap in hprof binary format
                         dump-options:
                           live         dump only live objects; if not specified,
                                        all objects in the heap are dumped.
                           format=b     binary format
                           file=<file>  dump heap to <file>
                         Example: jmap -dump:live,format=b,file=heap.bin <pid>
    -F                   force. Use with -dump:<dump-options> <pid> or -histo
                         to force a heap dump or histogram when <pid> does not
                         respond. The "live" suboption is not supported
                         in this mode.
    -h | -help           to print this help message
    -J<flag>             to pass <flag> directly to the runtime system
Copy the code

Execution Cases:

jmap -dump:format=b,file=test.bin 1713
Dumping heap to /Users/handexing/test.bin ...
Heap dump file created
Copy the code

Parameter Description:

  • -dump: generates a Java heap dump snapshot. Format for-dump:[live,]format=b,file=<filename>The live parameter indicates whether only live objects are dumped.
  • -finalization: Displays objects in the F-queue that are waiting for the Finalizer thread to execute the Finalize method.
  • -heap: Displays detailed Java heap information. For example, which collector, parameter configuration, generation, etc. This is valid on Linux.
  • -histo: displays the statistics of objects in the heap, including the number of classes and instances.
  • -permstat: displays permanent memory status based on classloader. This is valid on Linux.
  • -f: If the JVM does not respond to -dump, you can use -f to forcibly generate dump snapshots. This command is effective on Linux.

Jhat: JVM heap dump snapshot analysis tool

This command is used together with the jmap command to analyze heap dump snapshots generated by jmap. Jhat has a mini-HTTP/Web server built into it that generates the results of the analysis and can be viewed in a browser. (Then again, most people don’t use JHat to analyze snapshots directly unless there’s nothing else available.) It is also not recommended to use Jhat on the server because it will degrade the server performance.

Jhat Help documentation:

Usage:  jhat [-stack <bool>] [-refs <bool>] [-port <port>] [-baseline <file>] [-debug <int>] [-version] [-h|-help] <file>

	-J<flag>          Pass <flag> directly to the runtime system. For
			  example, -J-mx512m to use a maximum heap size of 512MB
	-stack false:     Turn off tracking object allocation call stack.
	-refs false:      Turn off tracking of references to objects
	-port <port>:     Set the port for the HTTP server.  Defaults to 7000
	-exclude <file>:  Specify a file that lists data members that should
			  be excluded from the reachableFrom query.
	-baseline <file>: Specify a baseline object dump.  Objects in
			  both heap dumps with the same ID and same class will
			  be marked as not being "new".
	-debug <int>:     Set debug level.
			    0:  No debug output
			    1:  Debug hprof file parsing
			    2:  Debug hprof file parsing, no server
	-version          Report version number
	-h|-help          Print this help and exit
	<file>            The file to read

For a dump file that contains multiple heap dumps,
you may specify which dump in the file
by appending "#<number>" to the file name, i.e. "foo.hprof#3".

All boolean options default to "true"
Copy the code

Execution Cases:

jhat test.bin  
Reading from dump.bin...
Dump file created Sat Feb 8 21:54:58 CST 2021
Snapshot read, resolving...
Resolving 271309 objects...
Chasing references, expect 54 dots......................................................
Eliminating duplicate references......................................................
Snapshot resolved.
Started HTTP server on port 7000
Server is ready.
Copy the code

To view the analysis results, visit localhost:7000.

Jstack: Java stack tracing tool

Used to generate a snapshot of the JVM’s thread at the time. A thread snapshot is a stack of methods being executed by each thread in the current JVM. The snapshot is used to locate the cause of a thread’s long pause, such as deadlock, infinite loop, and long suspension caused by resource requests. You can check what is going on without the corresponding thread.

Jstack help documentation:

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)

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
Copy the code

Execution Cases:

Jstack -L 1713 2021-02-08 20:16:16 Full Thread Dump Java HotSpot(TM) 64-bit Server VM (25.261-B12 mixed mode): "DestroyJavaVM" #53 prio=5 os_prio=31 tid=0x00007fa898808800 nid=0x1103 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE Locked ownable synchronizers: - None "http-nio-8080-AsyncTimeout" #51 daemon prio=5 os_prio=31 tid=0x00007fa898a4a000 nid=0x7903 waiting on condition [0x0000700007b0b000] java.lang.Thread.State: TIMED_WAITING (sleeping) at java.lang.Thread.sleep(Native Method) at org.apache.coyote.AbstractProtocol$AsyncTimeout.run(AbstractProtocol.java:1133) at java.lang.Thread.run(Thread.java:748)  Locked ownable synchronizers: - NoneCopy the code

Option parameter description:

  • -f: forces the output stack when normal input is not responded.
  • -m: Displays the C/C++ stack if local methods are called.
  • -l: Displays additional information about the lock in addition to the stack.

Case description

OOM analysis

Java code examples:

/** * @author handx * @version 1.0.0 * @classname heap.java * @description TODO * @createTime 2021 02 08 20:47:00 */ public class HeapOOM { private static class OOMObject{} public static void main(String[] args) { ArrayList<OOMObject> oomObjects = new ArrayList<OOMObject>(); while (true){ oomObjects.add(new OOMObject()); }}}Copy the code

VM Opetion configuration:

-Xms20m -Xmx20m -XX:+HeapDumpOnOutOfMemoryError  -XX:HeapDumpPath=./
Copy the code

Or you can pull a heap dump using the jmap command.

Printed after program execution:

java.lang.OutOfMemoryError: Java heap space Dumping heap to ./java_pid1824.hprof ... Heap dump file created [27809333 bytes in 0.075secs]Copy the code

Import dump files using JVisualVM analysis (a tool that comes with the JDK) :

Open theclassTAB, you can see that the first one is the object we created ourselves:

You can also view thread information on the heap dump:

This is basically a place where you can see which line of code is broken, and it’s time to fix it.

Write in the last

This article briefly introduces the common commands used in the JVM. If you want to be proficient in using them, you should also use them frequently in daily development and debugging. Next time, we will review how to analyze GC logs. I hope this article will help you. Like to add attention, continue to update the follow-up!!