In conventional java programming, you will never need address or location of a java object from memory. When you discuss about this in forums, the first question raised is why do you need to know the address of a java object? Its a valid question. But always, we reserve the right to experiment. Nothing is wrong in exploring uncharted areas.
I thought of experimenting using a little known class from sun package. Unsafe is a class that belongs to sun.misc package. For some of you the package might be new. You need not worry about it. If you have sun’s JDK then you have this class already.
When a class name is “Unsafe” in java, it calls for your attention immediately. Then I decided to dig deep into it and find what is unsafe about that class. Voila, it really opens up the pandora’s box. Its difficult to find the source of Unsafe. Get the source and look at the methods you will know what I am referring to.
Java’s security manager provides sufficient cover and ensures you don’t fiddle with memory that easily. As a first step, I thought of getting the memory location of a java object. Until the exploration, I too was 100% confident that it was not possible to find the location / address of an object in java.
Sun’s Unsafe.java api documentation shows us an opportunity to get the address using the method objectFieldOffset. That method says, “Report the location of a given field in the storage allocation of its class“. It also says, “it is just a cookie which is passed to the unsafe heap memory accessors“. Whatsoever, I am able to get the storage memory location of an object from the storage allocation of its class.
You can argue that, what we have got is not the absolute physical memory address of an object. But we have got the logical memory address. The following program will be quite interesting for you!
As a first step, I have to get an object of Unsafe class. It is quite difficult as the constructor is private. There is a method named getUnsafe which returns the unsafe object. Java’s security manager asks you to make your java source code privileged. I used little bit of reflection and got an instance out. I know there are better ways to get the instance. But to bypass the security easily I chose the following.
Using Unsafe’s object just invoke objectFieldOffset and staticFieldOffset. The result is address / location of object in the storage allocation of its class.
Following example program runs well on JDK 1.6
import sun.misc.Unsafe;
import java.lang.reflect.Field;
public class ObjectLocation {
private static int apple = 10;
private int orange = 10;
public static void main(String[] args) throws Exception {
Unsafe unsafe = getUnsafeInstance();
Field appleField = ObjectLocation.class.getDeclaredField("apple");
System.out.println("Location of Apple: "
+ unsafe.staticFieldOffset(appleField));
Field orangeField = ObjectLocation.class.getDeclaredField("orange");
System.out.println("Location of Orange: "
+ unsafe.objectFieldOffset(orangeField));
}
private static Unsafe getUnsafeInstance() throws SecurityException,
NoSuchFieldException, IllegalArgumentException,
IllegalAccessException {
Field theUnsafeInstance = Unsafe.class.getDeclaredField("theUnsafe");
theUnsafeInstance.setAccessible(true);
return (Unsafe) theUnsafeInstance.get(Unsafe.class);
}
}















Good tutorial
Imdad on July 6th, 2010 6:03 amNice try. Keep it going.
Geetha on July 6th, 2010 4:52 pmGreat info!
Anubha on July 28th, 2010 10:23 amThanks for sharing, keep going………
Jagdish on September 7th, 2010 6:18 amnice info….
ajay malhotra on September 27th, 2010 4:59 pmnice exploration
shaik muhammad on September 28th, 2010 10:18 amKeep experimenting and posting :-)
Chankey Pathak on October 4th, 2010 10:52 pmThanks for sharing….
Saravanan on October 27th, 2010 8:35 amnice expermentation, keep going
Anuradha on October 28th, 2010 8:58 amNice article.
Ajai on November 13th, 2010 4:19 pmExpecting a lot more :)
interesting tutorial
Mahender on November 21st, 2010 4:30 amgreat info..thanks keep on sharing…
Vinod Gulia on November 26th, 2010 11:45 pmHey thanks for posting this valuable info.
Swati on December 15th, 2010 5:29 pmNice article.
Sana on December 16th, 2010 1:15 pmKeep it up… :)
Hi Nice Try..
When try executing the given program getting the below error..
Access restriction: The type Unsafe is not accessible due to restriction on required library C:\Program Files
Sneha on December 20th, 2010 7:39 am\Java\jre6\lib\rt.jar
Superb try! very nice! Keep it up!!!!!
Mani on December 22nd, 2010 8:52 amGood Info. Thx..
gr8 on December 29th, 2010 9:46 amGreat information…Please keep sharing more stuffs
Robin on December 31st, 2010 6:11 amYour concets are really great.I became a fan of ur tutorial. Keep it up
shampa on March 9th, 2011 6:31 amReally gud article.. its nice 2 read ur all articles.Keep it up !!!
Alpna on April 20th, 2011 5:20 amawesome work….thnxx for such kind of post
PANKAJ SINHA on May 18th, 2011 5:58 pmHi !!
Its good article.. Keep on growing…
Best of luck !!!
Thangavel L Nathan on June 1st, 2011 11:38 amhey its GOOD. keep it up.
padmaraju on June 17th, 2011 5:21 amNice Explanation…
anil on June 22nd, 2011 11:42 am[...] If you’ve never heard of it before (or even if you have) then I encourage you to read this article to get an idea about the sort of things that can be done using ‘Unsafe‘. Note that we [...]
Resurrecting sun.misc.Uns&hellip on July 9th, 2011 3:22 pm[...] If you’ve never heard of it before (or even if you have) then I encourage you to read this article to get an idea about the sort of things that can be done using ‘Unsafe‘. Note that we [...]
Resurrecting sun.misc.Uns&hellip on July 9th, 2011 3:22 pmvery simple and good
chandra on September 26th, 2011 9:41 amThe output of the java program is not the address of the field(s) but the offset of the field in the object.
Ramesh on September 29th, 2011 3:01 pmgood job
praveen rohal on November 2nd, 2011 6:18 pmWell you CAN get an object’s address in physical memory using JNI. I realize this is a relatively old post, but if you are still interested let me know!
N.ZAZA on November 22nd, 2011 3:32 pmN. ZAZA I am interested in getting the physical address in JNI any tips?
O. L. on November 27th, 2011 12:18 pmIntresting Tutorial !!
Praveen on December 16th, 2011 10:24 amNice Tutorial:)
Sathyaraj on February 7th, 2012 3:44 pmBrilliant.. :)
Swapnil on March 28th, 2012 12:13 amGreat ….tutorial..!!
dhaval on April 4th, 2012 1:12 pmHello Everybody,
Muhammad on May 4th, 2012 5:36 pmthis is a great tutorial for me and i learn a lot from this .Today
this question is asked to me in interview ……..
Hi Nice Try..
When try executing the given program getting the below error..
Access restriction: The type Unsafe is not accessible due to restriction on required library C:\Program Files
\Java\jre6\lib\rt.jar
plz tell me what this mean basically
Arvind on May 28th, 2012 7:11 pm