1.JHSDB简介
JHSDB是一款基于服务性代理实现的进程外调试工具。服务性代理是HotSpot虚拟机中一组用于映射Java虚拟机运行信息的,主要基于Java语言实现的API集合。
2.JHSDB启动
2.1 启动示例代码
package com.ryj.hotspot; import java.io.IOException; import org.omg.CORBA.ObjectHolder; public class JHSDB_Test { static class Test { static ObjectHolder staticObj = new ObjectHolder(); ObjectHolder instanceObj = new ObjectHolder(); void foo() { ObjectHolder localObject = new ObjectHolder(); while (true) { try { Thread.sleep(6000 * 1000l); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } public static void main(String[] args) { Test test = new JHSDB_Test.Test(); test.foo(); } }
2.2 找到java进程号
C:Program FilesJavajdk1.8.0_144in>jps -l 8400
2.3 启动JHSDB
C:Program FilesJavajdk1.8.0_144lib>java -cp .sa-jdi.jar sun.jvm.hotspot.HSDB
如果出现报错信息,需要将缺少的文件移动到该lib目录下
2.4 在打开的窗口中输入进程号
- Inspector:查看 OOP 和 Klass 对象信息,双击线程名称也能够查看此信息
- Stack Memory:线程栈内存
- Show Java Stack Traces:显示线程堆栈信息
- Show Thread Infomation:显示线程信息
- Find Crashes
3.JHSDB实战
3.1 查看堆信息
可以通过Tools -> Heap Parameters 查看或通过Windows -> Console执行命令
hsdb> universe Heap Parameters: ParallelScavengeHeap [ PSYoungGen [ eden = [0x00000000d5d80000,0x00000000d5f73368,0x00000000d7e00000] , from = [0x00000000d8300000,0x00000000d8300000,0x00000000d8800000] , to = [0x00000000d7e00000,0x00000000d7e00000,0x00000000d8300000] ] PSOldGen [ [0x0000000081800000,0x0000000081800000,0x0000000086d80000] ] ]
3.2 查找对象内存地址
hsdb> scanoops 0x00000000d5d80000 0x00000000d8800000 com.ryj.hotspot.JHSDB_Test$ObjectHolder
由于本地环境 scanoops命令报错,后续参考 https://www.bilibili.com/video/av95220376/