当前位置:网站首页>JVM tuning V: JVM tuning tools and tuning practice
JVM tuning V: JVM tuning tools and tuning practice
2022-06-11 05:03:00 【Please close your eyes when it is dark】
jvm Tuning tools and tuning practices
jvm Built in common commands
JPS
jps Is used to view the hotspot Process of virtual machine id. When... Is not specified hostid when , The default view is native jvm process id
-l: Output the complete jar name
-v: Output starts the specified jvm Parameters

Jmap
This command can be used to view memory information , The number of instances and the size of occupied memory
jmap -histo 25358 # View instances generated in history
jmap -histo:live 25358 # View the currently surviving instances , During execution, a... May be triggered full gc
If there is a large amount of information, you can output it to a file to view jmap -histo 25358 > ./info.txt、less info.txt
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[][]
View heap information jmap -heap 25358
Export heap information jmap -dump:format=b,file=bx.hprof 25358 Export heap information , The exported information file can be put into the visualization tool for analysis , for example jvisualvm
When setting memory overflow, export the heap information at that time
-XX:+HeapDumpOnOutOfMemoryError
-XX:HeapDumpPath=./ ( route )
public class OOMTest {
// JVM Set up
// -Xms10M -Xmx10M -XX:+PrintGCDetails -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=D:\jvm.dump
public static void main(String[] args) {
List<Object> list = new ArrayList<>();
int i = 0;
int j = 0;
while (true) {
list.add(new User(i++, UUID.randomUUID().toString()));
new User(j--, UUID.randomUUID().toString());
}
}
}

If you want to know about memory usage , You can use this tool for analysis
Jstack
use jstack Add process id Look for deadlocks , See the following example
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");
}
}
jstack 22940
“Thread-1” The thread of
prio=5 priority =5
tid=0x00xxxxxxx Threads id
nid=0xxxxx The local thread ID corresponding to the thread nid
java.lang.Thread.State: BLOCKED Thread state
jstack Find out the occupation cpu The highest thread stack information
public class Math {
public int compute() {
// A method corresponds to a stack frame memory area
int a = 1;
int b = 2;
int c = (a + b) * 10;
return c;
}
public static void main(String[] args) {
Math math = new Math();
while (true){
math.compute();
}
}
stay Linux Pass through javac Math.java Compile , adopt java Math Run , Pay attention to the package name , It is better to remove the package name , Otherwise, the main class will not be found .
Use top Order to see Linux The state of , notice CPU100%.
top -p 94958 View the status of the specified process 
Press H, Get the memory of each thread 
Find memory and cpu The highest thread occupied pid, such as 94959
Convert to hexadecimal to get 0x172ef, This is thread id The hexadecimal representation of
perform jstack 94958|grep -A 10 0x172ef, Get thread stack information in 172ef This thread is behind the line 10 That's ok , You can see from the stack what causes cpu Skyrocketing call method 
jvisualvm Monitoring tools
Use jvisualvm When detecting the remote server , Additional parameters are required to start the service .
java -Dcom.sun.management.jmxremote.port=8888 -Djava.rmi.server.hostname=192.168.10.111 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false -jar xxx.jar
function jvisualvm



Jinfo
See what's running Java Extension parameters for the application
see jvm Parameters of operation jinfo -flags 100054
see java system parameter 
Jstat
jstat Command to view the usage of each part of heap memory , And the number of loaded classes . The format of the command is as follows :
jstat [- Command options ] [vmid] [ Time interval between ( millisecond )] [ Query times ]
Be careful : The use of jdk The version is jdk8
Garbage collection statistics jstat -gc pid The most commonly used , Can evaluate program memory usage and GC The overall situation of pressure 
S0C: survivor0 District Total size
S0U: survivor0 District Size has been used
EC:Eden The total size of
EU:Eden The size that has been used
OC: The total size of the old age
OU: The old age has used the size
MC: Meta space ( Method area ) The total size of
MU: Meta space ( Method area ) The size that has been used
CCSC: Compress class space size
CCSU: Compressed class space usage size
YGC: The project has been started up to now young GC frequency Above, 69 Time
YGCT: This happened 69 Time spent in total 0.959 second
FGC: The project has been started up to now full Gc frequency Above, 5 Time
FGCT: This happened 5 A total of 1.251 second
GCT: in total GC Time for 2.210
Heap memory statistics jstat -gccapacity 109691
NGCMN: The minimum capacity of the new generation
NGCMX: 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
OGCMN: The smallest capacity in the old days
OGCMX: 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 jstat -gcnew 109691
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
New generation memory statistics jstat -gcnewcapacity 109691
NGCMN: The minimum capacity of the new generation
NGCMX: The largest capacity of the new generation
NGC: Current Cenozoic capacity
S0CMX: The greatest survival 1 Area size
S0C: The current survival 1 Area size
S1CMX: The greatest survival 2 Area size
S1C: The current survival 2 Area size
ECMX: The largest Eden Park
EC: The current size of Eden Park
YGC: Garbage collection times of young generation
FGC: Recovery times of the elderly
Old age garbage collection statistics jstat -gcold 109691
MC: Method area size
MU: Method area usage size
CCSC: Compress class space size
CCSU: Compressed class space usage size
OC: Old age size
OU: Old age use size
YGC: Garbage collection times of young generation
FGC: Recycling times in the old days
FGCT: Garbage collection used time in the old days
GCT: The total time spent in garbage collection
Memory statistics in the old days jstat -gcoldcapacity 109691
OGCMN: The smallest capacity in the old days
OGCMX: The biggest capacity in the old days
OGC: The current size of the elderly
OC: Old age size
YGC: Garbage collection times of young generation
FGC: Recycling times in the old days
FGCT: Garbage collection used time in the old days
GCT: The total time spent in garbage collection
In addition to the above use, it can also be executed at intervals jstat -gc 109691 1000 10 1000 Represents milliseconds Namely To perform a second 1 Time , A total of execution 10 Time
You can also export to a file for subsequent viewing jstat -gc 109691 1000 10 >> aa.txt
JVM Operation forecast
use jstat gc -pid Command can calculate the following key data , With this data, you can optimize , 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 ), 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 .
Optimization idea
In fact, simply put, try to make every time 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 the old age . Try to reduce Full GC The frequency of , Avoid frequent Full GC Yes JVM The impact of performance .
Alibaba Arthas Introduction
brief introduction
Arthas yes Alibaba stay 2018 year 9 Open source in Java Diagnostic tools . Support JDK6+, Use command line interaction mode , It is convenient to locate and diagnose online program running problems .Arthas Official documents are very detailed , See :https://alibaba.github.io/arthas
** Arthas Use scenarios **
Thanks to the Arthas Powerful and rich features , Give Way Arthas What can be done is beyond imagination . Here are just a few common uses , More usage scenarios can be familiar with Arthas Then explore for yourself .
- Is there a global perspective to see the health of the system ?
- Why? CPU It's going up again , Where did it take up CPU ?
- Does the multithread running have a deadlock ? Is there a jam ?
- The program takes a long time to run , Where does it take longer ? How to monitor ?
- 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 ?
- Is there any way to monitor JVM The real-time running state of ?
Arthas Start installation
# github download arthas
wget https://alibaba.github.io/arthas/arthas-boot.jar
# perhaps Gitee download
wget https://arthas.gitee.io/arthas-boot.jar
# If the download fails, you can go to Arthas Official download full package decompression Then it's running arthas-boot.jar

Test case code
package com.jvm;
public class Arthas {
private static HashSet hashSet = new HashSet();
public static void main(String[] args) {
// simulation CPU Too high
cpuHigh();
// Simulate thread deadlock
deadThread();
// Keep going to hashSet Collection adds data
addHashSetThread();
}
/** * Keep going to hashSet Set add data */
public static void addHashSetThread() {
// Initializing constants
new Thread(() -> {
int count = 0;
while (true) {
try {
hashSet.add("count" + count);
Thread.sleep(1000);
count++;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
public static void cpuHigh() {
new Thread(() -> {
while (true) {
}
}).start();
}
/** * Deadlock */
private static void deadThread() {
/** Create resources */
Object resourceA = new Object();
Object resourceB = new Object();
// Create thread
Thread threadA = new Thread(() -> {
synchronized (resourceA) {
System.out.println(Thread.currentThread() + " get ResourceA");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread() + "waiting get resourceB");
synchronized (resourceB) {
System.out.println(Thread.currentThread() + " get resourceB");
}
}
});
Thread threadB = new Thread(() -> {
synchronized (resourceB) {
System.out.println(Thread.currentThread() + " get ResourceB");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread() + "waiting get resourceA");
synchronized (resourceA) {
System.out.println(Thread.currentThread() + " get resourceA");
}
}
});
threadA.start();
threadB.start();
}
}
Pay attention to the package name when running , Otherwise, the main class will not be found . My side is package com.jvm; stay Linux To create a mkdir -p com/jvm adopt javac Arthas.java compile become .class file And then it's running java com/jvm/Arthas
adopt top Command view 
Does cause cpu Gao Sheng .
Next through Arthas solve the problem
function Arthas java -jar arthas-boot.jar
Press 1 Enter the process project 
Arthas Common commands
help
Into success adopt help View related commands 
dashboard
dashboard: The dashboard 
You can see through the dashboard cpu High occupancy and thread blocking
thread
thread: View Thread details
First of all to see cpu High thread 
According to the information find com.jvm.Arahas Under this category cpuHigh Method Look at the corresponding code , So as to solve cpu High problem thread -b You can view thread deadlocks 
Then, according to the information, the deadlock problem is solved
jad
Input jad Add the full name of the class Can decompile , This makes it easy for us to check whether the online code is the correct version jad com.jvm.Arthas
ognl
Arthas 3.0 Use in ognl The expression replaces groovy To implement the evaluation function of the expression , It's solved groovy Potential memory leaks . Flexible use of ognl expression , It can greatly improve the efficiency of troubleshooting .ognl @[email protected]: View the values in the set 
ognl '@[email protected]("testabc123")' : Add values to the collection 
More commands can be used help Command view , Or view the document :https://alibaba.github.io/arthas/commands.html#arthas
边栏推荐
- The solution "no hardware is configured for this address and cannot be modified" appears during botu simulation
- Huawei equipment configuration MCE
- 免费数据 | 新库上线 | CnOpenData全国文物商店及拍卖企业数据
- What is the difference between gigabit network card and 10 Gigabit network card?
- MySQL nested sorting: first sort and filter the latest data, and then customize the sorting of this list
- ROS compilation error: could not find a package configuration file provided by "XXX“
- 力扣(LeetCode)161. 相隔为 1 的编辑距离(2022.06.10)
- Zed2 camera calibration -- binocular, IMU, joint calibration
- Leetcode 161 Editing distance of 1 (2022.06.10)
- Take stock of the AI black technologies in the Beijing Winter Olympic Games, and Shenzhen Yancheng Technology
猜你喜欢

Crmeb/v4.4 Standard Version open version mall source code applet official account h5+app mall source code

NVIDIA SMI has failed because it could't communicate with the NVIDIA driver

Powerful new UI installation force artifact wechat applet source code + multiple templates support multiple traffic main modes
![[Transformer]On the Integration of Self-Attention and Convolution](/img/64/59f611533ebb0cc130d08c596a8ab2.jpg)
[Transformer]On the Integration of Self-Attention and Convolution

Differences between the four MQ

Top 100 video information of station B

Paper recommendation: relicv2, can the new self supervised learning surpass supervised learning on RESNET?
![[Transformer]MViTv1:Multiscale Vision Transformers](/img/de/1f2751f08dbf4ea3f77a403365dbda.jpg)
[Transformer]MViTv1:Multiscale Vision Transformers

Writing a good research title: Tips & Things to avoid

How to quickly find the official routine of STM32 Series MCU
随机推荐
Cartographer learning records: 3D slam part of cartographer source code (I)
Huawei equipment is configured with bgp/mpls IP virtual private network
Writing a good research title: Tips & Things to avoid
KD-Tree and LSH
jvm调优五:jvm调优工具和调优实战
Ican uses fast r-cnn to get an empty object detection result file
Codesys get System Time
Use pathlib instead of OS and os Common methods of path
Simple knowledge distillation
Leetcode question brushing series - mode 2 (datastructure linked list) - 24 (m): swap nodes in pairs exchange nodes in the linked list
Possible errors during alphapose installation test
The solution "no hardware is configured for this address and cannot be modified" appears during botu simulation
[markdown syntax advanced] make your blog more exciting (III: common icon templates)
Tips and websites for selecting papers
C语言试题三(语法选择题——含知识点详解)
2021 iccv paper sharing - occlusion boundary detection
免费数据 | 新库上线 | CnOpenData全国文物商店及拍卖企业数据
华为设备配置本地虚拟专用网互访
IOU series (IOU, giou, Diou, CIO)
Holiday Homework