jvm 中的 Java 对象 ID [英] Java object ID in jvm

查看:35
本文介绍了jvm 中的 Java 对象 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Eclipse 调试时在对象值附近显示了一个对象 ID.

There is an object ID displayed near the object value in Eclipse While debugging.

例如:28332 是会话对象的 ID.另一个例子:waiting for: (id=101) 显示在调试面板中.这些 ID 既不是哈希码也不是 System.identityHashCode.

For example: 28332 is an ID of session object. Another example: waiting for: (id=101) is displayed in the Debug panel. These IDs are neither a hash code nor a System.identityHashCode.

有谁知道 - 如何获得对象的 this id?

Does anybody knows - how to get this id of object?

推荐答案

我认为他们在内部有一个 IdentityHashMap,为每个对象分配一个唯一的(但没有意义的)整数.这应该是 Eclipse 调试器的内部(不是对象具有的特殊 ID).你是问怎么弄的吗?

I presume they have internally an IdentityHashMap<Object, Integer>, assigning a unique (but meaningless otherwise) integer per object. This should be internal to the Eclipse debugger (not a special id that objects have). Are you asking how to get at that?

编辑:我会像这样设置断点(注意我不精通 Eclipse):

Edit: I would set up breakpoint like this (note I'm not well versed in Eclipse):

  • 我会有一个初始断点(就像您用来截取屏幕截图的断点),并打印我感兴趣的对象的 System.identityHashCode(object).
  • 然后我将使用条件 System.identityHashCode(object) == <你在上一步看到的任何数字> 创建一个断点.这种情况很少会停在错误的对象上.
  • I would have an initial breakpoint (like the one you used to take the screenshot), and print the System.identityHashCode(object) of the object I'm interested into.
  • Then I would create a breakpoint using the condition System.identityHashCode(object) == <whatever number you saw at the previous step>. It would be very rare for this to stop at the wrong object.

或者,如果您感兴趣的对象具有您可以使用的适当的 toString() 表示,您也可以尝试使用它代替 System.identityHashCode(object).在所有情况下,您不必依赖 Eclipse 的内部对象 id,而是捕获这样一个 id(或几乎),您可以从对象本身派生.

Or if the object you are interested in has an appropriate toString() representation you could use, you could also try that instead of System.identityHashCode(object). In all cases, you don't have to rely to Eclipse' internal object id, but capture such an id (or almost) that you can derive from the object itself.

这篇关于jvm 中的 Java 对象 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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