In traditional Java programming, you will no longer need to process Java objects or locations from memory. When you discuss this in the forum, the first question to ask is why do you need to know the address of a Java object? It is an effective problem. But in the past, we reserve the right to experiment. There is nothing wrong with exploring uncharted areas of the problem. I've come up with an experiment that uses the Sun company package. Unsafe is a SUN.MISC package. Maybe this bag is a little strange to you, look at the source code and method, you can know what I am referring to.
Java security Management provides enough hiding to make sure that you are not easily fiddling with memory. As a first step, I thought about getting a memory location for a Java object. Until the discovery, I was also once 100% confident that it was impossible to find the location of the object in Java.
Sun's Unsafe.java API documentation shows that we have the opportunity to get address usage objectfieldoffset. This method seems to say: "The class store in the report allocates its location in a specific field." It also says, "This is just one of the accessor cookies passed to the unsecured heap memory." Anyway, I was able to store an object's memory location from the storage allocation of its class. You can argue that what we get is not an object's absolute physical memory address. However, we got the logical memory address. The following program will be interesting to you!
As a first step, I have to get an object of the unsafe class. This is difficult because the constructor is private. There is a method named Getunsafe, which returns an unsafe object. Java security management requires that you give source code privileges. I used a little reflection and then I got an example. I know there are better ways to get examples, but I have chosen the following methods to circumvent security management.
Using unsafe objects, you only need to invoke Objectfieldoffset and Staticfieldoffset. The result is the memory allocation address of the class.
The following instance programs can be run on JDK1.6.
Copy Code code as follows:
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);
}
}
API Introduction:
Boolean compareandswapint (Object obj,long fieldoffset, int expect, int update);
Modifies the (FieldOffset) Int property value of the Obj object, and if the property value is expect, modifies to update, returns True, and returns False if the property value is not expect
Boolean Compareandswapobject (Object Obj,long FieldOffset, object expect, object update);
Modifies the (FieldOffset) property value of the Obj object, if the property value is expect, modifies to update, returns True, and returns False if the property value is not expect
Long Objectfieldoffset (field field);
Get filed offset in the object
VOID Park (Boolean flag, long time);
Make the current thread wait
void Unpark (thread thread)
Stop the current thread from waiting
Object GetObject (Object Obj,long FieldOffset);
The attribute that gets the offset of obj as FieldOffset
int getInt (Object obj,long FieldOffset);
Gets the int attribute of obj offset to FieldOffset