当前位置:网站首页>[100 cases of JVM tuning practice] 03 -- four cases of JVM heap tuning
[100 cases of JVM tuning practice] 03 -- four cases of JVM heap tuning
2022-07-02 19:00:00 【Half old 518】
front said
Author's brief introduction : Half old 518, Long distance runner , Determined to persist in writing 10 Blog of the year , Focus on java Back end
Column Introduction : Case driven introduction JVM knowledge , Teach you how to use JVM Troubleshooting 、 Evaluation code 、 Optimize performance
The article brief introduction : Introduce the related concepts of heap 、 Teach you to check 5 A common JVM Pile of cases
List of articles
6. Pile up
6.1 Characteristics of reactor
Use new All objects created by keyword will use heap .
characteristic :
- Thread sharing , The objects in the heap need to consider thread safety .
- There's a garbage collection mechanism .
6.2 Heap memory overflow problem
There is a garbage collection mechanism in the heap , But the premise of garbage collection is that the objects in the heap are no longer referenced ( actually , The algorithm for recycling references is the root reachable Algorithm , I'll talk about it later , The statement here is inaccurate ), So if we have too many objects that cannot be recycled , It may cause memory overflow .
public class MemoryOverFlow {
public static void main(String[] args) {
int i = 0;
String a = "hello";
List list = new ArrayList(); // until catch Code block execution , Has been used
try {
while (true) {
list.add(a);
a = a + a;
i++;
}
} catch (Throwable e) {
// Use Throwable, If you use Exception I can't wrap it up Error,i Can't be printed out
e.printStackTrace();
System.out.println(i);
}
}
}
appear OutOfMemoryError
java.lang.OutOfMemoryError: Overflow: String length out of range
at java.base/java.lang.StringConcatHelper.checkOverflow(StringConcatHelper.java:57)
at java.base/java.lang.StringConcatHelper.mix(StringConcatHelper.java:138)
at java.base/java.lang.StringConcatHelper.simpleConcat(StringConcatHelper.java:420)
at MemoryOverFlow.main(MemoryOverFlow.java:12)
28
another : Parameters -Xmx
You can set jvm Memory size , When troubleshooting heap memory problems, you can set it to be small ( Such as 8m), More likely to expose memory overflow problems . Setup method : Click on build and run Go with you modify options->add vm options.
6.3 Evaluation of the impact of code memory performance
I wrote a piece of code in my work , How to judge the impact of a piece of code on memory performance ? You can use the following tools .
jps
See what the system has java processjmap
Check the occupation of heap memory at a certain timejconsole
Multifunctional real-time monitoring tool
Through the following demo To demonstrate .
public class jvmdemo {
public static void main(String[] args) throws InterruptedException {
System.out.println("1....."); // Output hint , Easy to carry out Heap Dump
Thread.sleep(60000); // to 30s Time is used for Heap Dump
byte [] arr = new byte[1024 * 1024 * 10];
System.out.println("2.......");
Thread.sleep(60000);
arr = null;
System.gc();
System.out.println("3......");
Thread.sleep(100000L);
}
}
When the output 1… after , Execute first jps
see jvmdemo Corresponding pid
tip: If you are windows System ,jps No results returned , You can refer to the blog
give the result as follows .
perform jmap -heap xxx(pid)
Check the heap memory usage at this time .
tip:
If you execute jmap Report errors .
Error: -heap option used Cannot connect to core dump or remote debug server. Use jhsdb jmap instead
Because jdk8 Later versions, previous
jmap -heap xxx(pid)
The command can no longer be used . You can use the command insteadjhsdb jmap --heap --pid xxx
.
In the prompt message output 1,2,3 After three operations, the results are as follows .
Heap Usage:
G1 Heap:
regions = 2034
capacity = 8531214336 (8136.0MB)
used = 0 (0.0MB)
free = 8531214336 (8136.0MB)
0.0% used
G1 Young Generation:
Eden Space:
regions = 0
capacity = 29360128 (28.0MB)
used = 0 (0.0MB)
free = 29360128 (28.0MB)
0.0% used
Survivor Space:
regions = 0
capacity = 0 (0.0MB)
used = 0 (0.0MB)
free = 0 (0.0MB)
0.0% used
G1 Old Generation:
regions = 0
capacity = 507510784 (484.0MB)
used = 0 (0.0MB)
free = 507510784 (484.0MB)
0.0% used
Heap Usage:
G1 Heap:
regions = 2034
capacity = 8531214336 (8136.0MB)
used = 12582912 (12.0MB)
free = 8518631424 (8124.0MB)
0.14749262536873156% used
G1 Young Generation:
Eden Space:
regions = 0
capacity = 29360128 (28.0MB)
used = 0 (0.0MB)
free = 29360128 (28.0MB)
0.0% used
Survivor Space:
regions = 0
capacity = 0 (0.0MB)
used = 0 (0.0MB)
free = 0 (0.0MB)
0.0% used
G1 Old Generation:
regions = 3
capacity = 507510784 (484.0MB)
used = 12582912 (12.0MB)
free = 494927872 (472.0MB)
2.479338842975207% used
Heap Usage:
G1 Heap:
regions = 2034
capacity = 8531214336 (8136.0MB)
used = 673872 (0.6426544189453125MB)
free = 8530540464 (8135.357345581055MB)
0.007898898954588403% used
G1 Young Generation:
Eden Space:
regions = 0
capacity = 8388608 (8.0MB)
used = 0 (0.0MB)
free = 8388608 (8.0MB)
0.0% used
Survivor Space:
regions = 0
capacity = 0 (0.0MB)
used = 0 (0.0MB)
free = 0 (0.0MB)
0.0% used
G1 Old Generation:
regions = 1
capacity = 8388608 (8.0MB)
used = 673872 (0.6426544189453125MB)
free = 7714736 (7.3573455810546875MB)
8.033180236816406% used
Focus on uesd This one , You can see the changing process of memory in the code , here JVM version yes 16.0.2+7-67, Different versions may vary slightly .
Use jconsole Data can be observed in real time , And it's not just about observing local processes , You can also observe remote processes .
The method is simple to use . Enter... At the command terminal jconsole It will pop up automatically .
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-27AjN2Zs-1656678910764)(F:/%E5%8D%9A%E5%AE%A2%E5%9B%BE%E7%89%87/jvm/image-20220630213435635.png)]
The observations of the above code are as follows .
summary :
The above is for troubleshooting , We add log as well as sleep The way , Help us with dump Grasp of time , Similar methods can be used in actual production .
Except for memory ,jconsole You can also monitor threads 、cpu Occupancy rate and the number of classes change .
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-J3O31REj-1656678910765)(F:/%E5%8D%9A%E5%AE%A2%E5%9B%BE%E7%89%87/jvm/image-20220630214317498.png)]
It can also help us detect deadlocks .
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-98hyGDJs-1656678910765)(F:/%E5%8D%9A%E5%AE%A2%E5%9B%BE%E7%89%87/jvm/image-20220630214429419.png)]
6.4 The memory occupation of multiple garbage collection is still very high, and the troubleshooting of the problem
jvisualvm It's also a visualization tool , Than jconsole Better to use .
The method is simple to use , Use command which java
Find the java The installation path , And switch to this path , Then execute on the corresponding path jvisualvm
that will do .
In the high version JDK( Greater than 1.8 Or later updated 1.8 edition ) It will no longer be automatically integrated in . The reference blog can download the independent version :JDK There is no higher version VisualVM_ Dongli Jike's blog -CSDN Blog .
Now use it to demonstrate , The memory occupation of multiple garbage collection is still very high, and the troubleshooting of the problem
/** * Show me how many objects to view Heap dump dump */
public class Demo1_13 {
public static void main(String[] args) throws InterruptedException {
List<Student> students = new ArrayList<>();
for (int i = 0; i < 200; i++) {
students.add(new Student());
// Student student = new Student();
}
Thread.sleep(1000000000L);
}
}
class Student {
private byte[] big = new byte[1024*1024];
}
Use jvisualvm Click to select the corresponding process , You can see that the heap memory is very high , Carry out garbage collection .
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-gCeFjrEJ-1656678910765)(F:/%E5%8D%9A%E5%AE%A2%E5%9B%BE%E7%89%87/jvm/image-20220630215736401.png)]
Memory has not been reduced much .
Conduct Heap Dump
, Select the right menu bar , According to the occupied memory class Sort
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-OXUm6CVo-1656678910766)(F:/%E5%8D%9A%E5%AE%A2%E5%9B%BE%E7%89%87/jvm/image-20220630215949114.png)]
According to the occupied memory class The sorting results are as follows .
Click the class to see all instances of the corresponding class , We click on the one that occupies the most memory ArrayList
Check out the examples , Click elementData
You can see the details .
The discovery may be student The pot , You can see some examples , Confirm the conclusion .
Point again student object , Navigate to the property big. It is found that an attribute takes about 1M Space . I can't stand it
Then locate the corresponding source code .
/** * Show me how many objects to view Heap dump dump */
public class Demo1_13 {
public static void main(String[] args) throws InterruptedException {
List<Student> students = new ArrayList<>();
for (int i = 0; i < 200; i++) {
students.add(new Student());
// Student student = new Student();
}
Thread.sleep(1000000000L);
}
}
class Student {
private byte[] big = new byte[1024*1024];
}
Sure enough , We have 200 individual Student object , Holds the memory footprint 1M Of big example 200 individual , And has been living .
边栏推荐
- Introduction to sap s/4hana OData mock service
- How can retail enterprises open the second growth curve under the full link digital transformation
- The difference between SLC, MLC, TLC and QLC NAND SSD: which is better?
- 科技公司不同人对Bug的反应 | 每日趣闻
- 问题包含哪些环节
- Simulateur nightGod + application de test de capture de paquets Fiddler
- 徹底搞懂基於Open3D的點雲處理教程!
- Which securities company has a low, safe and reliable online account opening commission
- LightGroupButton* sender = static_cast<LightGroupButton*>(QObject::sender());
- 300+ documents! This article explains the latest progress of multimodal learning based on transformer
猜你喜欢
Web version 3D visualization tool, 97 things programmers should know, AI frontier paper | information daily # 2022.07.01
故障排查:kubectl报错ValidationError: unknown field \u00a0
The difference between SLC, MLC, TLC and QLC NAND SSD: which is better?
呆错图床系统源码图片CDN加速与破J防盗链功能
LightGroupButton* sender = static_ cast<LightGroupButton*>(QObject::sender());
Distance measurement - Jaccard distance
Stratégie touristique d'été de Singapour: un jour pour visiter l'île de San taosha à Singapour
Industrial software lecture - core technology analysis of 3D CAD design software - the second lecture of the Forum
鸿蒙第四次学习
科技公司不同人对Bug的反应 | 每日趣闻
随机推荐
IPtable port redirection masquerade[easy to understand]
故障排查:kubectl报错ValidationError: unknown field \u00a0
How to set vscode to delete the whole line shortcut key?
怎么用ps提取图片颜色分析色彩搭配
R语言dplyr包na_if函数把向量数值中的控制转化为缺失值NA、按照映射规则把指定内容转化为缺失值NA
LightGroupButton* sender = static_ cast<LightGroupButton*>(QObject::sender());
Redis(7)----数据库与过期键
@Component cannot get Dao layer
学生抖音宣传母校被吐槽“招生减章”,网友:哈哈哈哈哈哈
Deep neural network Summary
文字编辑器 希望有错误的句子用红色标红,文字编辑器用了markdown
M2dgr: slam data set of multi-source and multi scene ground robot (ICRA 2022)
科技公司不同人对Bug的反应 | 每日趣闻
Leetcode (154) -- find the minimum value II in the rotation sort array
SQL training 2
The difference between promise and observable
谷歌官方回应:我们没有放弃TensorFlow,未来与JAX并肩发展
任职 22 年,PowerShell 之父将从微软离职:曾因开发 PowerShell 被微软降级过
Chain game system development (unity3d chain game development details) - chain game development mature technology source code
Leetcode interview question 17.04 Vanishing numbers