The JVM debugging

JVM debugging -JDK tools

Check whether you are familiar with the commands, parameters, and application scenarios of debugging and locating tools.

Familiar with IDE (IntelliJ/Eclipse) functional view, local/remote debugging methods.

Understand the Junit framework structure, jMock/MockJ.

JvisualVM & JMC graphics tools and implicitly integrated tools, such as jStack, JMap, jstat, jinfo commands, parameters, and usage scenarios.

Prerequisites: Understand the basic structure of the JVM, garbage collection, ClassLoader.

jps

JPS -l Prints the full path to the main function. JPS -v displays the parameters passed to the JVM, such as -xmx256m

jstat

A tool for viewing heap information

jsat -<option> [-t] [-h<lines>] <vmid> [<interval> [<count>]]

option:

  • Class Classloader information
  • Compiler JIT compiler related
  • The gc gc related
  • Gccapacity Capacity and usage of the young band and the old generation
  • Gccause GC information, and the cause of the latest GC
  • Gcold old age, permanent generation information
  • Gcoldcapacity Specifies the old age capacity
  • Gcpermcapacity Capacity of the permanent generation
  • Gcutil GC information
  • Printcompilation prints JIT compilation information
  • -t Displays the running time
  • -h Outputs the header after the number of lines
  • Interval Indicates the statistical period, in milliseconds
  • Count how many times to output information

jinfo

Jinfo

  • -flag Displays specified JVM parameter values
  • – flag [+ | -] setting specifies the JVM Boolean value
  • -flag= Sets the values of the parameters of the specified JVM

    jinfo -flag MaxTenuringThreshold 2972View 2972 PID Maximum age for new generation to be promoted to old age

jmap

Generate heap dump, view heap object instance statistics, and view Classloader and Finalizer

Jmap use

option

  • No option: Displays the memory image information of the process, similar to the Solaris pmap command.
  • Heap: Displays Java heap details
  • Histo [:live] : Displays statistics of objects in the heap
  • Clstats: Prints classloader information
  • Finalizerinfo: Displays the objects in the F-Queue that are waiting for the Finalizer thread to execute the Finalizer method
  • Dump: : Generates a heap dump snapshotjmap -dump:format=b,file=heapdump.phrof pid
  • F: When -dump does not respond, use -dump or -histo. In this mode, the live subparameter is invalid.
  • Help: Prints the help information
  • J: Specifies the parameters passed to the JVM running JMap

jhat

To analyze heap snapshots, the official recommendation is to use Visual VM instead of analyzing the jmap exported hprof jhat C :\heap.hprof visit http://127.0.0.1:7000

jstack

Export the Java program thread stack, view of deadlock good tool www.jianshu.com/p/025cb069c…

jstack [-options] <pid>

  • -l Displays additional information about locks, such as the Ownable synchronizers list belonging to Java.util.Concurrent.

  • -f Displays stack information forcibly if ‘jstack [-l] pid’ does not correspond

  • -m Prints all stack information for Java and Native C/C ++ frameworks.

  • Pid – h | -help print help information need to be printed configuration information of the Java process id, you can use the JPS queries.

jstatd

Collect remote host information. Use this command together with other commands

VisualVM uses JStatd to remotely monitor the JVM

jcmd

Multifunctional tools, JDK1.7+ launched, execute commands for specific JVMS, can replace JMAP, can export heap, view Java processes, export thread information, GC.

JCMD, the JVM performance tuning tool

jcmd JCMD -l JCMD – h – pid: receive diagnosis process ID command request.

  • Main Class: The main class of the process that receives diagnostic command requests. When a process is matched, any process that contains the specified substring in the name of the main class is matched. If multiple running Java processes share the same Main class, diagnostic command requests are sent to all of them.

  • Command: Main class for the process that receives diagnostic command requests. When a process is matched, any process that contains the specified substring in the name of the main class is matched. If multiple running Java processes share the same Main class, diagnostic command requests are sent to all of them.

  • Note: If any arguments contain Spaces, you must enclose them with single or double quotation marks in English. In addition, you must use escape characters to transfer single or double quotes in arguments to prevent the operating system shell from handling these reference marks. Of course, you can also surround arguments with single quotes and then use double quotes within arguments (or, surround arguments with double quotes and use single quotes within arguments).

  • Perfcounter.print: Prints the performance counters available on the target Java process. The list of performance counters may change from Java process to Java process.

  • -f file: reads commands from file and invokes them on the target Java process. In file, each command must be written on a separate line. Lines starting with “#” are ignored. When all lines of command are invoked or a command containing the stop keyword is read, file processing is terminated.

  • -l: Displays the list of all processes.

  • -h: Views the help information. (with help)

Lists the actions that can be performed by the currently running Java process

C:\Windows\system32>jcmd 9592 help  
9592:  
The following commands are available:  
JFR.stop  
JFR.start  
JFR.dump  
JFR.check  
VM.native_memory  
VM.check_commercial_features  
VM.unlock_commercial_features  
ManagementAgent.stop  
ManagementAgent.start_local  
ManagementAgent.start  
GC.rotate_log  
Thread.print  
GC.class_stats  
GC.class_histogram  
GC.heap_dump  
GC.run_finalization  
GC.run  
VM.uptime  
VM.flags  
VM.system_properties  
VM.command_line  
VM.version  
help  
Copy the code

hprof

Used to monitor CPU and heap information for Java programs

Java-agentlib :hprof=help View the command

Introduction to Visual VM

Use VisualVM for performance analysis and tuning

Use more about JRockit over Mission Control for performance analysis and tuning yq.aliyun.com/articles/46…

JVM debugging – Common parameters

JVM heap configuration [very important]

-xms Initial heap space -xmx Maximum heap space can be set directly -xms = -xmx, can reduce the number of program GC, improve performance

-Xmn setting the new generation will affect the old age. You are advised to set it to 1/3 or 1/4 of the heap space

-xx: “SurvivorRatio” = Eden/FROM = Eden /to Example: -XMX20M-XMS20M-xMN1M-XX :SurvivorRatio= 2-XX :+PrintGCDetails Eden -from-to=2-1-1, new generation size = 512KB + 256KB + 256KB = 1MB

Basic strategy: try to keep objects in the new generation and reduce the number of GC in the old age

-xx :NewRatio New generation/old age

– XX: + HeapDumpOnOutOfMemoryError (OOM) is derived when memory heap information – XX: HeapDumpPath export heap memory specified path, fit the last

Example: – Xmx20m – Xms5m – XX: HeapDumpOnOutOfMemoryError – XX: HeapDumpPath = D: / my files/aaa. Dump system OOM, Export heap information to aaA.dump and view it using tools such as Visual VM

Non-heap memory configuration

-xx :PermSize -xx :MaxPermSize JDK1.7-, set the permanent area size

-xx :MaxMetaspaceSize JDK1.8+. The metadata area is limited only by the available memory of the system. You can specify the maximum available value of the permanent area

-Xss Specifies the stack size of the thread

-xx :MaxDirectMemorySize Maximum available direct memory similar to NIO direct memory, accessing the native heap space. If not set, the default is -xmx. GC is triggered when -xx :MaxDirectMemorySize is reached. If not, it will still be OOM.

Direct memory is suitable for scenarios with few requests and frequent access.

The GC log

-xx :+PrintGC Prints GC logs -xx :+PringGCDetails -xlog: GC JDK9,10+, prints GC logs

– XX: + PrintGCApplicationConcurrentTime GC will cause program pause, you can look at execution time

– XX: + PrintGCApplicatioStoppedTime GC will cause program pause, can view the pause time

-xx :+PrintReferenceGC views weak references, soft references, virtual references, Finalize queues

Class load, unload

-xx :+TraceClassLoading JDK8 -xlog :class+load=info JDK8,10+ TraceClassLoading, unloading

-xx :+PrintClassHistoram Added at system startup to view and print the distribution of classes at run time

-xx :+PrintVMOptions Prints explicit command-line arguments received by the JVM. -xx :+PrintCommandLineFlags prints explicit and implicit arguments passed to the JVM, which may be set when the JVM starts

-xx :+PrintFlagsFinal Prints all system parameters