java - 是否有可能在java中获取变量堆上的地址?

标签 java pointers memory-management heap-memory memory-address

假设我有这个变量:

...
Somekindofobject var = new Somekindofobject();
...

我想知道 var 在堆上的位置(通过地址,如 0x08 等),并打印出地址。

这可能吗?

最佳答案

i am working on a program that gets as input java program , and instruments code that prints out to file information about variable access. The only way that i can determine between two fields of objects of the same class is by thier address on heap. Therefore, i need the address on heap

您可以使用 System.identityHashCode 来获得相同性的概念。 它并不完美,但它非常好。 如果你确实得到了一个对象的堆地址,请记住 JVM 被允许移动对象(当分代垃圾收集器将一个长生命周期对象提升到老一代时经常这样做)所以对象的堆位置不是一个在所有情况下都很好地代表身份。

关于java - 是否有可能在java中获取变量堆上的地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4650970/

相关文章:

Java:SOAP 消息发送失败但在 SOAPUI 中有效

C++:用变量推导。一小段代码有问题

c++ - 指针在退出递归函数后更改其地址

c++ - 如何确定 C++ 中对象的大小?

java - 如何在我的 Java 应用程序中使用 Python 库?

java - 获取Java类/对象的私有(private)属性列表

java - 将 Spring 与 App Engine 结合使用

c - Printf() 在 C 中打印参数和指针

c - 从结构中释放内存

delphi - Delphi 中的第三方垃圾收集解决方案