vmtool

vmtool利用JVMTI接口,实现查询内存对象,强制GC等功能。

命令选项 描述
-a, --action <action> 指定执行动作,可选值为forceGc, getInstances
-c, --classloader <hash> 指定ClassLoader的哈希值
--classLoaderClass <name> 指定ClassLoader的类名
--className <name> 指定类的名称
-x, --expand <level> 指定扩展对象层级,默认1
--express <expression> ognl表达式,默认为instances
--libPath <path> 额外库路径
-l, --limit <number> 限制返回实例个数,默认10-1表示无限制
-h, --help 帮助

示例

# 强制执行gc
[arthas@17028]$ vmtool -a forceGc 

# 查看java.util.List在内存中的实例 
[arthas@17028]$ vmtool -a getInstances --className java.util.List
@List[][
    @ArrayList[isEmpty=false;size=2],
    @ArrayList[isEmpty=false;size=2],
    @ArrayList[isEmpty=false;size=2],
    @ArrayList[isEmpty=false;size=2],
    @ArrayList[isEmpty=false;size=2],
    @ArrayList[isEmpty=false;size=2],
    @ArrayList[isEmpty=false;size=2],
    @ArrayList[isEmpty=false;size=2],
    @ArrayList[isEmpty=false;size=2],
    @ArrayList[isEmpty=false;size=2],
]

# 查看SpringBoot服务实例 
[arthas@17028]$ vmtool -a getInstances --className io.github.passerr.system.application.service.UserService
@UserService[][
    @UserService$$EnhancerBySpringCGLIB$$7fb5d73c[io.github.passerr.system.application.service.UserService@2fe33319],
    @UserService[io.github.passerr.system.application.service.UserService@2fe33319],
]

# 指定ClassLoader查看实例 通过sc -d查询类加载器的哈希值
[arthas@17028]$ vmtool -a getInstances -c 9c7af21 --className io.github.passerr.system.application.service.UserService
@UserService[][
    @UserService$$EnhancerBySpringCGLIB$$7fb5d73c[io.github.passerr.system.application.service.UserService@2fe33319],
    @UserService[io.github.passerr.system.application.service.UserService@2fe33319],
]

# 查询实例并执行ognl表达式
vmtool -a getInstances --className java.util.Random --express "instances[0].seed.value"
@Long[0]