Skip to content

Add mallocTrim command to vmTool #3030

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions arthas-vmtool/src/main/java/arthas/VmTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,16 @@ public Class<?>[] getAllLoadedClasses() {
return getAllLoadedClasses0(Class.class);
}

@Override
public int mallocTrim() {
return mallocTrim0();
}

private static synchronized native int mallocTrim0();

@Override
public boolean mallocStats() {
return mallocStats0();
}
private static synchronized native boolean mallocStats0();
}
10 changes: 10 additions & 0 deletions arthas-vmtool/src/main/java/arthas/VmToolMXBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,14 @@ public interface VmToolMXBean {
* 获取所有已加载的类
*/
public Class<?>[] getAllLoadedClasses();

/**
* glibc 释放空闲内存
*/
public int mallocTrim();

/**
* glibc 输出内存状态到应用的 stderr
*/
public boolean mallocStats();
}
25 changes: 24 additions & 1 deletion arthas-vmtool/src/main/native/src/jni-library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
#include <jvmti.h>
#include "arthas_VmTool.h" // under target/native/javah/

#ifdef __GLIBC__
#include <malloc.h>
#endif

static jvmtiEnv *jvmti;
static jlong tagCounter = 0;
Expand Down Expand Up @@ -204,4 +207,24 @@ JNIEXPORT jobjectArray JNICALL Java_arthas_VmTool_getAllLoadedClasses0
}
jvmti->Deallocate(reinterpret_cast<unsigned char *>(classes));
return array;
}
}

extern "C"
JNIEXPORT jint JNICALL Java_arthas_VmTool_mallocTrim0
(JNIEnv *env, jclass thisClass) {
#ifdef __GLIBC__
return ::malloc_trim(0);
#endif
return -1;
}

extern "C"
JNIEXPORT jboolean JNICALL Java_arthas_VmTool_mallocStats0
(JNIEnv *env, jclass thisClass) {
#ifdef __GLIBC__
::malloc_stats();
return JNI_TRUE;
#else
return JNI_FALSE;
#endif
}
12 changes: 12 additions & 0 deletions arthas-vmtool/src/test/java/arthas/VmToolTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,16 @@ public void test_interrupt_thread() throws InterruptedException {
TimeUnit.SECONDS.sleep(5);
Assert.assertEquals(("interrupted " + interruptMe.getId() + " thread success."), re[0].getMessage());
}

@Test
public void testMallocTrim() {
VmTool vmtool = initVmTool();
vmtool.mallocTrim();
}

@Test
public void testMallocStats() {
VmTool vmtool = initVmTool();
vmtool.mallocStats();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
+ " vmtool --action getInstances --classLoaderClass org.springframework.boot.loader.LaunchedURLClassLoader --className org.springframework.context.ApplicationContext\n"
+ " vmtool --action forceGc\n"
+ " vmtool --action interruptThread -t 1\n"
+ " vmtool --action mallocTrim\n"
+ " vmtool --action mallocStats\n"
+ Constants.WIKI + Constants.WIKI_HOME + "vmtool")
//@formatter:on
public class VmToolCommand extends AnnotatedCommand {
Expand Down Expand Up @@ -157,7 +159,7 @@ public void setThreadId(int threadId) {
}

public enum VmToolAction {
getInstances, forceGc, interruptThread
getInstances, forceGc, interruptThread, mallocTrim, mallocStats
}

@Override
Expand Down Expand Up @@ -239,6 +241,18 @@ public void process(final CommandProcess process) {
process.end();

return;
} else if (VmToolAction.mallocTrim.equals(action)) {
int result = vmToolInstance().mallocTrim();
process.write("\n");
process.end(result == 1 ? 0 : -1, "mallocTrim result: " +
(result == 1 ? "true" : (result == 0 ? "false" : "not supported")));
return;
} else if (VmToolAction.mallocStats.equals(action)) {
boolean result = vmToolInstance().mallocStats();
process.write("\n");
process.end(result ? 0 : -1, "mallocStats result: " +
(result ? "true" : "not supported"));
return;
}

process.end();
Expand Down
29 changes: 29 additions & 0 deletions site/docs/doc/vmtool.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,32 @@ thread id 通过`-t`参数指定,可以使用 `thread`命令获取。
```bash
vmtool --action interruptThread -t 1
```

## glibc 释放空闲内存

Linux man page: [malloc_trim](https://man7.org/linux/man-pages/man3/malloc_trim.3.html)

```bash
vmtool --action mallocTrim
```

## glibc 内存状态

内存状态将会输出到应用的 stderr。Linux man page: [malloc_stats](https://man7.org/linux/man-pages/man3/malloc_stats.3.html)

```bash
vmtool --action mallocStats
```

输出到 stderr 的内容如下:

```
Arena 0:
system bytes = 135168
in use bytes = 74352
Total (incl. mmap):
system bytes = 135168
in use bytes = 74352
max mmap regions = 0
max mmap bytes = 0
```
29 changes: 29 additions & 0 deletions site/docs/en/doc/vmtool.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,32 @@ The thread id is specified by the `-t` parameter. It can be obtained using the `
```bash
vmtool --action interruptThread -t 1
```

## glibc Release Free Memory

Linux man page: [malloc_trim](https://man7.org/linux/man-pages/man3/malloc_trim.3.html)

```bash
vmtool --action mallocTrim
```

## glibc Memory Status

The memory status will be output to the application's stderr. Linux man page: [malloc_stats](https://man7.org/linux/man-pages/man3/malloc_stats.3.html)

```bash
vmtool --action mallocStats
```

The output to stderr is as follows:

```
Arena 0:
system bytes = 135168
in use bytes = 74352
Total (incl. mmap):
system bytes = 135168
in use bytes = 74352
max mmap regions = 0
max mmap bytes = 0
```
Loading