is it possible to get address on heap of a variable in java? Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of is it possible to get address on heap of a variable in java? without wasting too much if your time.

The question is published on by Tutorial Guruji team.

lets say i have this variable :

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

and i want to know where var is located on the heap ( by address , like 0x08 and so on),and to print the address out .

is it possible?

Answer

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

You can use System.identityHashCode to get a notion of sameness. It’s not perfect, but it’s pretty good. If you do get the heap address of an object, remember that the JVM is allowed to move objects around (and frequently does when a generational garbage collector promotes a long lived object to an older generation) so the heap location of an object is not a good proxy for identity in all circumstances.

We are here to answer your question about is it possible to get address on heap of a variable in java? - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji