Note source: Silicon Valley JVM complete tutorial, millions of playback, the peak of the entire network (Song Hongkang details Java virtual machine)

Update: gitee.com/vectorx/NOT…

Codechina.csdn.net/qq_35925558…

Github.com/uxiahnan/NO…

[TOC]

1. An overview of the article

1.1. Big factory interview questions

Pay treasure:

Three aspects of Alipay: What does JVM performance tuning do?

Millet:

Have you done any JVM memory optimizations?

From SQL, JVM, architecture, database four aspects of optimization ideas

Ant Financial:

Compilation optimization for the JVM

What does JVM performance tuning do

Which JVM diagnostic tuning tools have been used?

How should THE JVM be tuned? How much heap memory and stack space should be set

Three aspects: Which jVM-RELATED analysis tools have been used? What are the specific performance tuning steps

Ali:

How do I tune the JVM? What are the methods?

How to understand memory leaks? What can cause a memory leak? How to solve it?

Bytedance:

Three aspects: How to tune the JVM, how to tune the parameters?

Spelling is great:

From SQL, JVM, architecture, database four aspects of optimization ideas

Jingdong:

Which JVM diagnostic tuning tools have been used?

Why does GC occur frequently in a system with hundreds of thousands of concurrent seconds per second?

How do million-per-day transactions optimize the JVM?

How to monitor, locate and resolve the OOM of online production system?

How can high-concurrency systems optimize performance based on the G1 garbage collector?

1.2. Background

Problems in the production environment

  • What can I do if memory overflow occurs in the production environment?
  • How much memory should the production environment allocate to the server?
  • How can you tune the performance of the garbage collector?
  • How can I handle the CPU load surge in the production environment?
  • How many threads should the production environment allocate to the application?
  • How do I determine if a line of code was executed without logging?
  • How can I view the input return value of a method in real time without adding log?

Why tune

  • Prevent OOM from appearing
  • To solve the OOM
  • Reduce the frequency of Full GC occurrences

Different stages of consideration

  • Before the launch
  • Project operation stage
  • OOM appears online

1.3. Tuning Overview

Basis for monitoring

  • Run log
  • The exception stack
  • The GC log
  • Thread snapshot
  • Heap dump snapshot

General direction for tuning

  • Code wisely
  • Use hardware resources adequately and rationally
  • Tune the JVM properly

1.4. Steps for performance optimization

Step 1: Performance monitoring

  • The GC is frequent
  • CPU load is too high
  • OOM
  • Memory leaks
  • A deadlock
  • The program response time is long

Step 2: Performance analysis

  • Print GC logs and use GCviewer or gceasy. IO to analyze exception information
  • Flexible use of command line tools, jStack, Jmap, jInfo, etc
  • Dump the heap file and use the memory analysis tool to analyze the file
  • Use Aliarthas, JConsole, and JVisualVM to view JVM status in real time
  • Jstack Displays stack information

Step 3: Performance tuning

  • Increase memory appropriately and choose the garbage collector according to the business context
  • Optimize code to control memory usage
  • Increase the machine and distribute the node pressure
  • Set the appropriate number of thread pool threads
  • Use middleware to improve application efficiency, such as caching, message queuing, and so on
  • Other……

1.5. Performance evaluation/test indicators

Pause time (or response time)

The time used between submitting a request and the response that returns it is generally concerned with the average response time. A list of response times for common operations:

operation The response time
Open a site A few seconds
Database queries a record (indexed) More than ten milliseconds
The mechanical disk is addressed once 4 ms
Procedure Read 1 MB data sequentially from a mechanical disk Two milliseconds
1M data is read from SSDS in sequence 0.3 milliseconds
Switch from remote distributed to Redis reading a data 0.5 milliseconds
Read 1 MB data from the memory More than a dozen subtle
Java program local method calls A few subtle
2Kb data is transmitted over the network A subtle

In the garbage collection process:

  • Pause time: The amount of time a program’s worker thread is suspended while garbage collection is being performed.
  • -XX:MaxGCPauseMillis

throughput

  • A measure of the amount of work (requests) completed per unit of time
  • In GC: events that run user code as a percentage of total elapsed time (total elapsed time: program elapsed time + memory reclaimed time)
  • The throughput is 1-1/(1+n), where -xx ::GCTimeRatio=n

concurrency

  • The number of requests actually interacting with the server at any one time

Memory footprint

  • The size of memory occupied by the Java heap area

The relationship between them

Take highway traffic conditions as an example

  • Throughput: data on the number of vehicles passing through a freeway toll booth each day
  • Concurrency: The number of vehicles being driven on a highway
  • Response time: speed

2. JVM Monitoring and diagnostic tools – Command line section

Summary of 2.1.

Performance diagnosis is a problem that software engineers often face and solve in their daily work. In today’s user-experience oriented world, solving performance problems of applications can bring great benefits.

As one of the most popular programming languages, Java application performance diagnosis has been widely concerned in the industry. There are many factors that can cause performance problems in Java applications, such as thread control, disk reads and writes, database access, network I/O, garbage collection, and so on. To locate these problems, a good performance diagnostic tool is essential.

Lesson 1: Use data to explain problems, use knowledge to analyze problems, use tools to deal with problems.

Experience 2: No monitoring, no tuning!

Simple command line tools

In our first contact with Java learning, we must be the first to understand the two commands are javac, Java, so in addition, there are other commands for us to use?

We went to the bin directory where we installed the JDK and found a series of helper tools. These accessibility tools are used to capture different aspects and levels of information about the target JVM to help developers troubleshoot Java applications.

The official source address: hg.openjdk.java.net/jdk/jdk11/f…

2.2. JPS: View the Running Java process

Java Process Status (JPS) : displays all HotSpot VIRTUAL machine processes in a specified system. You can query the running virtual machine processes.

Note: For a local VM process, the local VM ID of the process is unique and the same as the PROCESS ID of the OPERATING system.

The syntax is: JPS [options] [hostid]

We can also print additional information by appending parameters.

The options parameter

  • -q: Only LVMID (Local Virtual Machine ID) is displayed. Do not display the name of the main class, etc
  • -l: prints the full class name of the application’s main class or the full jar path if the process executes a JAR package
  • -m: Outputs the parameters passed to the main() class when the vm process starts
  • -v: lists THE JVM parameters for starting vm processes. For example, -xMS20m -XMx50m is the JVM parameter specified by the startup program.

Note: The above parameters can be used comprehensively.

Note: If a Java process turns off the default UsePerfData parameter (that is, using -xx: -useperfData), the JPS command (and jstat below) will not detect the Java process.

Hostid parameters

The host name registered in the RMI registry. If you want to remotely monitor Java programs on your host, you need to install JStatd.

For network venues with more stringent security practices, it is possible to use a custom policy file to display access to a specific trusted host or network, although this technique is vulnerable to IP address fraud attacks.

If security issues cannot be handled using a custom policy file, the safest course of action is not to run the JStatd server, but to use the Jstat and JPS tools locally.

2.3. Jstat: View JVM statistics

Jstat (JVM Statistics Monitoring Tool) : a command line Tool used to monitor vm running status information. It can display classloading, memory, garbage collection, JIT compilation, and other running data in local or remote virtual machine processes. On servers with no GUI graphical interface and only a plain text console environment, it will be the preferred tool for locating virtual machine performance issues at runtime. Often used to detect garbage collection problems and memory leaks.

The official document: docs.oracle.com/javase/8/do…

Use basic grammar is: the jstat – < option > [-t] [-h < lines >] < vmid > [< interval > / < count >]]

View the command parameters: jstat-h or jstat-help

Where vmID is the process ID, that is, the first number seen after JPS, as follows:

The option parameter

The option can be composed of the following values.

Class loading related:

  • -class: displays ClassLoader information, such as the loading and unloading quantity of classes, total space, and time consumed by class loading

Garbage collection related:

  • -gc: displays heap information related to GC. Including Eden area, two Survivor areas, old age, permanent generation capacity, used space, GC time total and other information.
  • -gcCapacity: displays the same content as -gc, but the output focuses on the maximum and minimum space used by each area of the Java heap.
  • -gcutil: Displays basically the same content as -gc, but the output focuses on the percentage of the total space used.
  • -gCCause: Has the same function as -gcutil, but outputs an additional cause of the last or currently occurring GC.
  • – gcNew: displays the GC status of the new generation
  • -gcnewCapacity: displays the same content as -gcnew. The output focuses on the maximum and minimum space used
  • -geold: displays the GC status of the old age
  • -gcoldCapacity: displays the same content as -gcold. The output focuses on the maximum and minimum space used
  • – gcpermCapacity: displays the maximum and minimum capacity used by the permanent generation.

JIT related:

  • – Compiler: Displays the compiled methods and time used by the JIT compiler

  • -printcompilation: Outputs methods that have been JIT compiled

jstat -class

jstat -compiler

jstat -printcompilation

jstat -gc

jstat -gccapacity

jstat -gcutil

jstat -gccause

jstat -gcnew

jstat -gcnewcapacity

jstat -gcold

jstat -gcoldcapacity

jstat -t

jstat -t -h

header Meaning (bytes)
EC The size of Eden district
EU Used size of Eden area
S0C Size of Survivor 0 sector
S1C Size of Survivor 1 sector
S0U The used size of Survivor 0 sector
S1U The used size of Survivor 1 sector
MC The size of the meta space
MU The used size of the metacase
OC The size of old age
OU The size that has been used in the old age
CCSC Compress the size of the class space
CCSU The used size of the compressed class space
YGC Number of young GC’s from application startup to sampling time
YGCT Time consumed by young GC from application startup to sampling time (seconds)
FGC Number of full GC’s from application startup to sampling time
FGCT Elapsed time from application startup to full GC at sampling time (seconds)
GCT Total time for GC from application startup to sampling time

Interval parameter: Specifies the interval for output statistics, in milliseconds. That is, query interval

Count parameter: Specifies the total number of queries

-t parameter: The output information can be preceded by a Timestamp column, showing the running time of the program. Unit: second

-h parameter: Can output a table header information after the number of rows of data in the periodic data output

Add: Jstat can also be used to determine if there is a memory leak.

Step 1: In a long-running Java program, we can run the jstat command to get multiple rows of performance data in a row and take the minimum value of the OU column (that is, old memory used) in those rows.

Step 2: We then repeat the above operation at long intervals to obtain the minimum of multiple OU values. If these values show an upward trend, it indicates that the Java program’s memory usage is increasing in the past, which means that there are more objects that cannot be reclaimed and therefore a memory leak is likely.

2.4. jinfo: View and modify JVM configuration parameters in real time

Configuration Info (jinfo for Java) : displays vm Configuration parameters and can be used to adjust vm Configuration parameters. In many cases, Java applications do not specify all Java virtual machine parameters. At this point, the developer may not know the default value of a specific Java virtual machine parameter. In this case, you might need to look up the document to get a default value for a parameter. This search process can be very difficult. But with the JInfo tool, developers can easily find the current values of Java virtual machine parameters.

The basic syntax is jinfo [options] pid

Note: The Java process ID must be added

options Option to show
no option Output all parameters and system properties
-flag name Output parameters corresponding to the name
-flag [+-]name Only the parameters marked manageable can be dynamically modified
-flag name=value Sets the parameters corresponding to the name
-flags Print all parameters
-sysprops Output System Properties

jinfo -sysprops

> jinfo -sysprops
jboss.modules.system.pkgs = com.intellij.rt
java.vendor = Oracle Corporation
sun.java.launcher = SUN_STANDARD
sun.management.compiler = HotSpot 64-Bit Tiered Compilers
catalina.useNaming = true
os.name = Windows 10
.
Copy the code

jinfo -flags

> jinfo -flags 25592Non-default VM flags: -XX:CICompilerCount=4 -XX:InitialHeapSize=333447168 -XX:MaxHeapSize=5324668928 -XX:MaxNewSize=1774714880 -XX:MinHeapDeltaBytes=524288 -XX:NewSize=111149056 -XX:OldSize=222298112 -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseFastUnorderedTimeStamps -XX:-UseLargePagesIndividualAllocation -XX:+UseParallelGC Command  line: - agentlib: JDWP = transport = dt_socket, address = 127.0.0.1:8040, suspend = y, server = n - Drebel. Base = C: \ Users \ Vector \. Equal - Drebel. Env. Ide. Plugin. Version = 2021.1.2 - Drebel. Env. Ide. Version = 2020.3.3 - Drebel. Env. Ide. The product = IU -Drebel.env.ide=intellij -Drebel.notification.url=http://localhost:7976 - agentpath: C: \ Users \ Vector \ AppData \ Roaming \ JetBrains \ IntelliJIdea2020.3 \ plugins \ jr - ide - the idea \ lib \ jrebel6 \ lib \ jrebel64 DLL  -Dmaven.home=D:\eclipse\env\maven - Didea. Modules. Paths. A file = C: \ Users \ Vector \ AppData \ Local \ JetBrains \ IntelliJIdea2020.3 \ Maven \ idea - projects - state - 596682 - c7. properties -Dclassworlds.conf=C:\Users\Vector\AppData\Local\Temp\idea-6755-mvn.conf -Dmaven.ext.class.path=D:\IDEA\plugins\maven\lib\maven-event-listener.jar -javaagent:D:\IDEA\plugins\java\lib\rt\debugger-agent.jar -Dfile.encoding=UTF-8Copy the code

jinfo -flag

> jinfo -flag UseParallelGC 25592
-XX:+UseParallelGC

> jinfo -flag UseG1GC 25592
-XX:-UseG1GC
Copy the code

jinfo -flag name

> jinfo -flag UseParallelGC 25592
-XX:+UseParallelGC

> jinfo -flag UseG1GC 25592
-XX:-UseG1GC
Copy the code

jinfo -flag [+-]name

> jinfo -flag +PrintGCDetails 25592
> jinfo -flag PrintGCDetails 25592
-XX:+PrintGCDetails

> jinfo -flag -PrintGCDetails 25592
> jinfo -flag PrintGCDetails 25592
-XX:-PrintGCDetails
Copy the code

Development:

  • Java -xx :+PrintFlagsInitial Views the initial values for all JVM parameters to start

    [Global flags]
         intx ActiveProcessorCount                      = -1                                  {product}
        uintx AdaptiveSizeDecrementScaleFactor          = 4                                   {product}
        uintx AdaptiveSizeMajorGCDecayTimeScale         = 10                                  {product}
        uintx AdaptiveSizePausePolicy                   = 0                                   {product}
    ...
    Copy the code
  • Java -xx :+PrintFlagsFinal Views the final values of all JVM parameters

    [Global flags]
         intx ActiveProcessorCount                      = -1                                  {product}
    ...
         intx CICompilerCount                          := 4                                   {product}
        uintx InitialHeapSize                          := 333447168                           {product}
        uintx MaxHeapSize                              := 1029701632                          {product}
        uintx MaxNewSize                               := 1774714880                          {product}
    Copy the code
  • Java -xx :+PrintCommandLineFlags displays the names and values of detailed XX parameters that have been set by the user or JVM

    -XX:InitialHeapSize=332790016 -XX:MaxHeapSize=5324640256 -XX:+PrintCommandLineFlags -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:-UseLargePagesIndividualAllocation -XX:+UseParallelGC 
    Copy the code

2.5. Jmap: Export memory image files & memory usage

Jmap (JVM Memory Map) : On the one hand, it can get dump file (heap dump snapshot file, binary file), it can also get the target Java process Memory information, including the usage of various areas of the Java heap, heap object statistics, class loading information, etc. Developers can enter the command “jmap-help” in the console to see how to use the Jmap tool and some standard configuration options.

The official documentation: docs.oracle.com/en/java/jav…

The basic usage syntax is:

  • jmap [option] <pid>
  • jmap [option] <executable <core>
  • jmap [option] [server_id@] <remote server IP or hostname>
options role
-dump Generates a dump file (Java heap dump snapshot), -dump:live saves only live objects in the heap
-heap Output detailed information about the entire heap space, including GC usage, heap configuration information, and memory usage
-histo -histo:live Displays the statistics of objects in the heap space, including the number of classes, instances, and total capacity. -histo:live Displays the statistics of only living objects in the heap
-J <flag> Pass the parameters to the JVM started by JMap
-finalizerinfo Objects waiting for the Finalizer thread to execute the Finalize method are displayed in the F-queue. These objects are valid only for Linux and Solaris
-permstat The memory status information of the permanent generation is displayed using ClassLoader. This parameter is valid only on Linux or Solaris
-F If the VM process does not respond to the -dump option, you are forced to generate a dump file

Note: These parameters are somewhat different from the commands displayed on Linux, including the JDK version.

> jmap -dump:format=b,file=<filename.hprof> <pid>> jmap -dump:live,format=b,file=<filename.hprof> <pid>
Copy the code

Since JMap will access all objects in the heap, in order to ensure that it is not disturbed by application threads in the process, JMap needs to use a safety point mechanism that allows all threads to stay in state without changing the data in the heap. That is, the heap snapshot exported by JMap must be at a safe point location. This can lead to biased results based on the heap snapshot.

For example, if some objects in the compile-generated machine code have a lifetime between two safety points, the :live option will not be able to detect those objects.

In addition, if a thread fails to reach a safe point for a long time, JMap will wait forever. Unlike jstat, where the garbage collector actively stores the summary data required by Jstat in a fixed location, Jstat simply reads it.

2.6. Jhat: JDK comes with heap analysis tools

Jhat (JVM Heap Analysis Tool) : The Jhat (JVM Heap Analysis Tool) command provided by the Sun JDK is used together with the jmap command to analyze Heap dump files (Heap dump snapshots) generated by jmap. Jhat has a built-in HTTP/HTML server. After dump file analysis results are generated, users can view the analysis results (vm dump snapshot information) in the browser.

Using the jhat command, you start an HTTP service with port 7000, or http://localhost:7000/, which can be analyzed in the browser.

The jhat command has been removed from JDK9 and JDK10. It is recommended to use VisualVM instead.

Jhat <option> <dumpfile>

The option parameter role
| – stack false true Turn off | turn on object allocation call stack tracing
– | refs false true Turn off | Turn on object reference tracing
-port port-number Set the port number of the JHAT HTTP Server. The default port number is 7000
-exclude exclude-file Data members that need to be excluded when performing an object query
-baseline exclude-file Specify a base heap dump
-debug int Setting the Debug Level
-version After startup, display version information and exit
-J <flag> Pass in a startup parameter, such as -j -XMx512m

2.7. Jstack: Prints snapshots of threads in the JVM

Jstack (JVM Stack Trace) : Used to generate a snapshot of the thread (virtual machine Stack Trace) at the current time of the specified process in the virtual machine. A thread snapshot is a collection of method stacks being executed by each thread of the specified process in the current VIRTUAL machine.

Function of generating thread snapshot: It can be used to locate the cause of a long pause in a thread, such as deadlock between threads, dead loop, and long wait caused by requesting external resources. These are common causes of thread pauses for long periods of time. When a thread pauses, jStack displays the stack of calls made by individual threads.

The official documentation: docs.oracle.com/en/java/jav…

There are several states to look out for in Thread dump

  • Deadlock, Deadlock (focus)
  • Waiting on condition
  • Waiting on monitor entry
  • A) Blocked B) Blocked C) Blocked
  • Running, Runnable
  • Suspended, Suspended
  • In Object wait, object.wait () or TIMED_WAITING
  • Stop, the Parked
The option parameter role
-F Forces the output thread stack when a normally output request is not responded to
-l Displays additional information about locks in addition to the stack
-m C/C++ stacks can be displayed if local methods are called

2.8. JCMD: Multi-function command line

Since JDK 1.7, a command line tool, JCMD, has been added. It is a versatile tool that can be used to do all of the previous commands except jstat. For example, use it to export heap, memory usage, view Java processes, export thread information, perform GC, JVM runtime, and so on.

The official documentation: docs.oracle.com/en/java/jav…

JCMD has most of the functions of Jmap, and it is recommended to use the JCMD command instead of the jmap command on the Oracle website

** jcmd-l: ** Lists all JVM processes

** JCMD process number help: ** Lists all supported commands for the specified process

** JCMD process ID. Command: ** Displays the command data of the specified process

  • Thread.print replaces the JStack directive
  • Gc.class_histogram replaces the -histo operation in Jmap
  • Gc. heap_dump can replace -dump in jmap
  • Gc.run allows you to view the GC execution
  • Vm. uptime can view the total execution time of the program and can replace the -t operation in the jstat directive
  • Vm. system_properties can replace the jinfo-sysprops process ID
  • Vm. flags retrieves configuration parameter information for the JVM

2.9. Jstatd: Remote host information collection

While the previous instructions dealt only with monitoring native Java applications, some of these monitoring tools also support monitoring of remote machines (e.g., JPS, JSTAT). To enable remote monitoring, use the JStatd tool. The jstatd command is an RMI server program that acts as a proxy server to establish communication between the local computer and remote monitoring tools. The JStatd server passes native Java application information to the remote machine.


Next

03-JVM Monitoring and Diagnostic tools -GUI