且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何获取堆转储中使用的对象ID

更新时间:2022-12-04 21:13:08

创建堆转储有两种不同的方法:

There are two different ways to create a heap dump:

  1. 使用动态附加机制(jmap这样做),或
  2. 使用 Serviceability Agent ()
  1. from inside JVM process using Dynamic Attach Mechanism (jmap does so), or
  2. from the external process using Serviceability Agent (jmap -F)

在两种情况下,堆转储中的对象ID都是创建转储时对象的内存地址.以下是相关的HotSpot源代码: [1]

In both cases the object ID in the heap dump is the memory address of an object at the moment of creating the dump. Here is the relevant HotSpot source code: [1] and [2].

但是,此对象ID在转储文件之外毫无意义,因为在垃圾回收期间对象可以在内存中移动.

However, this object ID is meaningless outside the dump file, because objects can move in memory during Garbage Collection.

另一个问题是,很难(甚至不可能)从Java应用程序中获取Java对象的可靠地址-再次,因为对象可能沿着堆移动,并且因为对象引用的表示在不同对象之间可能会有所不同架构,环境和JVM选项,例如取决于堆大小,UseCompressedOops等.这是一个从Java应用程序中获取对象地址的示例,但这不能保证在所有JVM版本上都可以使用.

The other problem is that it's difficult (or even impossible) to get a reliable address of a Java object from within Java application - again, because the objects may move along the heap and because the representation of object references can vary between different architectures, environments and JVM options, e.g. depending on heap size, UseCompressedOops etc. Here is an example of getting an object address from within Java application, but this is not guaranteed to work on all JVM versions.