This is the 15th day of my participation in the August More text Challenge. For details, see: August More Text Challenge

Common problems in production are as follows:

  • CPU surge causes system unavailability or TPS reduction;
  • Minor Indicates that the number of GC sessions is excessive.
  • Full Too many GC times;
  • Full The GC time is too long.
  • Memory leak, memory overflow;
  • A deadlock, etc.;

The proper use of virtual machine troubleshooting and analysis tools can improve the efficiency of analyzing data, locating and resolving problems.

jps

View vm processes.

jmp

Used to view memory information and generate snapshots of heap storage (commonly known as headdump or dump files).

View the number of instances and memory usage

Jmap-histo 1695 >./jmp. TXT to print the program memory information used in the demonstration into the jmp. TXT file.

  • Num: serial number
  • Instances: indicates the number of Instances
  • Bytes: indicates the occupied space
  • [C is a char[], [S is a short[], [I is a int[], [B is a byte[], [[I is a int[][]

Viewing heap Information

JHSDB jmap –heap — PID 1695 >./jmap_heap. TXT to view heap information, print it to jmap_heap.

The first is Heap Configuration, or Heap Configuration information:

  • MinHeapFreeRatio: the minimum idle heap ratio of the JVM. The corresponding JVM parameter is-XX:MinHeapFreeRatio.
  • MaxHeapFreeRatio: indicates the minimum heap idle ratio of the JVM. The corresponding JVM parameter is-XX:MaxHeapFreeRatio.
  • MaxHeapSize: Maximum heap size. The corresponding JVM parameter is-XX:MaxHeapSize.
  • NewSize: the default size for the ‘new generation’ of the heap. The corresponding JVM parameter is-XX:NewSize.
  • MaxNewSize: The maximum size of the heap’s ‘new generation’. The corresponding JVM parameter is-XX:MaxNewSize.
  • OldSize: The size of the ‘old generation’ of the heap. The corresponding JVM parameter is-XX:OldSize.
  • NewRatio: The ratio of size between ‘Cenozoic’ and ‘old’. The corresponding JVM parameter is-XX:NewRatio.
  • SurvivorRatio: size ratio of Eden and Survivor in the young generation. The corresponding JVM parameter is-XX:SurvivorRatio.
  • SurvivorRatio: size ratio of Eden and Survivor in the young generation. The corresponding JVM parameter is-XX:SurvivorRatio.
  • MetaspaceSize: The default size of the meta space. The corresponding JVM parameter is-XX:MetaspaceSize.
  • CompressedClassSpaceSize: The default size of the class pointer compression space. The corresponding JVM parameter is-XX:CompressedClassSpaceSize.
  • MaxMetaspaceSize: The maximum size of the meta space. The corresponding JVM parameter is-XX:MaxMetaspaceSize.
  • G1HeapRegionSize: The size of the heap to be divided (that is, the size of each region) when the G1 garbage collector is used. The corresponding JVM parameter is-XX:G1HeapRegionSize.

Next comes Heap Usage, which includes the memory partition of the G1 collector, the allocation of the new generation, Eden, and Survivor regions, and the allocation of the old generation regions.

Exporting dump Files

Run jmap-dump :format=b,file=javagoing.hprof 1695 to export the dump file.

You can use the JVisualVM tool to analyze the dump file.

jstack

Java stack trace tool. This is used to generate a snapshot of a thread (commonly known as a threaddump or Javacore) of the virtual machine at the current time. A thread snapshot is a stack of all methods that are executing in the current VIRTUAL machine. The purpose of a thread snapshot is to locate the causes of long pauses, such as deadlocks between threads, deadloops, or long hangs due to requests for external resources.

Deadlock detection

A simple deadlock example:

public class DeadLock {

    private static Object lock1 = new Object();
    private static Object lock2 = new Object();

    public static void main(String[] args) {
        new Thread(() -> {
            synchronized (lock1) {
                System.out.println("thread1 begin");
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (lock2) {
                    System.out.println("thread1 end");
                }
            }

        }).start();


        new Thread(() -> {
            synchronized (lock2) {
                System.out.println("thread2 begin");
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (lock1) {
                    System.out.println("thread2 end"); } } }).start(); }}Copy the code

Start the program, find the deadlock state, use JPS command to view the process ID, and then use jStack + process ID to view the deadlock information. For example, jstack 2862>deadlock. TXT, the following information is generated:

Deadlocks can also be detected automatically using JVisualVM:

Find the stack information with the highest CPU

  1. Using the commandtop -p pidPid is your Java process id, such as 2862.
  2. Press H to get the memory of each thread.
  3. Find the thread TID with the highest memory and CPU usage, such as 2862.
  4. Converting to hexadecimal gives b2E, which is the hexadecimal representation of the thread ID.
  5. Perform jstack4977 | grep – b2e, get 1371 this thread in the thread stack information in line 10 lines.
  6. Look at the corresponding stack information to find the code that may have a problem.

jnfo

Java Configuration information Tool. View the extension parameters of the running Java program.

Viewing JVM parameters

Use jinfo-flags PID, as shown below:

View Java system parameters

Run the jinfo-sysprops pid command.

jstat

Vm information monitoring tool. Monitors vm running status information, including class loading, memory, garbage collection, and just-in-time compilation data in local or remote VM processes.

Garbage Collection Statistics

Use the jstat -gc pid command to view gc information, which can be used to evaluate the overall gc pressure and program memory usage.

  • S0C: Size of the first survival region
  • S1C: Size of the second survival zone
  • S0U: The use size of the first survival area
  • S1U: Usage size of the second survival area
  • EC: The size of Eden Park
  • EU: the use size of Eden Park
  • OC: Old age size
  • OU: Size used in old age
  • MC: Method area size (meta space)
  • MU: Method area usage size
  • CCSC: Compression class space size
  • CCSU: compressed class space usage size
  • YGC: Number of young generation garbage collection times
  • YGCT: waste collection time of young generation (unit: s)
  • FGC: Number of garbage collection times in the old era
  • FGCT: time spent in garbage collection in the old era, in s
  • GCT: total garbage collection time, in seconds

Heap memory statistics

Run the jstat -gccapacity pid command to view the heap memory statistics.

  • NGCMN: minimum capacity of the New generation
  • NGCMX: maximum capacity of the new generation
  • NGC: current generation capacity
  • S0C: The size of the first survival region
  • S1C: Size of the second survival zone
  • EC: The size of Eden Park
  • OGCMN: Minimum capacity in the old age
  • OGCMX: Maximum capacity in the old age
  • OGC: Current old age size
  • OC: current old age size
  • MCMN: indicates the minimum metadata capacity
  • MCMX: indicates the maximum metadata capacity
  • MC: Current metadata space size
  • CCSMN: the minimum size of the compressed class space
  • CCSMX: Maximum compression class space size
  • CCSC: current compressed class space size
  • YGC: Number of young generation GC
  • FGC: Old age
  • The GC number

New generation of garbage collection statistics

jstat -gcnew pid

  • S0C: Size of the first survival region
  • S1C: Size of the second survival zone
  • S0U: The use size of the first survival area
  • S1U: Usage size of the second survival area
  • TT: Indicates the number of times an object has survived in the Cenozoic
  • MTT: Indicates the maximum number of times an object can survive in the Cenozoic
  • DSS: Expected survival size
  • EC: The size of Eden Park
  • EU: the use size of Eden Park
  • YGC: Number of young generation garbage collection times
  • YGCT: Young generation waste collection time

New Generation Memory Statistics

jstat -gcnewcapacity pid

  • NGCMN: minimum capacity of the New generation
  • NGCMX: maximum capacity of the new generation
  • NGC: current generation capacity
  • S0CMX: maximum surviving block 1 size
  • S0C: indicates the size of the current surviving zone 1
  • S1CMX: Maximum surviving zone 2 size
  • S1C: indicates the size of the surviving region 2
  • ECMX: maximum Eden park size
  • EC: Current size of Eden Park
  • YGC: Number of young generation garbage collection times
  • FGC: Number of times to recycle in old age

You can also view old age garbage collection, memory statistics, and meta-space memory statistics, which I will not demonstrate. With this data in hand, you can use the optimization ideas described earlier to set some initial JVM parameters for your system, such as heap size, young generation size, ratio of Eden to Survivor, old age size, threshold for large objects, and threshold for older objects to enter old age.

conclusion

These are some of the most important tools that the JVM provides to help you solve your problems.