简体   繁体   English

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

[英]How to get Object ID as used in Heap Dump

I would like to be able to get the same ID that is being used in Java heap dumps (created via jmap or JMX, etc). 我希望能够获得在Java堆转储中使用的相同ID(通过jmap或JMX等创建)。 This is to be able to identify the live object at the still running application versus an older memory snapshot (the heap dump) of the same app. 这是为了能够识别仍在运行的应用程序中的活动对象与同一应用程序的旧内存快照(堆转储)。

I have already tested a little and it is obvioulsy not the hashCode, neither the JDI unique ID (which you can see in your debuggers). 我已经测试了一点,它显然不是hashCode,也不是JDI唯一ID(你可以在调试器中看到)。

From checking the code in the sun.jvm.hotspot.utilities I assume it is the objects address in memory. 通过检查sun.jvm.hotspot.utilities中的代码,我假设它是内存中的对象地址。 But also my tests with sun.misc.Unsafe didn't lead to the same id value as used in the heap dumps. 但是我对sun.misc.Unsafe的测试也没有导致与堆转储中使用的相同的id值。 (see here for some Unsafe explanation: http://zeroturnaround.com/rebellabs/dangerous-code-how-to-be-unsafe-with-java-classes-objects-in-memory/ ) (请参阅此处了解一些不安全的解释:http: //zeroturnaround.com/rebellabs/dangerous-code-how-to-be-unsafe-with-java-classes-objects-in-memory/

Any ideas? 有任何想法吗? Thanks :) ! 谢谢 :) !

There are two different ways to create a heap dump: 创建堆转储有两种不同的方法:

  1. from inside JVM process using Dynamic Attach Mechanism ( jmap does so), or 从JVM进程内部使用动态连接机制jmap这样做),或
  2. from the external process using Serviceability Agent ( jmap -F ) 来自使用Serviceability Agentjmap -F )的外部进程

In both cases the object ID in the heap dump is the memory address of an object at the moment of creating the dump. 在这两种情况下,堆转储中的对象ID都是创建转储时对象的内存地址。 Here is the relevant HotSpot source code: [1] and [2] . 以下是相关的HotSpot源代码: [1][2]

However, this object ID is meaningless outside the dump file, because objects can move in memory during Garbage Collection. 但是,此对象ID在转储文件外部没有意义,因为在垃圾收集期间,对象可以在内存中移动。

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, eg 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. 另一个问题是,从Java应用程序中获取Java对象的可靠地址很困难(甚至不可能) - 再次,因为对象可能沿着堆移动,并且因为对象引用的表示可能因不同的体系结构,环境而异和JVM选项,例如,取决于堆大小, UseCompressedOops等。这是从Java应用程序中获取对象地址的示例 ,但不保证这适用于所有JVM版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM