当前位置:网站首页>Common JVM tools and optimization strategies
Common JVM tools and optimization strategies
2022-07-05 22:50:00 【Thousands of miles in all directions】
Catalog
1.4. Performance optimization steps
2. JVM Monitoring and diagnostic tools - Command line
2.2. jps: See what's running Java process
2.3. jstat: see JVM Statistics
2.3.2 actual combat How to estimate JVM Operation of the
2.3.3 System frequency Full GC How to deal with the system jam
2.4. jinfo: View and modify in real time JVM Configuration parameters
2.5. jmap: Export memory image file & Memory usage
2.6. jhat:JDK Built in heap analysis tool
2.7. jstack: Print JVM Thread snapshot in
2.7.1 actual combat 1: Find the deadlock thread
2.7.2 actual combat 2:CPU All of a sudden , How to locate the code
2.8. jcmd: Multifunctional command line
3. JVM Monitoring and diagnostic tools -GUI piece
4.1.1. Type 1 : Standard parameter options
4.1.2. Type 2 :-X Parameter options
4.1.3. Type 3 :-XX Parameter options
4.2. add to JVM Parameter options
4.3. frequently-used JVM Parameter options
4.3.1. Print settings for XX Options and values
4.3.2. Pile up 、 Stack 、 Method area and other memory size settings
4.3.3. OutOfMemory Related options
4.3.4. Garbage collector related options
4.4. adopt Java Code acquisition JVM Parameters
1. The background that
We learn JVM The ultimate goal is to carry out JVM tuning , This is the need of work , It is also a problem for many interviewers to investigate their qualifications and environment , for example The problems in the production environment are :
How to deal with memory overflow in production environment ?
How much memory should the production environment allocate to the server ?
How to tune the performance of the garbage collector ?
Production environment CPU How to deal with high load ?
How many threads should the production environment allocate to the application ?
No addition log, How to determine whether a line of code has been executed by a request ?
No addition log, How to view the return value of a method in real time ?
2. JVM Monitoring and diagnostic tools - Command line
2.1. summary
Performance diagnosis is a problem that software engineers often face and solve in their daily work , In today's world of user experience , Solving the problem of application performance can bring great benefits .
Java As one of the most popular programming languages , Its application performance diagnosis has been widely concerned by the industry . May cause Java There are many factors causing performance problems in applications , For example, thread control 、 Disk read and write 、 Database access 、 The Internet I/O、 Garbage collection, etc . Want to position these issues , An excellent performance diagnosis tool is essential .
experience 1: Use data to illustrate , Using knowledge to analyze problems , Use tools to deal with problems .
experience 2: There is no monitoring 、 Non tuning !
Simple command line tools
We just came into contact with java While studying , The first two commands you must know are javac,java, Then besides , Is there any other command we can use ?
Let's go into the installation jdk Of bin Catalog , And there's a whole range of auxiliary tools . These aids are used to get the target JVM Different aspects 、 Different levels of information , Help developers solve Java Some of the problems with apps .
Official source address :jdk/jdk11: 1ddf9a99e4ad /src/jdk.jcmd/share/classes/sun/tools/
2.2. jps: See what's running Java process
jps(Java Process Status): Display all... In the specified system HotSpot Virtual machine process ( View virtual machine process information ), Can be used to query running virtual machine processes .
explain : For local virtual machine processes , The local virtual machine of the process ID Process with operating system ID It's consistent , Is the only one. .
The basic syntax is :jps [options] [hostid]
We can also add parameters , To print additional information .
options Parameters
-q: Just show LVMID(local virtual machine id), That is, the local virtual machine is unique id. Do not display the name of the main class, etc
-l: Output the full class name of the application's main class or If the process is executing jar package , The output jar The full path
-m: The output virtual machine process is passed to the main class when it starts main() Parameters of
-v: Lists the JVM Parameters . such as :-Xms20m -Xmx50m It's specified by the startup program jvm Parameters .
explain : The above parameters can be used comprehensively .
Add : If a Java The process turned off the default UsePerfData Parameters ( Use parameters -XX:-UsePerfData), that jps command ( And the following jstat) There will be no way to know Java process .
hostid Parameters
RMI The host name registered in the registry . If you want to remotely monitor java Program , Need to install jstatd.
For cyberspace with more stringent security practices , A custom policy file may be used to show access to a specific trusted host or network , Although this technology is vulnerable to IP Address fraud attack .
If security issues cannot be handled with a custom policy file , So the safest operation is not to run jstatd The server , It's used locally jstat and jps Tools .
2.3. jstat: see JVM Statistics
jstat(JVM Statistics Monitoring Tool): Command line tool for monitoring various running state information of virtual machine . It can show class loading in local or remote virtual machine processes 、 Memory 、 garbage collection 、JIT Compile and other operation data . In the absence of GUI The graphical interface , On servers that only provide a plain text console environment , It will be the preferred tool for locating virtual machine performance problems during runtime . It is often used to detect garbage collection problems and memory leakage problems .
2.3.1 Usage method
Official documents :https://docs.oracle.com/javase/8/docs/technotes/tools/unix/jstat.html
The basic syntax is :jstat -option [-t] [-hlines] vmid [interval [count]]
View the command related parameters :jstat-h or jstat-help
among vmid Is a process id Number , That is to say jps And then you see the number in front of you , as follows :
for example , The process under a testing machine of our company is as follows :
option Parameters
Options option It can be made up of the following values .
Class loading related :
-class: Show ClassLoader Information about : Class loading 、 Unloading quantity 、 Total space 、 Class loading time, etc
Garbage collection related :
-gc: Display and GC Related heap information . Include Eden District 、 Two Survivor District 、 Old age 、 Permanent generation capacity 、 used space 、GC Time totals, etc .
-gccapacity: Display content and -gc Basically the same , But the output is mainly about Java Maximum usage of each area of the heap 、 Minimum space .
-gcutil: Display content and -gc Basically the same , But the output focuses on the percentage of used space in the total space .
-gccause: And -gcutil Function as , But the extra output will result in the last or current GC Cause of occurrence .
-gcnew: It shows the new generation GC condition
-gcnewcapacity: Display content and -gcnew Basically the same , Output mainly focuses on the maximum used 、 Minimum space
-geold: Show older generation GC condition
-gcoldcapacity: Display content and -gcold Basically the same , Output mainly focuses on the maximum used 、 Minimum space
-gcpermcapacity: Shows the maximum number of times the permanent generation has used 、 Minimum space .
JIT dependent :
-compiler: Show JIT Compiler compiled methods 、 Time consuming information
-printcompilation: Output has been JIT Compiled method
The specific meaning of each field is as follows :
Header | meaning ( byte ) |
---|---|
EC | Eden The size of the area |
EU | Eden Area used size |
S0C | Survivor 0 The size of the area |
S1C | Survivor 1 The size of the area |
S0U | Survivor 0 Area used size |
S1U | Survivor 1 Area used size |
MC | The size of the meta space |
MU | The size of the meta space used |
OC | The size of the old age |
OU | The older generation has used the size of |
CCSC | Compress the size of the class space |
CCSU | Compress the size of the class space used |
YGC | From application startup to sampling young gc The number of times |
YGCT | From application startup to sampling young gc Elapsed time ( second ) |
FGC | From application startup to sampling full gc The number of times |
FGCT | From application startup to sampling full gc Time consuming ( second ) |
GCT | From application startup to sampling gc The total time of |
besides , We can also check the status of each area in more detail .
Heap memory statistics
jstat -gccapacity 50062
The specific meaning is :
N GC MN: The minimum capacity of the new generation
N GC MX: The largest capacity of the new generation
NGC: Current Cenozoic capacity
S0C: The size of the first surviving area
S1C: The size of the second surviving area
EC: The size of Eden Park
O GC MN: The smallest capacity in the old days
OGC MX: The biggest capacity in the old days
OGC: The current size of the elderly
OC: The current size of the elderly
MCMN: Minimum metadata capacity
MCMX: Maximum metadata capacity
MC: Current metadata space size
CCSMN: Minimum compressed class space size
CCSMX: Maximum compressed class space size
CCSC: Current compressed class space size
YGC: The younger generation gc frequency
FGC: Old age GC frequency
New generation garbage collection statistics
S0C: The size of the first surviving area
S1C: The size of the second surviving area
S0U: The size of the first surviving area
S1U: The size of the second surviving area
TT: The number of times an object has survived in a new generation
MTT: The maximum number of times an object has survived in a new generation
DSS: The expected size of the surviving area
EC: The size of Eden Park
EU: The use size of Eden Park
YGC: Garbage collection times of young generation
YGCT: Young generation garbage collection consumes time
In addition, there are new generation memory statistics -gcnewcapacity、 Old age garbage collection statistics -gcold、 Memory statistics in the old days -gcoldcapacity、 Metadata spatial statistics -gcmetacapacity wait , You can see more detailed information about the corresponding area .
2.3.2 actual combat How to estimate JVM Operation of the
use jstat gc -pid Command can calculate the following key data , With these data, we can adopt the optimization ideas introduced before , First set up some initial... For your system JVM Parameters , Such as heap memory size , Young generation size ,Eden and Survivor The proportion of , The size of the old age , Threshold for large objects , The threshold for older objects to enter the old age, etc .
The rate at which young people grow Can execute orders jstat -gc pid 1000 10 ( every other 1 Seconds to perform 1 subcommand , In total 10 Time ,mac In the environment, it is necessary to remove the pid), Through observation EU(eden The use of the zone ) To estimate... Per second eden About how many new objects , If the system load is not high , You can put the frequency 1 Second to second 1 minute , even to the extent that 10 Minutes to see the whole situation . Be careful , General systems may have peak and daily periods , So we need to estimate the growth rate of objects in different situations at different times .
Young GC The trigger frequency and time consumption of each time If we know the growth rate of young people, we can infer that eden The size of the area is calculated as Young GC How often does it trigger ,Young GC The average time spent by YGCT/YGC The formula works out , Based on the results, we can probably know how long the system will last because Young GC How long is it going to be stuck .
Every time Young GC After that, how many people survive and enter the elderly generation This is because I knew about it before Young GC The frequency of , Suppose every 5 Minutes at a time , Then you can execute the command jstat -gc pid 300000 10 , Observe the results every time eden, survivor And changes in the use of older people , In every time gc after eden The use of the zone is generally greatly reduced ,survivor And the elderly are likely to grow , The object of these increases is every time Young GC After the survival of the object , And you can see that every time Young GC How many objects are there in the old age , So we can calculate the growth rate of objects in the old age .
Full GC The trigger frequency and time consumption of each time Knowing the growth rate of objects in the old age, we can calculate Full GC The trigger frequency of ,Full GC You can use the formula for each time FGCT/FGC calculated .
In fact, the optimization idea is simply to try to make every Young GC After the survival of less than Survivor Regional 50%, It's all in the younger generation . Try not to let the object enter old age generation . Try to reduce Full GC The frequency of , Avoid frequent Full GC Yes JVM The impact of performance .
2.3.3 System frequency Full GC How to deal with the system jam
We can speculate that full gc Than minor gc There are many reasons ? 1、 Excess caused by insufficient meta space full gc. 2、 Display call System.gc() Cause superfluous full gc, This kind of general online as far as possible XX:+DisableExplicitGC Parameter disable , If you add this JVM Launch parameters , So the code is called System.gc() No effect . 3、 Too many objects enter the elderly .
Generally speaking, every time Full GC Will be in 500 Millisecond or so , and Young GC Every time 50ms within . The data in the figure is an assumption we made :
At this point, we think of two strategies : Increase the elderly generation , Or increase the new generation ?
Combine this object to move to the rule reasoning of the old age , We can judge , If we increase the space of the old age , At this time, the elderly generation executes full GC The cycle of is sure to get longer , But every time full gc It takes longer .
And if we increase the new generation , At this time, there will be more objects that can't endure S0 and S1, Therefore, fewer objects will enter the elderly , therefore Full GC The frequency must be low , Can solve problems .
2.4. jinfo: View and modify in real time JVM Configuration parameters
jinfo(Configuration Info for Java): View virtual machine configuration parameter information , It can also be used to adjust the configuration parameters of the virtual machine . In many cases, cards ,Java The application does not specify all Java Virtual machine parameters . And then , Developers may not know a specific Java Default values for virtual machine parameters . under these circumstances , You may need to find a document to get the default value of a parameter . This search process can be very difficult . But there is. jinfo Tools , Developers can easily find Java The current value of the virtual machine parameter .
The basic syntax is :jinfo [options] pid
explain :java process ID You have to add
Options | Option description |
---|---|
no option | Output all parameters and system properties |
-flag name | Output the parameter of the corresponding name |
-flag [+-]name | Turn on or off the parameter of the corresponding name Only those marked as manageable Can be dynamically modified |
-flag name=value | Set parameters of corresponding name |
-flags | Output all parameters |
-sysprops | Output system properties |
jinfo -sysprops
jinfo -sysprops process ID
for example , The process under a testing machine of our company is as follows :
Then execute the following command :
jinfo -sysprops 12913
You can see a lot of basic information :
This contains a lot of basic information about services
We can also use jinfo -flags Command to view the detailed configuration information of service startup
jinfo -flags 12913
There is too much information , We can find specific ones as needed , for example :
Use jinfo -flag View the settings of a specific item , for example
Input : jinfo -flag UseParallelGC 12913
Now output :
-XX:+UseParallelGC
Input :
jinfo -flag UseG1GC 12913
This will output :
-XX:-UseG1GC
2.5. jmap: Export memory image file & Memory usage
jmap(JVM Memory Map): On the one hand, the function is to obtain dump file ( Heap dump snapshot file , Binary ), It can also get targets Java Process memory related information , Include Java The usage of each area of the reactor 、 Statistics of objects in the heap 、 Class loading information, etc . Developers can enter commands in the console “jmap -help” Look up jmap The specific use of the tool and some standard options configuration .
Official help files :jmap
The basic syntax is :
jmap [option] pid
jmap [option] executable core
jmap [option] [[email protected]] remote server IP or hostname
Options | effect |
---|---|
-dump | Generate dump file (Java Heap dump snapshot ),-dump:live Save only live objects in the heap |
-heap | Output details of the entire heap space , Include GC Use 、 Heap configuration information , And memory usage information, etc |
-histo | Output statistics of objects in heap space , Including class 、 Number of instances and total capacity ,-histo:live Count only the live objects in the heap |
-J flag | Pass parameters to jmap Starting up jvm |
-finalizerinfo | Displayed in the F-Queue Waiting in the Finalizer Threads execute finalize Object of method , only linux/solaris The platform works |
-permstat | With ClassLoader Output permanent memory state information for statistical aperture , only linux/solaris The platform works |
-F | When a virtual machine process pair -dump When the option has no response , Enforce generation dump file , only linux/solaris The platform works |
explain : These parameters and linux How many different commands do you enter to display , Including also affected by jdk The impact of the version .
Case study 1: View the loaded class information
View the information of a loaded class :
jmap -histo 49938
The output is as follows :
among :
num: Serial number
instances: Number of instances
bytes: Occupied space size
class name: Class name ,[C is a char[],[S is a short[],[I is a int[],[B is a byte[],[[I is a int[][]
Case study 2: View memory information
Format :
jmap heap Process number
The above way is in mac The following is forbidden and cannot be used . Another format is :
jmap -dump:live,format=b,file=eureka.hprof 50062
In this way, we will copy the information to eureka.hprof In the .
You can also set memory overflow to automatically export dump file ( When the memory is large , It may not lead out )
1. -XX:+HeapDumpOnOutOfMemoryError
2. -XX:HeapDumpPath=./ ( route )
for example , The parameter configuration of a service of our company is as follows :
#JVM Parameters
JVM_ARGS="-XX:ParallelGCThreads=4 \
-XX:+UseCMSInitiatingOccupancyOnly -XX:+UseFastAccessorMethods \
-XX:+PrintGCDetails -XX:+PrintGCApplicationStoppedTime -XX:+PrintHeapAtGC\
-XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=10 -XX:GCLogFileSize=50M \
-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/home/shared/log/heapdump.hprof \
-XX:SoftRefLRUPolicyMSPerMB=0 -verbose:gc -Xloggc:/home/shared/log/gc-truman.log"
because jmap All objects in the heap will be accessed , In order to ensure that this process is not interfered by the application thread ,jmap We need to use the security point mechanism , Let all threads stay in the state of not changing the data in the heap . in other words , from jmap The exported heap snapshot must be at the safe point . This may lead to a deviation in the analysis results based on the heap snapshot .
for instance , Suppose in the machine code generated by compilation , Some objects have a life cycle between two safe points , that :live Option will not be able to probe these objects .
in addition , If a thread cannot run to a safe point for a long time ,jmap Will keep waiting . It's the same as what I said before jstat Is different , The garbage collector will take the initiative to jstat The required summary data is saved in a fixed location , and jstat Just read it directly .
2.6. jhat:JDK Built in heap analysis tool
jhat(JVM Heap Analysis Tool):Sun JDK Provided jhat Command and jmap Command with , Used to analyze jmap Generated heap dump file ( Heap dump snapshot ).jhat Built in a micro HTTP/HTML The server , Generate dump After analyzing the results of the document , Users can view the analysis results in the browser ( Analyze virtual machine dump snapshot information ).
Used jhat command , It starts a http service , The port is 7000, namely http://localhost:7000/, It can be analyzed in the browser .
explain :jhat Command in JDK9、JDK10 Has been removed from , The official suggested that VisualVM Instead of .
It's basically grammar :jhat option dumpfile
option Parameters | effect |
---|---|
-stack false | true | close | Turn on object allocation call stack trace |
-refs false | true | close | Turn on object reference tracking |
-port port-number | Set up jhat HTTP Server Port number , Default 7000 |
-exclude exclude-file | Data members that need to be excluded when executing object queries |
-baseline exclude-file | Specify a base heap dump |
-debug int | Set up debug Level |
-version | After startup, display version information and exit |
-J flag | Pass in the startup parameters , such as -J-Xmx512m |
2.7. jstack: Print JVM Thread snapshot in
jstack(JVM Stack Trace): Used to generate a thread snapshot of the current time of the specified process in the virtual machine ( Virtual machine stack trace ). Thread snapshot is the collection of method stacks executed by each thread of the specified process in the current virtual machine .
The function of generating thread snapshot : Can be used to locate the cause of a long pause in a thread , Such as interthread deadlocks 、 Dead cycle 、 Long waiting caused by requesting external resources . These are common causes of long thread pauses . When a thread stalls , You can use it jstack Show the stack of each thread call .
Official help files :jstack
stay thread dump in , Pay attention to the following states
Deadlock ,Deadlock( Focus on )
Waiting for resources ,Waiting on condition( Focus on )
Waiting to get the monitor ,Waiting on monitor entry( Focus on )
Blocking ,Blocked( Focus on )
In execution ,Runnable
Pause ,Suspended
Object waiting ,Object.wait() or TIMED_WAITING
stop it ,Parked
option Parameters | effect |
---|---|
-F | When a normal output request is not responded to , Force output thread stack |
-l | Besides the stack , Show additional information about locks |
-m | If you call a local method , Can be displayed C/C++ The stack |
2.7.1 actual combat 1: Find the deadlock thread
How deadlock happened , How can we locate where there is a problem ? For example, the following code will deadlock when executed :
public class DeadLockTest {
private static Object lock1 = new Object();
private static Object lock2 = new Object();
public static void main(String[] args) {
new Thread(() -> {
synchronized (lock1) {
try {
System.out.println("thread1 begin");
Thread.sleep(5000);
} catch (InterruptedException e) {
}
synchronized (lock2) {
System.out.println("thread1 end");
}
}
}).start();
new Thread(() -> {
synchronized (lock2) {
try {
System.out.println("thread2 begin");
Thread.sleep(5000);
} catch (InterruptedException e) {
}
synchronized (lock1) {
System.out.println("thread2 end");
}
}
}).start();
System.out.println("main thread end");
}
}
Use... First jps Find our process :
And then use jstack Check the information :jstack 53385
At this time, we will see the following information ;
Obviously , here thread-1 lock cc58, Then wait for the lock cc48, and thread-2 lock cc48, Then wait for the lock cc58, Therefore, the two cannot yield to each other , Cause a deadlock .
We can also pass jvisualvm Check it out. :
You can see that a “ Deadlock detected ”, If you click “ Threads Dump” You can see and jstack Similar information .
2.7.2 actual combat 2:CPU All of a sudden , How to locate the code
If the program suddenly CPU The proportion soared very high , How to locate the problem of which code ? We use jstack As the main tool, we can solve , Now let's demonstrate , Let's first write such a piece of code :
public class Math {
public int compute() {
int c = (2 + 3) * 10;
return c;
}
public static void main(String[] args) {
Math math = new Math();
while (true) {
math.compute();
}
}
}
Obviously, this code execution will always perform calculations , Take up a lot of CPU resources , Our positioning method is :
Input top Command check occupancy CPU High process
For example, after the above code is executed , Input... At the terminal top The command will see the following information :
Obviously here 51221 Process occupied 95% The above CPU resources .
2. adopt top -pid Process number ,( Some environments are top -p Process number ) Check threads with high resource consumption
for example :
top -pid 51221
Then input H, You can see the active threads in the process .
But this function mac Under no , I did the following tests in the company environment , Screenshot :
We can see... Here 19664 It's occupation CPU The highest thread . And then we can use it jstack To search for , To this end, we first see Math The program is in jstack What is the information in .
We can see here a tid and nid, among tid yes JVM Thread ID in , and nid Is local nid, We should use the latter at the same time , In the figure above 19664 yes 10 Base number , And in the jstack Medium is 16 Base number , Therefore, we need to convert it before executing , So at this time, we can search through the following command Math Method :
jstack 19663|grep -A 10 4cd0
This will get the thread stack information 4cd0 This thread is behind the line 10 That's ok , You can see from the stack what causes cpu High pitched Method of use .
2.8. jcmd: Multifunctional command line
stay JDK 1.7 in the future , A new command line tool has been added jcmd. It's a versatile tool , Can be used to achieve the front in addition to jstat All other command functions . such as : Use it to export the heap 、 Memory usage 、 see Java process 、 Export thread information 、 perform GC、JVM Running time, etc .
Official help files :jcmd
jcmd Have jmap Most of the functions , And in Oracle It is also recommended on the official website of jcmd Command generation jmap command
jcmd -l: List all JVM process
jcmd Process number help: For the specified process , List all the specific commands supported
jcmd Process number Specific commands : Displays the data of the command for the specified process
Thread.print Can replace jstack Instructions
GC.class_histogram Can replace jmap Medium -histo operation
GC.heap_dump Can replace jmap Medium -dump operation
GC.run You can see GC Implementation of
VM.uptime You can see the total execution time of the program , Can replace jstat Directive -t operation
VM.system_properties Can replace jinfo -sysprops process id
VM.flags Can get JVM Configuration parameter information of
3. JVM Monitoring and diagnostic tools -GUI piece
3.1. Tool Overview
Using command line tools or combinations from the previous chapter can help you get the target Java Basic information about application performance , But they have the following limitations :
1. Unable to get method level analysis data , Such as the calling relationship between methods 、 Call times and call time of each method ( This is crucial for locating application performance bottlenecks ).
2. Ask the user to log in to the target Java On the host where the application is located , It's not very convenient to use .
3. The analysis data is output through the terminal , The results are not intuitive enough .
So ,JDK Provides some memory leak analysis tools , Such as jconsole,jvisualvm etc. , Used to assist developers in locating problems , But these tools are often not enough to meet the needs of rapid positioning . So here we introduce a few more tools 、 A little more .
JDK Built-in tools
jconsole:JDK The visual monitoring tool . see Java Running overview of the application 、 Monitoring heap information 、 The permanent zone ( Or meta space ) usage 、 Class loading, etc
Visual VM:Visual VM It's a tool , It provides a visual interface , For viewing Java Running on a virtual machine is based on Java Technology application details .
JMC:Java Mission Control, built-in Java Flight Recorder. Can collect... With very low performance overhead Java Performance data for virtual machines .
Third party tools
MAT:MAT(Memory Analyzer Tool) Is based on Eclipse Memory analysis tools , It's a fast one 、 functional Java heap Analysis tools , It can help us find memory leaks and reduce memory consumption
JProfiler: Business software , Need to pay . Powerful .
3.2. JConsole
jconsole: from Java5 Start , stay JDK The built-in java Monitoring and management console . Used to deal with JVM Medium memory 、 Thread and class monitoring , It's based on JMX(java management extensions) Of GUI Performance monitoring tools .
Official address :Using JConsole - Java SE Monitoring and ManagementGuide
3.3. Visual VM
Visual VM It is a powerful visual tool for multi in one fault diagnosis and performance monitoring . It integrates multiple JDK Command line tools , Use Visual VM Can be used to display the virtual machine process and process configuration and environment information (jps,jinfo), Monitoring application CPU、GC、 Pile up 、 Method area and thread information (jstat、jstack) etc. , Even in place of JConsole. stay JDK 6 Update 7 in the future ,Visual VM As JDK Part of the release (VisualVM stay JDK/bin Under the table of contents ) namely : It's completely free .
The main function :
1. Generate / Read heap memory / Thread snapshot
2. see JVM Parameters and system properties
3. View the running virtual machine processes
4. Real time monitoring of program resources
5.JMX Proxy connection 、 Remote environmental monitoring 、CPU Analysis and memory analysis
Official address :VisualVM: Home
3.4. Eclipse MAT
MAT(Memory Analyzer Tool) Tool is a powerful Java Heap memory Analyzer . Can be used to find memory leaks and check memory consumption .MAT Is based on Eclipse Developed , Not only can it be used alone , It can also be embedded in as a plug-in Eclipse Use in . Is a free performance analysis tool , It is very convenient to use .
MAT Can be analyzed heap dump file . In memory analysis , As long as you get the memory image of the current device hprof file , adopt MAT Open it and you can see the current memory information intuitively . As a general rule , This memory information contains :
All the object information , Include object instances 、 Member variables 、 Basic type values stored in the stack and reference values of other objects stored in the heap .
All the class information , Include classloader、 Class name 、 Parent class 、 Static variables, etc
GCRoot Reference paths to all of these objects
Thread information , Including the thread call stack and the thread local variables of this thread (TLS)
MAT It's not a universal tool , It doesn't handle all types of heap storage files . But more mainstream manufacturers and formats , for example Sun,HP,SAP Adopted by the HPROF Binary heap storage files , as well as IBM Of PHD Heap storage files can be well parsed .
The most attractive thing is the ability to quickly generate memory leak reports for developers , It is convenient to locate and analyze problems . although MAT It's so powerful , But memory analysis is not as simple as one click , A lot of memory problems still need to be solved from MAT The information presented to us can only be found through experience and intuition .
Official address : Eclipse Memory Analyzer Open Source Project | The Eclipse Foundation
3.5. JProfiler
Running Java Sometimes I want to test the memory usage of the runtime , At this time, you need to use the test tool to check . stay eclipse There are Eclipse Memory Analyzer tool(MAT) Plug ins can be tested , And in the IDEA There is also such a plug-in in , Namely JProfiler.JProfiler By ej-technologies Developed by the company Java Apply performance diagnostic tools . Powerful , But the charge .
characteristic :
Easy to use 、 User friendly interface ( Simple and powerful )
It has little influence on the application being analyzed ( Provide templates )
CPU,Thread,Memory Analysis is particularly powerful
Support for jdbc,noSql,jsp,servlet,socket Wait for analysis
Support for multiple modes ( offline , On-line ) Analysis of
Support monitoring local 、 The remote JVM
Cross platform , There are multiple installed versions of the operating system
The main function :
1- Method call : Analysis of method calls can help you understand what your application is doing , And find ways to improve its performance
2- Memory allocation : By analyzing objects on the heap 、 Reference chains and garbage collection can help you fix memory leaks , Optimize memory usage
3- Threads and locks :JProfiler Provides a variety of analysis views for threads and locks to help you find multithreading problems
4- Advanced subsystem : Many of the performance problems are at a higher semantic level . for example , about JDBC call , You may want to find the slowest SQL sentence .JProfiler Support integration analysis of these subsystems
Official website address :Java Profiler - JProfiler
Data collection mode :
JProfier There are two ways to collect data :Sampling( Sample collection ) and Instrumentation( Refactoring patterns )
Instrumentation: This is a JProfiler Full function mode . stay class Before loading ,JProfier Write the relevant function code into the class Of bytecode in , For the running jvm There is a certain influence .
advantage : Powerful . In this setting , The call stack information is accurate .
shortcoming : To analyze class More , It has a great impact on the performance of the application ,CPU It can be expensive ( Depending on Filter The control of ). Therefore, the use of this mode is generally compatible with Filter Use , Analyze only specific classes or packages
Sampling: Similar to sample statistics , Every once in a while (5ms) Count the information in the method stack of each thread stack .
advantage : Yes CPU The cost of this is very low , Little impact on Application ( Even if you don't configure any Filter)
shortcoming : Some of the data / Features cannot provide ( for example : Number of calls to method 、 execution time )
notes :JProfiler It does not indicate the type of data collection , The collection type here is the collection type for method calls . because JProfiler Most of its core functions rely on the data collected by method calls , So it can be directly considered as JProfiler The data collection type of .
Remote sensing monitoring Telemetries
Memory view Live Memory
Live memory Memory profiling :class/class instance Information about . For example, the number of objects , size , Object to create a method execution stack , Object creation hotspots .
All objects All Objects: Displays a list of all loaded classes and the number of instances allocated on the heap . Only Java 1.5(JVMTI) This view will be displayed .
Record object Record Objects: View the assignment of objects for a specific period of time , And record the allocated call stack .
Assign access tree Allocation Call Tree: Show a request tree or method 、 class 、 Package or with annotated assignment information to the selected class J2EE Components .
Assign hotspots Allocation Hot Spots: Show a list , Including method 、 class 、 Package or assign... Of the selected class J2EE Components . You can mark the current value and display the difference value . For each hotspot, its trace tree can be displayed .
Class tracker Class Tracker: The class trace view can contain any number of charts , Show instances and times of selected classes and packages .
Heap traversal heap walker
cpu View cpu views
JProfiler Provides different ways to log access trees to optimize performance and detail . Threads or thread groups and thread status can be selected by all views . All views can be aggregated into methods 、 class 、 Package or J2EE Components and other different layers .
Access tree Call Tree: Show an accumulated top-down tree , The tree contains everything in JVM Access queues recorded in .JDBC,JMS and JNDI Service requests are annotated in the request tree . The request tree can be based on Servlet and JSP Yes URL Different needs to be split .
hotspot Hot Spots: Show a list of the most time consuming methods . It can display the backtree for each hotspot . The hotspot can request... In accordance with the method ,JDBC,JMS and JNDI Service request and in accordance with URL Request to calculate .
Access map Call Graph: Show a method from selected 、 class 、 Package or J2EE Figure of the access queue at the beginning of the component .
Methods statistics Method Statistis: Show the call time details of the method recorded over time .
Thread view threads
JProfiler Judge the running state of the thread by monitoring its history , And monitor whether there is thread blocking , It can also present the methods managed by a thread in the form of a tree . Analyze threads .
Thread history Thread History: Shows an activity schedule with thread activity and thread status .
Thread monitoring Thread Monitor: Show a list , Including all active threads and their current activity status .
Thread dump Thread Dumps: Show stack trace for all threads .
Thread analysis focuses on three aspects :
1.web The maximum number of threads in the container . such as :Tomcat The thread capacity of should be slightly larger than the maximum number of concurrent .
2. Thread blocking
3. thread deadlock
Monitoring and locking Monitors &Locks
All threads hold locks and lock information . Observe JVM And see the status :
Deadlock detection chart Current Locking Graph: Show JVM The current deadlock graph in .
Current monitors Current Monitors: Displays the monitors currently in use and includes their associated threads .
Lock history chart Locking History Graph: The display is recorded in JVM Lock history in .
History test records Monitor History: Shows the history of major waiting and blocking events .
Monitor usage statistics Monitor Usage Statistics: Show group monitoring , Statistical monitoring data of threads and monitoring classes
3.6. Arthas
The above tools must be configured with relevant monitoring parameters in the server project process , The tool then remotely connects to the project process , Get the relevant data . This will bring some inconvenience , For example, the network of online environment is isolated , Local monitoring tools are not connected to the online environment at all . And it's like Jprofiler Such a business tool , It needs to be paid .
So is there a tool that doesn't need a remote connection , There is no need to configure monitoring parameters , At the same time, it also provides rich performance monitoring data ?
Alibaba open source performance analysis artifact Arthas emerge as the times require .
Arthas yes Alibaba Open source Java Diagnostic tools , Loved by developers . Online troubleshooting , No need to restart ; Dynamic tracking Java Code ; Real-time monitoring JVM state .Arthas Support JDK 6 +, Support Linux/Mac/Windows, Use command line interaction mode , At the same time, it provides rich Tab Automatic completion function , Further facilitate problem location and diagnosis . When you encounter the following similar problems and are at a loss ,Arthas Can help you solve :
Where does this class come from jar Package loaded ? Why do you report all kinds of related Exception?
Why didn't I change the code to ? I didn't commit? The branch is wrong ?
I can't get online when I have a problem debug, Can't we just add logs and redistribute them ?
There is a problem with the data processing of a user online , But online also can't debug, It can't be reproduced offline !
Is there a global perspective to see the health of the system ?
Is there any way to monitor JVM The real-time running state of ?
How to quickly locate the hot spot of application , Generate flame chart ?
Official address : Quick start — Arthas 3.6.2 file
Installation mode : If the speed is slow , You can try the code cloud in China Gitee download .
wget https://io/arthas/arthas-boot.jar
wget https://arthas/gitee/io/arthas-boot.jar
Arthas Just one. java Program , So you can use it directly java -jar function .
In addition to looking at it on the command line ,Arthas There is also support for Web Console. After successfully starting the connection process, it has started automatically , You can directly access http://127.0.0.1:8563/ visit , The operation mode on the page is exactly the same as the console .
Basic instructions
quit/exit Quit current Arthas client , other Arthas Xihutuan is not affected
stop/shutdown close Arthas Server side , all Arthas All clients exit
help View command help
cat Print file contents , and linux Inside cat Command similar
echo Printing parameters , and linux Inside echo Command similar
grep Match lookup , and linux Inside gep Command similar
tee Copy standard input to standard output and specified file , and linux Inside tee Command similar
pwd Return to the current working directory , and linux Command similar
cls Clear the current screen area
session View information about the current session
reset Reset enhancement class , Will be Arthas All the enhanced classes are restored , Arthas All enhanced classes will be reset when the server is shut down
version Output current target Java Process loaded Arthas Version number
history Print command history
keymap Arthas List of shortcuts and custom shortcuts
jvm relevant
dashboard Real time data panel of current system
thread View the current JVM Thread stack information for , add -b Parameters can directly see the deadlock thread
jvm View the current JVM Information about
sysprop View and modify JVM System properties of
sysem see JVM Environment variables of
vmoption View and modify JVM The diagnosis is related to option
perfcounter View the current JVM Of Perf Counter Information
logger View and modify logger
getstatic View the static properties of the class
ognl perform ognl expression
mbean see Mbean Information about
heapdump dump java heap, similar jmap Ordered heap dump function
class/classloader relevant
sc see JVM Loaded class information
-d Output details of the current class , Include the source of the original file loaded by this class 、 Declaration of a class 、 Loaded Classloader Wait for details . If a class is divided into multiple Classloader Loaded , There will be many times
-E Turn on regular expression matching , The default is wildcard matching
-f Output the member variable information of the current class ( Need to match parameters -d Use it together )
-X Specifies the traversal depth of the property when outputting a static variable , The default is 0, I.e. direct use toString Output
sm View method information for loaded classes
-d Show the details of each method
-E Turn on regular expression matching , The default is wildcard matching
jad Decompile specifies the source code of the loaded class
mc Memory compiler , Memory compilation .java File for .class file
retransform Load external .class file , retransform To JVM in
redefine Load external .class file ,redefine To JVM in
dump dump The... Of the class has already been loaded byte code Go to a specific directory
classloader see classloader Inheritance tree of ,urts, Class loading information , Use classloader Go to getResource
-t see classloader Inheritance tree of
-l Load instances by class to view statistics
-c use classloader Corresponding hashcode To see the corresponding Jar urls
monitor/watch/trace relevant
monitor Method execution monitoring , Call the number 、 execution time 、 Failure rate -c Statistical period , The default value is 120 second watch Methods perform observation , The range that can be observed is : Return value 、 Throw an exception 、 Enter the reference , By writing groovy Expression to view the corresponding variable -b Observe... Before method calls ( Off by default ) -e Observe after the method is abnormal ( Off by default ) -s Observe... After the method returns ( Off by default ) -f At the end of the method ( Normal return and abnormal return ) Observe ( Default on ) -x Specifies the property traversal depth of the output result , The default is 0 trace Method internal call path , And output the time consumption of each node on the method path -n Execution limit stack Output the call path of the current method to be called tt Method to execute the spatiotemporal tunnel of data , Record the input and return information of each call of the specified method , And can observe these different time adjustments
other
jobs List all job
kill Force termination of mission
fg Pull the suspended task to the foreground for execution
bg Put the suspended task in the background
grep Search for results that meet the criteria
plaintext Remove the result of the command ANSI Color
wc Output results by line
options View or set up Arthas Global switch
profiler Use async-profiler Sample the application , Generate flame chart
See the video explanation for specific operations such as decompilation .
3.7. Java Misssion Control
stay Oracle Acquisition Sun Before ,Oracle Of JRockit The virtual machine provides a device called JRockit Mission Control Virtual machine diagnostic tool for .
stay Oracle Acquisition sun after ,Oracle The company also owns Hotspot and JRockit Two virtual machines . according to Oracle about Java The strategy of , In the future development , Will JRokit The excellent features of are transplanted to Hotspot On . One of the important improvements is in Sun Of JDK Added JRockit Support for .
stay Oracle JDK 7u40 after ,Mission Control This tool has been bound to Oracle JDK In the release .
since Java11 Start , In this section JFR It has been open source . But before Java edition ,JFR Belong to Commercial Feature adopt Java Virtual machine parameters -XX:+UnlockCommercialFeatures Turn on .
Java Mission Control( abbreviation JMC) , Java A powerful official tool , It's a tool for Java Application Management 、 monitor 、 Tool suite for profiling and troubleshooting . It contains a GUI The client and many other tools used to collect Java Plug in of virtual machine performance data, such as JMX Console( It can access the data that is used to store the virtual machine in the system MXBeans) And the efficiency built into the virtual machine profiling Tools Java Flight Recorder(JFR).
JMC The other advantage of this is : Using sampling , Instead of traditional code placement technology , The impact on application performance is very, very small , It's completely open JMC To do the pressure test ( The only impact could be full gc More ).
Official address :GitHub - JDKMissionControl/jmc: This mirror is deprecated - please start using https://github.com/openjdk/jmc
Java Flight Recorder
Java Flight Recorder yes JMC One of the components of , Can collect... With very low performance overhead Java Performance data for virtual machines . Compared with other tools ,JFR The performance overhead is small , Under the default configuration, the average is lower than 1%.JFR It can directly access the enemy in the virtual machine without affecting the optimization of the virtual machine . Therefore, it is very suitable for full load operation in production environment Java Program .
Java Flight Recorder and JDK Mission Control Together to create a complete tool chain .JDK Mission Control But for Java Flight Recorder Continuous collection of low-level and detailed runtime information for efficient 、 Detailed analysis .
When enabled JFR A series of events occurring during the run will be recorded . These include Java Level events such as thread Events 、 Lock event , as well as Java Events inside the virtual machine , For example, create a new object , Garbage collection and just in time compilation events . Divided by timing and duration ,JFR There are four types of events , There are four of them :
Instantaneous Events (Instant Event) , What users care about is whether they happen or not , For example, abnormal 、 Thread start events .
Continuous events (Duration Event) , What users care about is their duration , For example, garbage collection events .
Timing events (Timed Event) , Is a persistent event whose duration exceeds a specified threshold .
Sampling events (Sample Event), It's a periodically sampled event .
A common example of sampling events is method sampling (Method Sampling), That is to count the stack trace of each thread every other . If there is a recurring method in these sampled stack traces , So we can speculate that this method is a hot spot method
3.8. Other tools
Flame Graphs( Flame chart )
In the pursuit of extreme performance scenarios , Understand how your program works cpu It's important what you're doing , The flame diagram is a very intuitive display CPU A tool for time allocation throughout the life cycle of a program . Flame diagrams should not be new to modern programmers , This tool can be very intuitive to show the call to find CPU Consumption bottlenecks .
Online about Java Most of the explanation of the flame diagram comes from Brenden Gregg The blog of http://new.brendangregg.com/flamegraphs.html
Flame chart , Simply by x Axis bar width to measure time index ,y The axis represents the level of the thread stack .
Tprofiler
Case study : Use JDK Self provided tools for JVM Tuning can bring TPS from 2.5 Upgrade to 20( Promoted 7 times ), And accurate Positioning system bottlenecks .
System bottlenecks are : There are not too many objects in the application 、 A large number of business threads are frequently creating temporary objects with a long life cycle , There's a problem in the code .
that , How to accurately locate these performance codes in massive business code ? Ali open source tools are used here Tprofiler To locate These performance codes , Successfully solved GC Too frequent performance bottle pre , And finally on the basis of the last optimization TPS It's been promoted again 4 times , That is to say, to be promoted to 100.
Tprofiler Configuration deployment 、 Remote operation 、 It's not too complicated to read and talk , The operation is still very simple . But it can To the point 、 Immediate effect , Helped us to solve GC Too frequent performance bottle pre .
Tprofiler The most important feature is to be able to work out the time period you specify JVM Of top method these top method It's very likely that it caused you JVM The culprit of performance bottleneck . This is most of the other JVM What tuning tools don't have , Include JRockit Mission Control.JRokit Lead developer Marcus Hirt On its private blog 《 Lom Overhead Method Profiling cith Java Mission Control》 It is clearly pointed out in the comments below that JRMC Well doesn't support TOP Statistics of methods .
Official address :GitHub - alibaba/TProfiler: TProfiler It is a performance analysis tool that can be used in production environment for a long time
Btrace
Common dynamic tracking tools are BTrace、HouseHD( The project has been discontinued )、Greys-Anatomy( Developed by Chinese people Personal developers )、Byteman(JBoss Produce ), Be careful Java The running time tracking tool well is not limited to these types , But these are relatively common .
BTrace yes SUN Kenai An open source project under the cloud computing development platform , For the purpose of java Provide a safe and reliable dynamic tracking analysis tool . Let's look at the divination day first Trace Official definition :
It's about a Java Platform security dynamic tracking tool , Can be used to dynamically track a running Java Program .BTrace Dynamically adjust the target application's classes to inject tracking code (“ Bytecode tracking “).
4. JVM Runtime parameters
4.1. JVM Parameter options
Official website address :java
4.1.1. Type 1 : Standard parameter options
> java -help
usage : java [-options] class [args...]
( Executive class )
or java [-options] -jar jarfile [args...]
( perform jar file )
The options include :
-d32 Use 32 Bit data model ( If available )
-d64 Use 64 Bit data model ( If available )
-server choice "server" VM
Default VM yes server.
-cp < Contents and zip/jar Class search path for file >
-classpath < Contents and zip/jar Class search path for file >
use ; Separate directories , JAR archives
and ZIP File list , Used to search for class files .
-D< name >=< value >
Set system properties
-verbose:[class|gc|jni]
Enable verbose output
-version Output product version and exit
-version:< value >
Warning : This feature is out of date , Will be in
Delete... In future releases .
The specified version is required to run
-showversion Output the product version and continue
-jre-restrict-search | -no-jre-restrict-search
Warning : This feature is out of date , Will be in
Delete... In future releases .
Include... In version search / Exclusive to users JRE
-? -help Output this help message
-X Output help for non-standard options
-ea[:<packagename>...|:<classname>]
-enableassertions[:<packagename>...|:<classname>]
Enable assertions at the specified granularity
-da[:<packagename>...|:<classname>]
-disableassertions[:<packagename>...|:<classname>]
Disable assertions with specified granularity
-esa | -enablesystemassertions
Enable system assertion
-dsa | -disablesystemassertions
Disable system assertion
-agentlib:<libname>[=< Options >]
Load native agent Library <libname>, for example -agentlib:hprof
See also -agentlib:jdwp=help and -agentlib:hprof=help
-agentpath:<pathname>[=< Options >]
Load the native agent library by the full pathname
-javaagent:<jarpath>[=< Options >]
load Java Programming language agent , see also java.lang.instrument
-splash:<imagepath>
Use the specified image to display the start screen
For more information , see also http://www.oracle.com/technetwork/java/javase/documentation/index.html.
Server Patterns and Client Pattern
Hotspot JVM There are two patterns , Namely server and client, Pass respectively -server and -client Mode setting
32 Bit system , By default Client Type of JVM. Want to use Server Pattern , The machine configuration has at least 2 More than one CPU and 2G The physical memory above .client Mode is suitable for desktop applications that require less memory , By default Serial Serial garbage collector
64 Bit system , Only support server Mode JVM, For applications that require large memory , Parallel garbage collectors are used by default
Official website address :https://docs.oracle.com/javase/8/docs/technotes/guides/vm/server-class.html
How to know which mode the system uses by default ?
adopt java -version command : You can see Server VM word , Representing the current system usage is Server Pattern
> java -version
java version "1.8.0_201"
Java(TM) SE Runtime Environment (build 1.8.0_201-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.201-b09, mixed mode)
4.1.2. Type 2 :-X Parameter options
java -X
-Xmixed Mixed mode execution ( Default )
-Xint Explain only mode execution
-Xbootclasspath:< use ; Separate directories and zip/jar file >
Set search paths to guide classes and resources
-Xbootclasspath/a:< use ; Separate directories and zip/jar file >
Attached at the end of the boot class path
-Xbootclasspath/p:< use ; Separate directories and zip/jar file >
Before the boot class path
-Xdiag Display additional diagnostic messages
-Xnoclassgc Disable garbage collection
-Xincgc Enable incremental garbage collection
-Xloggc:<file> take GC The status is recorded in the file ( With a time stamp )
-Xbatch Disable background compilation
-Xms<size> Set the initial Java Heap size
-Xmx<size> Set the maximum Java Heap size
-Xss<size> Set up Java Thread stack size
-Xprof Output cpu Profile data
-Xfuture Enable the most stringent checks , Expected future defaults
-Xrs Reduce Java/VM Use of operating system signals ( See documentation )
-Xcheck:jni Yes JNI Function to perform other checks
-Xshare:off Don't try to use shared class data
-Xshare:auto Use shared class data when possible ( Default )
-Xshare:on Require shared data , Otherwise it will fail .
-XshowSettings Show all settings and continue
-XshowSettings:all
Show all settings and continue
-XshowSettings:vm Show all and vm Related settings and continue
-XshowSettings:properties
Show all property settings and continue
-XshowSettings:locale
Show all locale related settings and continue
-X Options are nonstandard options , If there is any change , Without notice .
How do you know JVM Mixed mode is used by default ?
similarly , adopt java -version command : You can see mixed mode word , Representing the current system using mixed mode
4.1.3. Type 3 :-XX Parameter options
Boolean Type format
-XX:+<option> Enable option attribute
-XX:-<option> Ban option attribute
Not Boolean Type format
-XX:<option>=<number> Set up option The number , You can bring units like k/K/m/M/g/G
-XX:<option>=<string> Set up option Character value
4.2. add to JVM Parameter options
eclipse and idea I don't need to say much about the configuration in , stay Run Configurations in VM Options Medium configuration is ok , Be the same in essentials while differing in minor points
The program is running
function jar package
java -Xms100m -Xmx100m -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps -jar demo.jar
Tomcat function war package
# linux Next catalina.sh add to
JAVA_OPTS="-Xms512M -Xmx1024M"
# windows Next catalina.bat add to
set "JAVA_OPTS=-Xms512M -Xmx1024M"
# Set up Boolean Type parameter
jinfo -flag [+|-]<name> <pid>
# Set not to Boolean Type parameter
jinfo -flag <name>=<value> <pid>
4.3. frequently-used JVM Parameter options
4.3.1. Print settings for XX Options and values
-XX:+PrintCommandLineFlags Program runtime JVM Default settings or manually set by the user XX Options
-XX:+PrintFlagsInitial Print all XX The default value of the option
-XX:+PrintFlagsFinal Print all XX The actual value of the option
-XX:+PrintVMOptions Print JVM Parameters of
4.3.2. Pile up 、 Stack 、 Method area and other memory size settings
# Stack
-Xss128k <==> -XX:ThreadStackSize=128k Set the thread stack size to 128K
# Pile up
-Xms2048m <==> -XX:InitialHeapSize=2048m Set up JVM The initial heap memory is 2048M
-Xmx2048m <==> -XX:MaxHeapSize=2048m Set up JVM The maximum heap memory is 2048M
-Xmn2g <==> -XX:NewSize=2g -XX:MaxNewSize=2g Set the size of the younger generation to 2G
-XX:SurvivorRatio=8 Set up Eden District and Survivor Area ratio , The default is 8
-XX:NewRatio=2 Set the ratio between the old and the young , The default is 2
-XX:+UseAdaptiveSizePolicy Set size scale adaptive , Default on
-XX:PretenureSizeThreadshold=1024 The setting allows objects larger than this threshold to be allocated directly to the older generation , Only right Serial、ParNew Collector valid
-XX:MaxTenuringThreshold=15 Set the age limit for the new generation to promote the old generation , The default is 15
-XX:TargetSurvivorRatio Set up MinorGC After the end Survivor The expected proportion of space occupied by the zone
# Method area
-XX:MetaspaceSize / -XX:PermSize=256m Set up the meta space / The initial value of permanent generation is 256M
-XX:MaxMetaspaceSize / -XX:MaxPermSize=256m Set up the meta space / The maximum value of permanent generation is 256M
-XX:+UseCompressedOops Use compressed objects
-XX:+UseCompressedClassPointers Use compressed class pointers
-XX:CompressedClassSpaceSize Set up Klass Metaspace Size , Default 1G
# Direct memory
-XX:MaxDirectMemorySize Appoint DirectMemory Capacity , Default equal to Java Heap maximum
4.3.3. OutOfMemory Related options
-XX:+HeapDumpOnOutMemoryError Memory appears OOM Time generation Heap Dump files , The two are mutually exclusive
-XX:+HeapDumpBeforeFullGC appear FullGC Time generation Heap Dump files , The two are mutually exclusive
-XX:HeapDumpPath=<path> Appoint heap The storage path of the dump file , Default current directory
-XX:OnOutOfMemoryError=<path> Specify the path to the feasibility program or script , Happen when OOM When the script is executed
4.3.4. Garbage collector related options
First of all, we need to understand the collocation relationship between garbage collectors
The red dotted line indicates that jdk8 When I was Deprecate,jdk9 Is deleted
The dotted green line indicates that jdk14 When I was Deprecate
The empty green box means jdk9 When I was Deprecate,jdk14 Is deleted
# Serial Recyclers
-XX:+UseSerialGC The younger generation uses Serial GC, Used in the old days Serial Old GC
# ParNew Recyclers
-XX:+UseParNewGC The younger generation uses ParNew GC
-XX:ParallelGCThreads Set the number of threads for the younger generation of parallel collectors .
In a general way , Best and CPU Equal quantity , To avoid the impact of too many threads on garbage collection performance .
_ParallelGCThreads={CPU_Count(CPU_Count<=8)3+(5∗CPU_Count/8)(CPU_Count>8)
# Parallel Recyclers
-XX:+UseParallelGC The younger generation uses Parallel Scavenge GC, Activate each other
-XX:+UseParallelOldGC Used in the old days Parallel Old GC, Activate each other
-XX:ParallelGCThreads
-XX:MaxGCPauseMillis Set the maximum pause time for the garbage collector ( namely STW Time for ), In milliseconds .
In order to control the pause time as much as possible MaxGCPauseMills within , The collector adjusts as it works Java Heap size or some other parameters .
For users , The shorter the pause, the better the experience ; But the server side focuses on high concurrency , Overall throughput .
So the server side is suitable for Parallel, Control . This parameter should be used with caution .
-XX:GCTimeRatio The proportion of garbage collection time to total time (1 / (N+1)), Used to measure throughput
Value range (0,100), The default value is 99, That is, the garbage collection time should not exceed 1%.
With the previous -XX:MaxGCPauseMillis There are some contradictions in the parameters . The longer the pause is ,Radio The parameter is easy to exceed the set ratio .
-XX:+UseAdaptiveSizePolicy Set up Parallel Scavenge The collector has an adaptive tuning strategy .
In this mode , The size of the younger generation 、Eden and Survivor The proportion of 、 Parameters such as the age of the person who is promoted to the old age will be automatically adjusted , To reach the heap size 、 The balance between throughput and pause time .
Where manual tuning is difficult , You can use this adaptive approach directly , Only specify the maximum heap for the virtual machine 、 Target throughput (GCTimeRatio) And pause time (MaxGCPauseMills), Let the virtual machine do its own tuning .
# CMS Recyclers
-XX:+UseConcMarkSweepGC The younger generation uses CMS GC.# Serial Recyclers
-XX:+UseSerialGC The younger generation uses Serial GC, Used in the old days Serial Old GC
# ParNew Recyclers
-XX:+UseParNewGC The younger generation uses ParNew GC
-XX:ParallelGCThreads Set the number of threads for the younger generation of parallel collectors .
In a general way , Best and CPU Equal quantity , To avoid the impact of too many threads on garbage collection performance .
_ParallelGCThreads={CPU_Count(CPU_Count<=8)3+(5∗CPU_Count/8)(CPU_Count>8)
# Parallel Recyclers
-XX:+UseParallelGC The younger generation uses Parallel Scavenge GC, Activate each other
-XX:+UseParallelOldGC Used in the old days Parallel Old GC, Activate each other
-XX:ParallelGCThreads
-XX:MaxGCPauseMillis Set the maximum pause time for the garbage collector ( namely STW Time for ), In milliseconds .
In order to control the pause time as much as possible MaxGCPauseMills within , The collector adjusts as it works Java Heap size or some other parameters .
For users , The shorter the pause, the better the experience ; But the server side focuses on high concurrency , Overall throughput .
So the server side is suitable for Parallel, Control . This parameter should be used with caution .
-XX:GCTimeRatio The proportion of garbage collection time to total time (1 / (N+1)), Used to measure throughput
Value range (0,100), The default value is 99, That is, the garbage collection time should not exceed 1%.
With the previous -XX:MaxGCPauseMillis There are some contradictions in the parameters . The longer the pause is ,Radio The parameter is easy to exceed the set ratio .
-XX:+UseAdaptiveSizePolicy Set up Parallel Scavenge The collector has an adaptive tuning strategy .
In this mode , The size of the younger generation 、Eden and Survivor The proportion of 、 Parameters such as the age of the person who is promoted to the old age will be automatically adjusted , To reach the heap size 、 The balance between throughput and pause time .
Where manual tuning is difficult , You can use this adaptive approach directly , Only specify the maximum heap for the virtual machine 、 Target throughput (GCTimeRatio) And pause time (MaxGCPauseMills), Let the virtual machine do its own tuning .
# CMS Recyclers
-XX:+UseConcMarkSweepGC The younger generation uses CMS GC.
When this parameter is turned on, the -XX:+UseParNewGC open . namely :ParNew(Young District )+ CMS(Old District )+ Serial Old The combination of
-XX:CMSInitiatingOccupanyFraction Set the threshold for heap memory usage , Once that threshold is reached , And we started recycling .JDK5 The default values for and before are 68,DK6 And above, the default value is 92%.
If memory growth is slow , You can set a slightly larger value , A large threshold can effectively reduce CMS The trigger frequency of , Reducing the number of recycling in the old age can significantly improve application performance .
conversely , If application memory usage is growing fast , The threshold should be lowered , To avoid frequent triggering of old age serial collectors .
Therefore, this option can effectively reduce Fu1l GC Number of executions .
-XX:+UseCMSInitiatingOccupancyOnly Whether it is dynamically adjustable , send CMS Keep pressing CMSInitiatingOccupancyFraction Set the value to start
-XX:+UseCMSCompactAtFullCollection Used to specify at the end of execution Full GC After the memory space compression finishing
To avoid memory fragmentation . However, due to memory compression, the grooming process cannot be executed concurrently , The problem is that the pause is getting longer .
-XX:CMSFullGCsBeforeCompaction Set how many times to execute Full GC After the memory space compression finishing .
-XX:ParallelCMSThreads Set up CMS Number of threads .
CMS The default number of threads to start is (ParallelGCThreads+3)/4,ParallelGCThreads Is the number of threads in the younger generation of parallel collectors .
When CPU When resources are tight , suffer CMS The impact of collector threads , The performance of an application can be very poor during the garbage collection phase .
-XX:ConcGCThreads Set the number of threads for concurrent garbage collection , The default value is based on ParallelGCThreads Calculated
-XX:+CMSScavengeBeforeRemark mandatory hotspot stay cms remark Do it before the stage minor gc, For improvement remark The speed of the stage
-XX:+CMSClassUnloadingEnable If any , Enable recycle Perm District (JDK8 Before )
-XX:+CMSParallelInitialEnabled For opening CMS initial-mark The stage is marked by multi thread
Used to increase marking speed , stay Java8 Start has been turned on by default
-XX:+CMSParallelRemarkEnabled User on CMS remark The stage is marked again by multi thread , Default on
-XX:+ExplicitGCInvokesConcurrent
-XX:+ExplicitGCInvokesConcurrentAndUnloadsClasses
These two parameters are user specified hotspot Virtual reality is performing System.gc() When using CMS cycle
-XX:+CMSPrecleaningEnabled Appoint CMS Is it necessary to Pre cleaning Stage
# G1 Recyclers
-XX:+UseG1GC Manually specify the use of G1 The collector performs the memory reclaim task .
-XX:G1HeapRegionSize Set each Region Size .
The value is 2 The power of , The scope is 1MB To 32MB Between , The goal is based on the smallest Java The heap size is divided into about 2048 Regions . The default is heap memory 1/2000.
-XX:MaxGCPauseMillis Set the maximum expected GC Pause time indicator (JVM I will try my best to achieve , But there is no guarantee that ). The default value is 200ms
-XX:ParallelGCThread Set up STW when GC The value of the number of threads . The maximum setting is 8
-XX:ConcGCThreads Set the number of threads for concurrent tagging . take n Set to the number of parallel garbage collection threads (ParallelGCThreads) Of 1/4 about .
-XX:InitiatingHeapOccupancyPercent Set trigger concurrency GC Periodic Java Heap occupancy threshold . Beyond this value , It triggers GC. The default value is 45.
-XX:G1NewSizePercent The minimum percentage of the whole heap memory occupied by the new generation ( Default 5%)
-XX:G1MaxNewSizePercent The largest percentage of the whole heap memory occupied by the new generation ( Default 60%)
-XX:G1ReservePercent=10 Reserve memory area , prevent to space(Survivor Medium to District ) overflow
How to choose a garbage collector ?
Give priority to JVM The adaptive , Size the heap
Serial collector : Memory less than 100M; Single core 、 Stand alone program , And there is no pause time requirement
Parallel collector : many CPU、 High throughput 、 Allow a pause longer than 1 second
concurrent collector : many CPU、 Pursue low pause time 、 Respond quickly ( For example, the delay cannot exceed 1 second , Such as Internet applications )
The official recommendation G1, High performance . Now the Internet Project , It's basically using G1
In particular :
There is no best collector , There is no universal collector
Tuning is always for specific scenarios 、 Specific needs , There is no collector for once and for all
4.3.5. GC Log related options
-XX:+PrintGC <==> -verbose:gc Print brief log information
-XX:+PrintGCDetails Print detailed log information
-XX:+PrintGCTimeStamps Print program starts to GC When it happened , collocation -XX:+PrintGCDetails Use
-XX:+PrintGCDateStamps Print GC Time stamp when it happened , collocation -XX:+PrintGCDetails Use
-XX:+PrintHeapAtGC Print GC Front and back heap information , Here's the picture
-Xloggc:<file> Output GC In the file under the specified path
-XX:+TraceClassLoading Monitor the loading of classes
-XX:+PrintGCApplicationStoppedTime Print GC The pause time of the thread
-XX:+PrintGCApplicationConcurrentTime Apply uninterrupted execution time before printing garbage collection
-XX:+PrintReferenceGC Print how many different reference types are recycled
-XX:+PrintTenuringDistribution Print JVM In every time MinorGC After the current use of Survivor The age distribution of the subjects in the study
-XX:+UseGCLogFileRotation Enable GC Automatic dump of log files
-XX:NumberOfGCLogFiles=1 Set up GC Number of cycles for the log file
-XX:GCLogFileSize=1M Set up GC The size of the log file
4.3.6. The other parameters
-XX:+DisableExplicitGC Ban hotspot perform System.gc(), Default disabled
-XX:ReservedCodeCacheSize=<n>[g|m|k]、-XX:InitialCodeCacheSize=<n>[g|m|k] Specify the size of the code cache
-XX:+UseCodeCacheFlushing Give up some compiled code , Avoid when the code cache is full JVM Switch to interpreted-only The situation of
-XX:+DoEscapeAnalysis Open escape analysis
-XX:+UseBiasedLocking Open the bias lock
-XX:+UseLargePages Open and use the big page
-XX:+PrintTLAB Print TLAB Usage situation
-XX:TLABSize Set up TLAB size
4.4. adopt Java Code acquisition JVM Parameters
Java Provides java.lang.management Packages are used to monitor and manage Java Virtual machine and Java Other components in the runtime , It allows local or remote monitoring and management of running Java virtual machine . among ManagementFactory Class is more commonly used , in addition Runtime Class to get memory 、CPU Kernel number and other related data . By using these api, You can monitor the heap memory usage of the application server , Set some thresholds for alarm and other processing .
public class MemoryMonitor {
public static void main(String[] args) {
MemoryMXBean memorymbean = ManagementFactory.getMemoryMXBean();
MemoryUsage usage = memorymbean.getHeapMemoryUsage();
System.out.println("INIT HEAP: " + usage.getInit() / 1024 / 1024 + "m");
System.out.println("MAX HEAP: " + usage.getMax() / 1024 / 1024 + "m");
System.out.println("USE HEAP: " + usage.getUsed() / 1024 / 1024 + "m");
System.out.println("\nFull Information:");
System.out.println("Heap Memory Usage: " + memorymbean.getHeapMemoryUsage());
System.out.println("Non-Heap Memory Usage: " + memorymbean.getNonHeapMemoryUsage());
System.out.println("======================= adopt java To get the relevant system status ============================ ");
System.out.println(" Current heap memory size totalMemory " + (int) Runtime.getRuntime().totalMemory() / 1024 / 1024 + "m");// Current heap memory size
System.out.println(" Free heap memory size freeMemory " + (int) Runtime.getRuntime().freeMemory() / 1024 / 1024 + "m");// Free heap memory size
System.out.println(" Maximum available total heap memory maxMemory " + Runtime.getRuntime().maxMemory() / 1024 / 1024 + "m");// Maximum available total heap memory size
}
}
5. analysis GC journal
5.1. GC classification
in the light of HotSpot VM The implementation of the , It's inside GC According to the recycling area, it can be divided into two types : One is partial collection (Partial GC), One is the whole collection (Full GC)
Part of the collection (Partial GC): Not the whole collection Java Garbage collection . Which is divided into :
Cenozoic collection (Minor GC / Young GC): It's just a new generation (Eden / S0, S1) Garbage collection
Old age collection (Major GC / Old GC): It's just garbage collection in the old days . at present , Only CMS GC There will be separate collection of old age behavior . Be careful , A lot of times Major GC Hui He Full GC To confuse with , We need to distinguish between old age recycling and whole heap recycling .
Mixed collection (Mixed GC): Collecting garbage from the whole new generation and part of the old age . at present , Only G1 GC There's going to be this kind of behavior
Collect the whole pile (Full GC): Collect the whole java Garbage collection in the heap and method area .
5.2. GC Classification of log
about java We can configure the application through some configuration gc Print out all the logs , Then analysis gc Log to get key indicators , analysis GC reason , tuning JVM Parameters . Print GC Log method , stay JVM Add parameters to parameters ,%t Represents time .
java -jar -Xloggc:./gc-%t.log -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps -XX:+PrintGCCause -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=10 -XX:GCLogFileSize=100M microservice-eureka-server.jar
We intercepted part of JVM Just started part GC journal :
We can see the red box in the first line of the figure , Is the configuration parameter of the project . Not only is printing configured here GC journal , And related VM Memory parameters . The second line in the red box is in this GC Occurs at a point in time GC And then it's about GC situation . 1. about 2.909: This is from jvm Start the calculation to this time GC Time passed , There is also a specific occurrence time and date ahead .
Full GC(Metadata GC Threshold) This is a time full gc, In brackets is gc Why , PSYoungGen It's the younger generation GC, ParOldGen It's from the old days GC,Metaspace It's the of meta space GC.
6160K->0K(141824K), These three numbers correspond to GC It used to occupy the size of the younger generation ,GC Later, the younger generation occupied , And the whole young generation Small .
112K->6056K(95744K), These three numbers correspond to GC It used to occupy the size of the old age ,GC Later, the old age occupied , And the whole old age size .
6272K->6056K(237568K), These three numbers correspond to GC The size of the heap memory previously occupied ,GC After that, the heap memory is occupied , And the entire heap memory Size .
20516K->20516K(1069056K), These three numbers correspond to GC The size of the memory that used to occupy meta space ,GC After that, the meta space memory is occupied , And the size of the entire Metaspace memory .
0.0209707 Is the point in time GC It takes time .
It can be found several times from the log fullgc It is caused by insufficient meta space , So we can turn up the meta space , That is to say -jar Add parameters later :
‐XX:MetaspaceSize=256M ‐XX:MaxMetaspaceSize=256M
Then execute again , Let's take a look after the adjustment gc The log finds that there are no problems caused by insufficient meta space fullgc 了 .
about CMS and G1 The parameters set by the collector will be different , The output log will be a little different , You can also try to print the corresponding gc Log analysis , You can find gc In the log gc Step by step The steps we talked about before are similar .CMS Yes. :
‐Xloggc:d:/gc‐cms‐%t.log ‐Xms50M ‐Xmx50M ‐XX:MetaspaceSize=256M ‐XX:MaxMetaspaceSize=256M ‐XX:+PrintGCDetails‐XX:+P rintGCDateStamps ‐XX:+PrintGCTimeStamps ‐XX:+PrintGCCause ‐XX:+UseGCLogFileRotation ‐XX:NumberOfGCLogFiles=10 ‐XX:GCLogFileSize=100M ‐XX:+UseParNewGC ‐XX:+UseConcMarkSweepGC
G1 Yes. :
‐Xloggc:d:/gc‐g1‐%t.log ‐Xms50M ‐Xmx50M ‐XX:MetaspaceSize=256M ‐XX:MaxMetaspaceSize=256M ‐XX:+PrintGCDetails ‐XX:+Pr intGCDateStamps
‐XX:+PrintGCTimeStamps ‐XX:+PrintGCCause ‐XX:+UseGCLogFileRotation ‐XX:NumberOfGCLogFiles=10 ‐XX:GCLogFileSize=100M ‐XX:+UseG1GC
These parameters above , Can help us to view and analyze GC Garbage collection of . But if GC There are many logs , Thousands of lines . Even if you can see ten lines at a glance , It's over , The brain is also blank . So we can use some functions to help us analyze , Here's a recommended one gceasy(Universal JVM GC analyzer - Java Garbage collection log analysis made easy), Sure Upload gc file , Then he will use the visual interface to show GC situation . As shown in the figure below
Above we can see the young generation , Old age , And permanent memory allocation , And maximum usage .
In the figure above, we can see that there is GC Changes before and after , And other information . This tool also provides machine learning based JVM Intelligent optimization suggestions , Of course, now this function needs to be paid .
边栏推荐
猜你喜欢
Leetcode weekly The 280 game of the week is still difficult for the special game of the week's beauty team ~ simple simulation + hash parity count + sorting simulation traversal
Nacos 的安装与服务的注册
Navigation day answer applet: preliminary competition of navigation knowledge competition
一文搞定垃圾回收器
鏈錶之雙指針(快慢指針,先後指針,首尾指針)
[error record] file search strategy in groovy project (src/main/groovy/script.groovy needs to be used in the main function | groovy script directly uses the relative path of code)
EasyCVR集群部署如何解决项目中的海量视频接入与大并发需求?
南京:全面启用商品房买卖电子合同
Overview of Fourier analysis
點到直線的距離直線的交點及夾角
随机推荐
fibonacci search
One article deals with the microstructure and instructions of class
一文搞定垃圾回收器
First, redis summarizes the installation types
Thinkphp5.1 cross domain problem solving
Global and Chinese market of water treatment technology 2022-2028: Research Report on technology, participants, trends, market size and share
Nacos 的安装与服务的注册
Activate function and its gradient
Navigation day answer applet: preliminary competition of navigation knowledge competition
Nacos 的安装与服务的注册
The code generator has deoptimised the styling of xx/typescript. js as it exceeds the max of 500kb
50. Pow(x, n). O(logN) Sol
Nangou Gili hard Kai font TTF Download with installation tutorial
Business introduction of Zhengda international futures company
Evolution of APK reinforcement technology, APK reinforcement technology and shortcomings
Boring boring
Post-90s tester: "after joining Ali, this time, I decided not to change jobs."
[speech processing] speech signal denoising based on Matlab GUI Hanning window fir notch filter [including Matlab source code 1711]
Binary tree (II) -- code implementation of heap
【无标题】