How to get Object ID as used in Heap Dump

 ̄綄美尐妖づ 提交于 2019-12-11 05:19:42

问题


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). 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).

From checking the code in the sun.jvm.hotspot.utilities I assume it is the objects address in memory. But also my tests with sun.misc.Unsafe didn't lead to the same id value as used in the heap dumps. (see here for some Unsafe explanation: http://zeroturnaround.com/rebellabs/dangerous-code-how-to-be-unsafe-with-java-classes-objects-in-memory/)

Any ideas? Thanks :) !


回答1:


There are two different ways to create a heap dump:

  1. from inside JVM process using Dynamic Attach Mechanism (jmap does so), or
  2. from the external process using Serviceability Agent (jmap -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. Here is the relevant HotSpot source code: [1] and [2].

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

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.



来源:https://stackoverflow.com/questions/30629014/how-to-get-object-id-as-used-in-heap-dump

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!