如何获取堆转储中使用的对象ID [英] How to get Object ID as used in Heap Dump

查看:180
本文介绍了如何获取堆转储中使用的对象ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够获得与Java堆转储中使用的相同的ID(通过jmap或JMX等创建).这是为了能够识别仍在运行的应用程序中的活动对象,而不是同一应用程序的较旧的内存快照(堆转储).

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.

我已经进行了一些测试,这显然不是hashCode,也不是JDI唯一ID(您可以在调试器中看到).

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

通过检查sun.jvm.hotspot.utilities中的代码,我认为它是内存中的对象地址.但是我对sun.misc.Unsafe的测试也没有产生与堆转储中使用的id相同的id值. (有关不安全的说明,请参见此处: http://zeroturnaround.com/rebellabs/dangerous-code-how-to-be-unsafe-with-java-classes-objects-in-memory/)

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 :) !

推荐答案

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

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.

这篇关于如何获取堆转储中使用的对象ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆