假设我有这个变量:
...
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/