This is the first part of the JVM Inside Out series. For your convenience you can find other parts using the links below:
Part 1 — Getting object address
Part 2 — Reading object content
Part 3 — Java raw type trickery
Part 4 — Locks and out of band exceptions

How to get object address in JVM? There is actually one easy trick to do it via reflection. See this code:

We create an object of type AddressExtractor which has one long field for storing the address. Next, we use reflection to get field named pointerValue. Trick is that we can use reflection to examine Field instance pointing to the pointerValue field. Since this instance must know the type, it stores it in a field called type. We can now use reflection to modify it and trick reflection to think that pointerValue is of type Object.

Next, in lines 16-18 we just create some dummy object to see if we are right. We initialize its field to some readable value and then assign this object to pointerValue long field. JVM now needs to assign Ordinary Object Pointer to the long value. We can then print it out.

I’m using 64-bit JVM and disable OOP compression using -XX:-UseCompressedOops. I get the following output:

Let’s attach WinDBG and see what happens:

We can see first two integers being a mark-word, next two integers for klass word (remember, we disabled OOP compression). Finally, we get one long field (two integers) with expected value.

Of course, this address is valid as long as GC doesn’t move the object. Most likely, if you add System.gc() at the end then the object will be moved.