openjdk8 使用 hsdb 工具追踪内存中的对象
调试的源代码
package test;
public class JHSDB_TestCase {
private static class ObjectHolder{}
static class Test{
static ObjectHolder staticObj = new ObjectHolder();
ObjectHolder instanceObj = new ObjectHolder();
// 忽略变量未使用的警告
@SuppressWarnings("unused")
void foo() {
ObjectHolder localObj = new ObjectHolder();
// 这里需要打个断点
System.out.println("done");
}
}
public static void main(String[] args) {
Test test = new JHSDB_TestCase.Test();
test.foo();
}
}
调试参数
-Xmx10m -XX:-UseCompressedOops
运行cmd控制台
查看程序JVMID
jps -l
打开hsdb工具
hsdb 8524
[Tools] → [Heap Parameters]
[Windows] → [Console]
# 语法:scanoops [eden起始地址] [to终止地址] [包名.类名$变量名]
scanoops 0x0000000012500000 0x0000000012700000 test.JHSDB_TestCase$ObjectHolder
# 查看栈中存放的数据
[Windows] → [Console]
revptrs 0x00000000125e4440
显示
null
Oop for java/lang/Class @ 0x00000000125e2448
# 查看栈中存放的数据
[Windows] → [Console]
revptrs 0x00000000125e4468
显示
Oop for test/JHSDB_TestCase$Test @ 0x00000000125e4450
# 监控内存中地址的数据,0x00000000125e4450
[Tools] → [Inspector]
# 查看栈中存放的数据
[Windows] → [Console]
revptrs 0x00000000125e4478
显示
Computing reverse pointers...
Done.
null
null
# 选择主线程
点击[Java Thread Name] → [main]
# 查看栈中数据
点击工具栏[Stack Memory],可以看到
0x00000000125e4478 PSYoungGen test/JHSDB_TestCase$ObjectHolder
实验完成,调试代码中ObjectHolder三个对象均已找到,并成功追
溯到引用它们的地方,也就实践验证ObjectHolder存放在虚拟机内存的栈和堆中。