Skip to content

Commit

Permalink
vmtool command support forceGc action. alibaba#1781
Browse files Browse the repository at this point in the history
  • Loading branch information
hengyunabc committed Apr 30, 2021
1 parent ac27ea3 commit 3499a30
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 10 deletions.
7 changes: 7 additions & 0 deletions arthas-vmtool/src/main/java/arthas/VmTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public static synchronized VmTool getInstance(String libPath) {
*/
private static native String check0();

private static native void forceGc0();

/**
* 获取某个class在jvm中当前所有存活实例
*/
Expand Down Expand Up @@ -78,6 +80,11 @@ public String check() {
return check0();
}

@Override
public void forceGc() {
forceGc0();
}

@Override
public <T> T[] getInstances(Class<T> klass) {
return getInstances0(klass);
Expand Down
5 changes: 5 additions & 0 deletions arthas-vmtool/src/main/java/arthas/VmToolMXBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ public interface VmToolMXBean {
*/
public String check();

/**
* https://docs.oracle.com/javase/8/docs/platform/jvmti/jvmti.html#ForceGarbageCollection
*/
public void forceGc();

/**
* 获取某个class在jvm中当前所有存活实例
*/
Expand Down
8 changes: 8 additions & 0 deletions arthas-vmtool/src/main/native/include/arthas_VmTool.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 13 additions & 6 deletions arthas-vmtool/src/main/native/src/jni-library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@ JNIEXPORT jclass JNICALL getClass(JNIEnv *env) {
return cachedClass;
}

extern "C"
JNIEXPORT jstring JNICALL
Java_arthas_VmTool_check0(JNIEnv *env, jclass thisClass) {
return env->NewStringUTF("OK");
}

extern "C"
jvmtiEnv *getJvmtiEnv(JNIEnv *env) {

Expand All @@ -36,6 +30,19 @@ jvmtiEnv *getJvmtiEnv(JNIEnv *env) {
return jvmti;
}

extern "C"
JNIEXPORT void JNICALL
Java_arthas_VmTool_forceGc0(JNIEnv *env, jclass thisClass) {
jvmtiEnv *jvmti = getJvmtiEnv(env);
jvmti->ForceGarbageCollection();
}

extern "C"
JNIEXPORT jstring JNICALL
Java_arthas_VmTool_check0(JNIEnv *env, jclass thisClass) {
return env->NewStringUTF("OK");
}

extern "C"
jobject createJavaInstance(JNIEnv *env, jclass javaClass) {
//找到java类的构造方法
Expand Down
4 changes: 2 additions & 2 deletions arthas-vmtool/src/test/java/arthas/VmToolTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void testIsSnapshot() {
System.out.println("sum size->" + vmtool.sumInstanceSize(VmTool.class));
beforeInstances = null;

System.gc();
vmtool.forceGc();
Thread.sleep(100);
System.out.println(weakReference1.get() + " " + weakReference2.get());
VmTool[] afterInstances = vmtool.getInstances(VmTool.class);
Expand Down Expand Up @@ -80,7 +80,7 @@ public void testGetInstancesMemoryLeak() {
totalTime.addAndGet(cost);
System.out.println(i + " instance size:" + (instances == null ? 0 : instances.length) + ", cost " + cost + "ms avgCost " + totalTime.doubleValue() / i + "ms");
instances = null;
System.gc();
vmtool.forceGc();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
@Summary("jvm tool")
@Description(Constants.EXAMPLE
+ " vmtool --action getInstances --className demo.MathGame\n"
+ " vmtool --action getInstances --className demo.MathGame --express 'instances.size()'\n"
+ " vmtool --action getInstances --className demo.MathGame --express 'instances.length'\n"
+ " vmtool --action forceGc\n"
+ Constants.WIKI + Constants.WIKI_HOME + "vmtool")
//@formatter:on
public class VmToolCommand extends AnnotatedCommand {
Expand Down Expand Up @@ -123,7 +124,7 @@ public void setExpress(String express) {
}

public enum VmToolAction {
getInstances, load
getInstances, forceGc, load
}

@Override
Expand Down Expand Up @@ -178,8 +179,14 @@ public void process(final CommandProcess process) {
}

process.write(new ObjectView(value, this.expand).draw());
process.write("\n");
process.end();
}
} else if (VmToolAction.forceGc.equals(action)) {
vmToolInstance().forceGc();
process.write("\n");
process.end();
return;
}

process.end();
Expand Down
Binary file modified lib/libArthasJniLibrary-x64.dylib
Binary file not shown.
7 changes: 7 additions & 0 deletions spy/src/main/java/arthas/VmTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public static synchronized VmTool getInstance(String libPath) {
*/
private static native String check0();

private static native void forceGc0();

/**
* 获取某个class在jvm中当前所有存活实例
*/
Expand Down Expand Up @@ -78,6 +80,11 @@ public String check() {
return check0();
}

@Override
public void forceGc() {
forceGc0();
}

@Override
public <T> T[] getInstances(Class<T> klass) {
return getInstances0(klass);
Expand Down
5 changes: 5 additions & 0 deletions spy/src/main/java/arthas/VmToolMXBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ public interface VmToolMXBean {
*/
public String check();

/**
* https://docs.oracle.com/javase/8/docs/platform/jvmti/jvmti.html#ForceGarbageCollection
*/
public void forceGc();

/**
* 获取某个class在jvm中当前所有存活实例
*/
Expand Down

0 comments on commit 3499a30

Please sign in to comment.