Arthas 3.5.1 发布:神级特性!内存搜索对象
Arthas是Alibaba开源的Java诊断工具,深受开发者喜爱。
-
Github:https://github.com/alibaba/arthas
-
文档:https://arthas.aliyun.com/doc/
以前使用watch等命令时,我们通常要先知道哪个类,调用了哪个函数,然后触发调用。这样有局限:
-
线上触发调用比较难
-
要watch到正确的函数可能要选择多次
-
条件表达式/结果表达式 可能需要多次测试
另外,如果想要查找内存里的对象,需要heapdump再分析。
Arthas在最新发布的 3.5.1 版本里,带来神级特性:通过vmtool命令,可以在JVM内存搜索对象。
vmtool在线教程
下面以vmtool在线教程为例,演示vmtool命令的功能:
-
https://arthas.aliyun.com/doc/arthas-tutorials.html?language=cn&id=command-vmtool
首先启动任意spring boot应用,比如:
wget https://github.com/hengyunabc/spring-boot-inside/raw/master/demo-arthas-spring-boot/demo-arthas-spring-boot.jar
java -jar demo-arthas-spring-boot.jar
然后用arthas attach目标进程,成功之后就可以使用vmtool命令了:
wget https://arthas.aliyun.com/arthas-boot.jarjava -jar arthas-boot.jar
查找jvm里的字符串对象
首先,vmtool命令通过getInstances这个action,在JVM里搜索字符串:
$ vmtool --action getInstances --className java.lang.String@String[][@String[Sorry, deque too big],@String[head=%d tail=%d capacity=%d%n],@String[elements=%s%n],@String[sun/nio/ch/IOVecWrapper],@String[40252e37-8a73-4960-807e-3495addd5b08:1620922383791],@String[40252e37-8a73-4960-807e-3495addd5b08:1620922383791],@String[sun/nio/ch/AllocatedNativeObject],@String[sun/nio/ch/NativeObject],@String[sun/nio/ch/IOVecWrapper$Deallocator],@String[Java_sun_nio_ch_FileDispatcherImpl_writev0],]
limit参数
通过
--limit参数,可以限制返回值数量,避免获取超大数据时对JVM造成压力。默认值是10。
所以上面的命令实际上等值于:
vmtool --action getInstances --className java.lang.String --limit 10
如果设置--limit为负数,则遍历所有对象。
查找spring context
以前的在线教程里,我们需要通过tt命令来拦载spring调用,然后获取到spring context。
通过vmtool命令,我们可以直接获取到srping context:
$ vmtool --action getInstances \--className org.springframework.context.ApplicationContext@ApplicationContext[][@AnnotationConfigEmbeddedWebApplicationContext[org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@12028586: startup date [Thu May 13 16:08:38 UTC 2021]; root of context hierarchy],]
指定返回结果展开层数
getInstancesaction返回结果绑定到instances变量上,它是数组。
通过
-x/--expand参数可以指定结果的展开层次,默认值是1。
vmtool --action getInstances --className org.springframework.context.ApplicationContext -x 2
获取srping bean,执行表达式
getInstancesaction返回结果绑定到instances变量上,它是数组。可以通过--express参数执行指定的表达式。
比如,查找所有的spring beans名字:
vmtool --action getInstances \--className org.springframework.context.ApplicationContext \--express 'instances[0].getBeanDefinitionNames()'
比如,调用userController.findUserById(1)函数:
$ vmtool --action getInstances \--className org.springframework.context.ApplicationContext \--express 'instances[0].getBean("userController").findUserById(1)'@User[id=@Integer[1],name=@String[name1],]
查找所有的spring mapping对象
vmtool --action getInstances --className org.springframework.web.servlet.HandlerMapping
$ vmtool --action getInstances --className org.springframework.web.servlet.HandlerMapping@HandlerMapping[][@SimpleUrlHandlerMapping[org.springframework.web.servlet.handler.SimpleUrlHandlerMapping@5d3819c8],@EmptyHandlerMapping[org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport$EmptyHandlerMapping@11d509ba],@RequestMappingHandlerMapping[org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping@56a5f2e3],@WelcomePageHandlerMapping[org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WelcomePageHandlerMapping@4c0a4ed3],@EmptyHandlerMapping[org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport$EmptyHandlerMapping@51e1f8c3],@BeanNameUrlHandlerMapping[org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping@68c0a39c],@SimpleUrlHandlerMapping[org.springframework.web.servlet.handler.SimpleUrlHandlerMapping@110b768d],]
查找所有的 javax.servlet.Filter
在Arthas的在线教程里,我们介绍过怎么排查http请求 404/401 的问题。使用的是 trace javax.servlet.Filter *命令。
现在使用vmtool命令,我们可以直接查找出所有的Filter对象,加速定位过程。
$ vmtool --action getInstances --className javax.servlet.Filter@Filter[][@OrderedCharacterEncodingFilter[org.springframework.boot.web.filter.OrderedCharacterEncodingFilter@49b69493],@OrderedHiddenHttpMethodFilter[org.springframework.boot.web.filter.OrderedHiddenHttpMethodFilter@5477cb9e],@AdminFilter[com.example.demo.arthas.AdminFilterConfig$AdminFilter@3b625385],@WsFilter[org.apache.tomcat.websocket.server.WsFilter@14875f22],@OrderedRequestContextFilter[org.springframework.boot.web.filter.OrderedRequestContextFilter@6bed550e],@OrderedHttpPutFormContentFilter[org.springframework.boot.web.filter.OrderedHttpPutFormContentFilter@3e538cba],]
指定 classloader name
在多classloader情况下,还可以指定classloader来查找对象:
vmtool --action getInstances \--classLoaderClass org.springframework.boot.loader.LaunchedURLClassLoader \--className org.springframework.context.ApplicationContext
指定 classloader hash
可以通过sc命令查找到加载class的 classloader。
$ sc -d org.springframework.context.ApplicationContextclass-info org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContextcode-source file:/private/tmp/demo-arthas-spring-boot.jar!/BOOT-INF/lib/spring-boot-1.5.13.RELEASE.jar!/name org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext...class-loader +-org.springframework.boot.loader.LaunchedURLClassLoader@19469ea2+-sun.misc.Launcher$AppClassLoader@75b84c92+-sun.misc.Launcher$ExtClassLoader@4f023edbclassLoaderHash 19469ea2
然后用-c/--classloader 参数指定:
vmtool --action getInstances \-c 19469ea2 \--className org.springframework.context.ApplicationContext
强制GC
当启用 -XX:+DisableExplicitGC的JVM参数之后,调用System.Gc()可能并不会触发GC行为。
vmtool里提供了强制GC的功能:
vmtool --action forceGc
如果应用配置了-verbose:gc参数,则可以在应用的标准输出里看到类似的日志:
[GC (JvmtiEnv ForceGarbageCollection) 25760K->17039K(349696K), 0.0015299 secs][Full GC (JvmtiEnv ForceGarbageCollection) 17039K->16840K(353792K), 0.0154049 secs]
致谢
-
vmtool功能是在社区开发者dragon-zhang(张子成)的最初PR上,多次讨论修改完成的,感谢他的工作,同时欢迎大家提出PR,参与开发😄。
总结
-
vmtool wiki: https://arthas.aliyun.com/doc/vmtool
-
Release 日志: https://github.com/alibaba/arthas/releases/tag/arthas-all-3.5.1
上面只展示使用vmtool命令操作spring context的例子。实际上可以应用在很多方面,比如:
-
查找 RPC 的 Provider/Consumer 实例
-
查找 MQ 的订阅对象信息
-
查找 Mybatis 的mapping对象
欢迎大家到Issue里分享使用经验:https://github.com/alibaba/arthas/issues
最后我们正在寻找小伙伴,特别是深圳的同学,欢迎大家加入。
