Copyright belongs to the author, any form of reprint please contact the author to obtain authorization and indicate the source.

JVM parameter types

Standard parameters

Basically unchanged, relatively stable

  • -help
  • – server and client
  • – version, – showversion
  • Cp, — the classpath

Nonstandardized parameter

There are changes in some JVMS, but the changes are small

  • -Xint: Explains execution
  • -xcomp: compiles native code the first time it is used
  • -xmixed: In mixed mode, the JVM decides whether to compile native code or not

XX parameters

XX parameters are non-standardized parameters, relatively unstable, mainly used for JVM tuning and Debug, and can be divided into two categories:

  • Boolean Type format: -xx :[+-] Indicates that (+) enables or disables the (-) name attribute. For example, -xx :+UseConMarkSweepGC -xx :UseG1GC
  • Non-boolean Format: -xx: = Indicates that the value of name is value. For example, -xx :MaxGCPauseMillis = 200 -xx :GCTimeRatio = 19

-xmx (maximum JVM memory) -xms (minimum JVM memory) It is not an X parameter, Instead, the XX parameter -xms is equivalent to -xx :InitialHeapSize -xmx is equivalent to -xx :MaxHeapSize

How do I view JVM runtime parameters

  • -xx :+PrintFlagsInitial To view an initial value
  • -xx :+PrintFlagsFinal To view a final value
  • – XX: + UnlockExperimentalVMOptions unlock experimental parameters
  • – XX: + UnlockDiagnosticVMOptions unlock diagnostic parameters
  • -xx :+PrintCommandLineFlags Prints command line parameters

Example: Java -xx :+PrintFlagsFinal -version

= indicates the default value



:= Value modified by the user or JVM

jps

The JPS command is similar to the Linux ps command, but it only lists all the Java applications on the system. You can use the JPS command to view information about Java processes, such as startup classes, incoming parameters, and Java VIRTUAL machine parameters. Refer to JPS, the JVM performance tuning tool

jinfo

View the value of a running parameter for a JVM

Jinfo example

  • Viewing the maximum memoryJinfo-flag MaxHeapSize [Process ID]
  • Check the garbage collectorJinfo - flag UseConcMarkSweepGC UseG1GC/UseParallelGC (process ID)

Use jstat to view JVM statistics

JDK Tools and Utilities

Class loading information

Options: -class, -compiler, -GC, -printcompilation

Garbage collection Information

Options: -gc, -gcutil, -gCCause, -gcnew, -gcold

  • S0C, S1C, and S0U: indicates the total quantity and usage of S0 and S1
  • EC and EU: total quantity and usage in Eden
  • OC and OU: total volume and usage of the Old area
  • MC and MU: total Metaspace area and usage
  • CCSC and CCSU: indicates the total amount and usage of compressed class space
  • YGC, YGCT: YoungGC times and time
  • Number and time of FGC and FGCT: FullGC
  • GCT: Total GC time

JIT compilation information

Options: -compiler, -printcompilation

Jmap +MAT analyzes memory overflow

Example test project based on Spring Boot fast build user.java

public class User{
    private int id;
    private String name; 
    # construct method
    # get() and set()
}
Copy the code

MemoryController.java

@RestController public class MemoryController{ private List<User> userList = new ArrayList<User>(); /** * set the heap size (-xmx32m -xms32m) **/ @getMapping ("/heap")     ## Heap-based memory overflow
    public String heap(){
        int i = 0;
        while(true){ userList.add(new User(i++, UUID.randomUUID().toString())); }} /** * Set the maximum memory size (-xx :MetaspaceSize=32M -XX:MaxMetaspaceSize=32M) **/ @getMapping ("/noheap")   ## Non-heap memory overflow
    public String noheap() {while(true) {## Based on dynamically generated class testsclassList.addAll(Metaspace.createClasses()); }}}Copy the code

Metaspace.java

import java.util.ArrayList; import java.util.List; import org.objectweb.asm.ClassWriter; import org.objectweb.asm.MethodVisitor; import org.objectweb.asm.Opcodes; Public class Metaspace extends ClassLoader {/* * Public class Metaspace extends ClassLoader {/* * public class Metaspace extends ClassLoader# # classList<Class<? >> classes = new ArrayList<Class<? > > ();## Loop 1000W times to generate 1000W different classes.
    for (int i = 0; i < 10000000; ++i) {
        ClassWriter cw = new ClassWriter(0);
        ## define a Class named Class{I} with public access domain, java.lang.Object parent Class, and no interface
        cw.visit(Opcodes.V1_1, Opcodes.ACC_PUBLIC, "Class" + i, null, "java/lang/Object", null);
        Define the constructor 
      
        method
      
        MethodVisitor mw = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>"."()V", null, null);
        The first instruction is to load this
        mw.visitVarInsn(Opcodes.ALOAD, 0);
        The second instruction calls the constructor of the parent Object class
        mw.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object"."<init>"."()V".false);
        The third instruction is return
        mw.visitInsn(Opcodes.RETURN);
        mw.visitMaxs(1, 1);
        mw.visitEnd();

        Metaspace test = new Metaspace();
        byte[] code = cw.toByteArray();
        # # define the classClass<? > exampleClass = test.defineClass("Class"+ i, code, 0, code.length); classes.add(exampleClass); }}Copy the code

Access path Localhost :8080/heap Memory overflow diagram

localhost:8080/noheap

Export the application memory image file

There are two ways to export data: automatic export of memory overflow and manual export of memory overflow

  • Automatic memory overflow export – using the following JVM parameter options
-XX:+HeapDumpOnOutOfMemoryError
-XX:HeapDumpPath=./     The path to the file to export
Copy the code

  • Manually export some options using the jmap command:-heap, -clstats, -dump:, -F More can be found here

    Large data may fail to be exported.

    Example below:

Use MAT to analyze and locate errors

MAT installation and use tutorial memory analysis tool MAT use advanced MAT

The JStack field thread is abnormal

Java stack trace – Prints a stack trace for a thread for a given process or core file or remote debug server.

options instructions
-F Force a stack dump when jStack [-L] PID does not respond.
-l Print additional information about the lock, such as a list of available java.util.Concurrent synchronizers that are owned
-m Prints a mixed mode stack trace with Java and native C/C ++ frames.
-H Print help information.
-help Print help information.

The status of a Java thread

The following table lists possible thread states for a thread dump using the Control + Break Handler.

Thread state describe
NEW The topic has not yet begun.
RUNNABLE The thread is executing in the JVM.
BLOCKED The thread is blocked waiting for the monitor to lock.
WAITING A thread waits indefinitely for another thread to perform a particular operation.
TIMED_WAITING A thread is waiting for another thread to perform an operation for a maximum of specified wait time.
TERMINATED The thread has exited.

Thread state flow diagram

Instance -CPU spikes

CpuController.java

import java.util.ArrayList; import java.util.List; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @restController public class CpuController {/** ** / @requestMapping ("/loop")
	public List<Long> loop(){
		String data = "{\"data\":[{\"partnerid\":]";
		returngetPartneridsFromJson(data); } private Object lock1 = new Object(); private Object lock2 = new Object(); /** ** */ @requestMapping ("/deadlock")
	public String deadlock(){ new Thread(()->{ synchronized(lock1) { try {Thread.sleep(1000); }catch(Exception e) {} synchronized(lock2) { System.out.println("Thread1 over"); } } }) .start(); new Thread(()->{ synchronized(lock2) { try {Thread.sleep(1000); }catch(Exception e) {} synchronized(lock1) { System.out.println("Thread2 over");
				}
			}
		}) .start();
		return "deadlock";
	}
	public static List<Long> getPartneridsFromJson(String data){  
	    ##{\"data\":[{\"partnerid\":982,\"count\":\"10000\",\"cityid\":\"11\"},{\"partnerid\":983,\"count\":\"10000\",\"cityid\" :\"11\"},{\"partnerid\":984,\"count\":\"10000\",\"cityid\":\"11\"}]}
	    ## Above is normal data
	    List<Long> list = new ArrayList<Long>(2);  
	    if(data == null || data.length() <= 0){  
	        return list;  
	    }      
	    int datapos = data.indexOf("data");  
	    if(datapos < 0){  
	        return list;  
	    }  
	    int leftBracket = data.indexOf("[",datapos);  
	    int rightBracket= data.indexOf("]",datapos);  
	    if(leftBracket < 0 || rightBracket < 0){  
	        return list;  
	    }  
	    String partners = data.substring(leftBracket+1,rightBracket);  
	    if(partners == null || partners.length() <= 0){  
	        return list;  
	    }  
	    while(partners! =null && partners.length() > 0){ int idpos = partners.indexOf("partnerid");  
	        if(idpos < 0){  
	            break;  
	        }  
	        int colonpos = partners.indexOf(":",idpos);  
	        int commapos = partners.indexOf(",",idpos);  
	        if(colonpos < 0 || commapos < 0){  
	            //partners = partners.substring(idpos+"partnerid".length()); This part of the code raises a problem
	            continue;
	        }  
	        String pid = partners.substring(colonpos+1,commapos);  
	        if(pid == null || pid.length() <= 0){  
	            //partners = partners.substring(idpos+"partnerid".length()); This part of the code raises a problem
	            continue;
	        }  
	        try{  
	            list.add(Long.parseLong(pid));  
	        }catch(Exception e){  
	            //do nothing  
	        }  
	        partners = partners.substring(commapos);  
	    }  
	    returnlist; }}Copy the code

The top command is used to query Linux cpus

The export file
Jstack [process ID] > [fileName]






Jstack and thread dump analysis



Java operation and maintenance JStack dump log file details

Output top -p [process ID] -h for all threads

Visual monitoring based on JVisualVM

Monitoring local Tomcat

View deadlocks, loop

View the execution time of the hotspot method

Select the address of the plug-in center for the JDK version

Monitor remote Tomcat

Monitor normal Java processes