In depth analysis of address usage of Java objects

  • 2020-04-01 01:46:18
  • OfStack

  In traditional Java programming, you would no longer need to process Java objects or locations from memory. When you discuss this in a forum, the first question that comes up is why do you need to know the address of a Java object? It's a valid problem. But in the past, we reserved the right to experiment. There is nothing wrong with exploring uncharted territory. I came up with an experiment using the sun package. Unsafe is a part of the sun.misc package. This package may seem a little strange to you, but look at the source code and the methods, and you'll see what I mean.

Java security management provides enough hiding to ensure that you can't fiddle with memory that easily. As a first step, I thought about getting the memory location of a Java object. Until exploring, I was also 100% confident that it was impossible to find the location of an object in Java.
< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201305/20130504142930.png ">

  Sun's Unsafe. Java API documentation shows that we have an opportunity to get the address using the method objectFieldOffset. The method says, "the class storage in the report allocates its location in a particular domain. It also says, "this is just one of the accessors whose cookies are passed to the unsafe heap memory." In any case, I can allocate the memory location to store an object from its class's storage. You could argue that what we're getting is not the absolute physical memory address of an object. 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 called getUnsafe that returns an unsafe object. Java security management requires that you give source code privileges. I used a little bit of reflection and got an example. I know there are better ways to get instances, but I chose the following to bypass security management.

With an Unsafe object, you simply call the objectFieldOffset and the staticFieldOffset. The result is the memory allocation address of the class.

The following example program can run on JDK1.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);
 }
}

The API is introduced:
Boolean compareAndSwapInt(Object obj,long fieldoffset, int expect, int update);
      Modify the (fieldoffset) Int property value of obj object, if the property value is expect, modify it to update, return true, if the property value is not expect, return false
Boolean compareAndSwapObject(Object obj,long Fieldoffset, Object expect, Object update);
      Modify the (fieldoffset) property value of obj object, if the property value is expect, change it to update and return true; if the property value is not expect, return false
Long objectFieldOffset Field (Field);
      Gets the offset of the field in the object
Void park(Boolean flag, long time);
      Causes the current thread to wait
Void unpark (Thread  The thread)
      Causes the current thread to stop waiting
The Object getObject (Object obj, long fieldoffset);
      The obj offset is the property of fieldoffset
Int get int (Object obj, long fieldoffset);
      The obj offset is the int attribute of fieldoffset


Related articles: