Memory layout of Java Objects (ii): Use Sun.misc.Unsafe to get the offset address of the class field and the value of the Read field

Source: Internet
Author: User

In the previous article, we listed several conclusions about calculating the size of Java objects and the use of Jol tools, and the source code of the Jol tools is interesting to look at. Now we use the Sun.misc.Unsafe in the JDK to calculate the offset address of the next field, a verification of the conclusions in the previous article, and a comparison with the Jol output results. You can refer to this article for information on how to get the Sun.misc.Unsafe object.

public class Vo{public int a = 0;public long B = 0;public static String c= "123";p ublic static Object d= null;public Stati c int e = 100;}

1. Get the offset address of the instance field

Gets the offset address of the instance field, and the field with the smallest offset (only next to the head) is the size of the object header System.out.println (Unsafe.objectfieldoffset (VO.class.getDeclaredField ("a "))); System.out.println (Unsafe.objectfieldoffset (VO.class.getDeclaredField ("B"));// FieldOffset as with Objectfieldoffset functions, FieldOffset is an outdated method and it is best not to use SYSTEM.OUT.PRINTLN (Unsafe.fieldoffset ( VO.class.getDeclaredField ("B")));


2. Get the head size and element size of the array

The offset address of the first element of the array, which is the number of bytes occupied by the array header int[] intarr = new Int[0]; System.out.println (Unsafe.arraybaseoffset (Intarr.getclass ()));//the size occupied by each element in the array System.out.println ( Unsafe.arrayindexscale (Intarr.getclass ()));
there are many constants in the unsafe class that end with Base_offset, such as Array_int_base_offset, which are obtained through the Arraybaseoffset method. The Arraybaseoffset method is a local method that can get the offset address of the first element of the array. There are many constants in the unsafe class that end with Index_scale, such as Array_int_index_scale, which are obtained through the Arrayindexscale method. Using Arraybaseoffset with Arrayindexscale, you can position each element in the array in memory.

3. Get the static field offset for a class

Gets the static field offset address of the class System.out.println (Unsafe.staticfieldoffset (VO.class.getDeclaredField ("C"))); System.out.println (Unsafe.staticfieldoffset (VO.class.getDeclaredField ("D")));//Gets the starting address of the static field, via the start address and offset address, You can get the value of the static field//Just the starting address of the static field, type is not long, but object type Object base1 = Unsafe.staticfieldbase (vo.class); Object base2 = Unsafe.staticfieldbase (VO.class.getDeclaredField ("D")); System.out.println (BASE1==BASE2);//true

4. Get the number of bits in the operating system

  the report the size in bytes of a native pointer.//  returns 4 or 8, which represents a 32-bit or 64-bit operating system. System.out.println (Unsafe.addresssize ());//returns 32 or 64, gets whether the operating system is 32-bit or 64-bit System.out.println (System.getproperty (" Sun.arch.data.model "));



With the above snippet, we can successfully get the offset address of each field in the class, which is consistent with the output of the Jol tool and our conclusion. With the offset address of the field, we are able to get the value of the field directly through unsafe by adding the object's starting point.


5. Read the value of the object instance field

Gets the property value of the instance field Vo vo = new Vo (); vo.a = 10000;long Aoffset = Unsafe.objectfieldoffset (VO.class.getDeclaredField ("a")); int VA = Unsafe.getint (vo, Aoffset); System.out.println ("va=" +va);

6. Get the property value for a static field

VO.E = 1024; Field Sfield = VO.class.getDeclaredField ("E"), Object base = Unsafe.staticfieldbase (Sfield), long offset = Unsafe.staticfieldoffset (Sfield); System.out.println (Unsafe.getint (base, offset));//1024

You can see that the unsafe feature is very powerful, and the bit Java language provides a lower level of functionality.


Memory layout of Java Objects (ii): Use Sun.misc.Unsafe to get the offset address of the class field and the value of the Read field

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.