eclipse jvm 中的 Java 对象 ID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3289550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 14:55:02  来源:igfitidea点击:

Java object ID in jvm

javaeclipsedebugging

提问by Gorbush

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

在 Eclipse 中调试时,对象值附近会显示一个对象 ID。

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.

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

Does anybody knows - how to get thisid of object?

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

采纳答案by Dimitris Andreou

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?

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

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

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

  • 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.
  • 我会有一个初始断点(就像你用来截取屏幕截图的断点),并打印出System.identityHashCode(object)我感兴趣的对象的断点。
  • 然后我将使用条件创建一个断点System.identityHashCode(object) == <whatever number you saw at the previous step>。这种情况很少会停在错误的对象上。

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.

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