JVM actual combat in the project

I don’t know if you know the JVM-related parameters in your project, such as the amount of heap memory set, how much memory for the new generation, what garbage collector, GC frequency, etc., so this article explores the JVM in the project from the actual parameter.

1. Preparation for the preparation of
memory analysis tools :
there are many memory analysis tools, such as IBM's HeapAnalyzer, eclipse's MAT,
here use the eclipse MAT
official download address

常用命令:
top  查看监控整体信息
jps  可以查看部署的Java Pid
jinfo 可以查看jvm相关参数
jstat 可以查看gc相关信息,比如gc次数,新生代,老年代占比等等
jmap  用于查看堆信息,当然也可以导出dump文件

这些都是命令行工具,当然也有可视化工具
jconsole,jvisualvm

2. Start directly.
Since our production environment does not allow operation, we retreat to use the test environment for operation.
First of all, I found that our young gc is more frequent, about once every 1 minute; the number of full gc is relatively low. The condition for triggering young gc is that the eden area is full, so I want to know why the eden area is full so soon. You can see that the number of YGC times has been 1061 times. The
Insert picture description here
meaning of the parameters will be explained below.

① use top or jps command to locate the Java process Pid
top -b find pid 35 the Java
Insert picture description here
jps 35
Insert picture description here
② use jinfo check out our jvm information, you can see the new generation, the old era, the garbage collector types, etc.
jinfo -flags 35
Insert picture description here

我们一般用下列参数来初始化jvm参数
-Xms 最小堆内存
-Xmx 最大堆内存
-Xmn 新生代内存=-XX:newSize = -XX:MaxnewSize = -Xmn.也就是说-Xmn会同时设置前面2个参数
-XX:+UseXXGC 表示使用哪种GC
-XX:PermSize 设置永久代最小空间大小。
-XX:MaxPermSize 设置永久代最大空间大小。
-Xss 设置每个线程的堆栈大小。
-XX:NewRatio=n:设置年轻代和年老代的比值。如:3,表示年轻代与年老代比值为13,年轻代占整个年轻代年老代和的1/4
-XX:SurvivorRatio=n:年轻代中Eden区与两个Survivor区的比值。注意Survivor区有两个。如:3,表示Eden:Survivor=32,一个Survivor区占整个年轻代的1/5

③ use jstat View gc information
jstat -gcutil 35 information to show the percentage of the heap
Insert picture description here

各参数含义:
S0   survivor区
S1   survivor区
E    eden区
O    老年代区
M    jdk1.8是元空间 1.7是P 永久代
CCS  压缩空间
YGC  young gc 次数
YGCT young gc 耗时
FGC  full gc 次数
FGCT full gc 耗时
GCT  gc总耗时

This can refer to
④ use jmap Export stack information
jmap -dump: format = b, file = heap.hprof 35
Insert picture description here
⑤ open just downloaded MAT tools, jmap upper left corner of the file exported file into heap.hprof MAT tools
Insert picture description here
here we focus on leak The suspects are very intimate. We have found out the possible memory leaks for us, click in and have a look.
Insert picture description here
It can be seen that the 26.47% of the memory above is the object of my coding. This class is the monitoring class of the whole link, which is used to generate interception objects. You can guess that the whole link is accessed by Java probes. Similar to a proxy, we use interceptor interception to collect our request information before and after our request, and then after the request is complete, we can recycle the interceptor object, so we can see that our eden area is very slow due to the creation of interceptor objects, and then again Reclaimed by ygc.
Of course, the actual investigation process is not so simple, usually the hidden level is very deep, this is to use other features.

For example, Dominator Tree: You can see the proportion of memory occupied by objects, and we can check those large objects
Insert picture description here
first. You can see
the meaning of Shallow Heap and Retained Heap in the figure above :
Shallow Heap: The memory occupied by the object itself size, which does not contain the object reference
Retained Heap: the size of the current object + current object can be referenced directly or indirectly to the total size of the object. (The meaning of indirect reference: A->B->C, C is an indirect reference), and exclude objects directly or indirectly referenced by GC Roots. For
specific differences, please refer to this article:
Add link description
and this object was released by gc Memory is Retained Heap

Then click on the object to click on list objetcs incoming and outgoing references to analyze
incoming references: the object that refers to the object
outgoing references: the object that the object refers to. For
example, A and B refer to C, C refers to D, and E is
from the perspective of C, and AB is C. incoming references, DE is outgoing references of C

Insert picture description here
We are looking at outgoing references, that is, the object referenced by the object. You can see that there is an array, which is full of interceptor objects, which can confirm that the
Insert picture description here
general process we just said is like this. You don’t know how to search, try more .

Guess you like

Origin blog.csdn.net/u010857795/article/details/112943756