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

Conversation

kkHAIKE
Copy link

@kkHAIKE kkHAIKE commented May 23, 2025

malloc_trim 是排查 java 64M 问题的重要函数,参考 https://juejin.cn/post/6854573220733911048

CLOSE: #3020

@CLAassistant
Copy link

CLAassistant commented May 23, 2025

CLA assistant check
All committers have signed the CLA.

@kkHAIKE kkHAIKE changed the title Add mallocTrim command to vmTool [WIP]Add mallocTrim command to vmTool May 23, 2025
@kkHAIKE kkHAIKE changed the title [WIP]Add mallocTrim command to vmTool Add mallocTrim command to vmTool May 23, 2025
@hengyunabc
Copy link
Collaborator

缺少测试,需要增加基本的能调用成功的测试。不一定要构造出对应的场景。比如在 linux 下跑测试时要调用成功。

@kkHAIKE
Copy link
Author

kkHAIKE commented May 26, 2025

根据 malloc_trim 的官方文档 https://man7.org/linux/man-pages/man3/malloc_trim.3.html
-> 有内存释放才会返回 true

而且 trim_native_heap 的实现 https://github.com/openjdk/jdk21/blob/jdk-21%2B35/src/hotspot/os/linux/os_linux.cpp#L5518
-> 只要是 glibc 就会返回 true

所以如果要补充单元测试的话,可能只有直接调用,并不能检测结果是否 true.

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

另外: 非 glibc 的 mallocTrim 返回 false 是否更合适

@hengyunabc
Copy link
Collaborator

看起来,好像是 mallocTrim 函数要返回两个信息: 是否有支持,是否有成功。
考虑增加一个 isMallocTrimSupported 的函数?

@kkHAIKE
Copy link
Author

kkHAIKE commented Jun 4, 2025

可以把返回改为 int,然后 command 层去适配输出。。

主要我是觉得单元测试不好写,只能是像上个回复一样简单的调用,还有必要写吗

@hengyunabc
Copy link
Collaborator

可以把返回改为 int,然后 command 层去适配输出。。

主要我是觉得单元测试不好写,只能是像上个回复一样简单的调用,还有必要写吗

单元测试的作用主要是确保编译为 so 之后,在java层能正常调用。功能是否正常可以先不管。

另外需要同步更新下 wiki 文档。

@hengyunabc hengyunabc requested a review from Copilot June 8, 2025 07:08
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds two new commands, mallocTrim and mallocStats, to vmTool to help diagnose Java memory issues as described in the linked issue and reference article.

  • Updated English and Chinese documentation with usage examples.
  • Extended VmToolCommand, VmToolMXBean, and VmTool classes to support new actions.
  • Added native JNI implementations and test cases for the new commands.

Reviewed Changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated no comments.

Show a summary per file
File Description
site/docs/en/doc/vmtool.md Added commands and usage examples for mallocTrim and mallocStats in English.
site/docs/doc/vmtool.md Added commands and usage examples for mallocTrim and mallocStats in Chinese.
core/src/main/java/com/taobao/arthas/core/command/monitor200/VmToolCommand.java Updated command help text, enum, and process() method to include new actions.
arthas-vmtool/src/test/java/arthas/VmToolTest.java Added basic tests for mallocTrim and mallocStats.
arthas-vmtool/src/main/native/src/jni-library.cpp Added JNI implementations for mallocTrim and mallocStats.
arthas-vmtool/src/main/java/arthas/VmToolMXBean.java Updated interface to include mallocTrim and mallocStats methods.
arthas-vmtool/src/main/java/arthas/VmTool.java Provided native method wrappers for mallocTrim and mallocStats.
Comments suppressed due to low confidence (2)

arthas-vmtool/src/test/java/arthas/VmToolTest.java:215

  • Consider adding assertions to validate the result of mallocTrim instead of just executing the method.
public void testMallocTrim() {

arthas-vmtool/src/test/java/arthas/VmToolTest.java:221

  • Consider adding assertions or validations to ensure mallocStats returns the expected outcome.
public void testMallocStats() {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

FR: 支持 malloc_trim
3 participants