Skip to content

Commit 67a659e

Browse files
authored
feat: vmtool suppor heap analyzer. #1915 (#3122)
1 parent ccbc933 commit 67a659e

File tree

10 files changed

+1362
-17
lines changed

10 files changed

+1362
-17
lines changed

arthas-vmtool/pom.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@
133133
<directory>src/main/native/src</directory>
134134
<fileNames>
135135
<fileName>jni-library.cpp</fileName>
136+
<fileName>heap_analyzer.c</fileName>
136137
</fileNames>
137138
</source>
138139
</sources>
@@ -218,6 +219,7 @@
218219
<directory>src/main/native/src</directory>
219220
<fileNames>
220221
<fileName>jni-library.cpp</fileName>
222+
<fileName>heap_analyzer.c</fileName>
221223
</fileNames>
222224
</source>
223225
</sources>
@@ -283,4 +285,4 @@
283285
<scope>test</scope>
284286
</dependency>
285287
</dependencies>
286-
</project>
288+
</project>

arthas-vmtool/src/main/java/arthas/VmTool.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,16 @@ public static synchronized VmTool getInstance(String libPath) {
6767
*/
6868
private static synchronized native Class<?>[] getAllLoadedClasses0(Class<?> klass);
6969

70+
/**
71+
* 分析堆内存占用最大的对象与类。
72+
*/
73+
private static synchronized native String heapAnalyze0(int classNum, int objectNum);
74+
75+
/**
76+
* 分析指定类实例的引用回溯链。
77+
*/
78+
private static synchronized native String referenceAnalyze0(Class<?> klass, int objectNum, int backtraceNum);
79+
7080
@Override
7181
public void forceGc() {
7282
forceGc0();
@@ -128,4 +138,14 @@ public boolean mallocStats() {
128138
return mallocStats0();
129139
}
130140
private static synchronized native boolean mallocStats0();
141+
142+
@Override
143+
public String heapAnalyze(int classNum, int objectNum) {
144+
return heapAnalyze0(classNum, objectNum);
145+
}
146+
147+
@Override
148+
public String referenceAnalyze(Class<?> klass, int objectNum, int backtraceNum) {
149+
return referenceAnalyze0(klass, objectNum, backtraceNum);
150+
}
131151
}

arthas-vmtool/src/main/java/arthas/VmToolMXBean.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,23 @@ public interface VmToolMXBean {
6767
* glibc 输出内存状态到应用的 stderr
6868
*/
6969
public boolean mallocStats();
70+
71+
/**
72+
* 分析堆内存占用最大的对象与类(从 GC Root 可达对象出发)。
73+
*
74+
* @param classNum 需要展示的类数量
75+
* @param objectNum 需要展示的对象数量
76+
* @return 分析结果文本
77+
*/
78+
public String heapAnalyze(int classNum, int objectNum);
79+
80+
/**
81+
* 分析某个类的实例对象,并输出占用最大的若干对象及其引用回溯链(从对象回溯到 GC Root)。
82+
*
83+
* @param klass 目标类
84+
* @param objectNum 需要展示的对象数量
85+
* @param backtraceNum 回溯层数,-1 表示一直回溯到 root,0 表示不输出引用链
86+
* @return 分析结果文本
87+
*/
88+
public String referenceAnalyze(Class<?> klass, int objectNum, int backtraceNum);
7089
}

0 commit comments

Comments
 (0)