Java single object memory layout .md

We wrote a utility class Java objects obtain the share memory size (how to get a Java object in the share memory size of the article ObjectSizeFetcher), then the next, we use this tool class look at the various types of objects in Java accounting for the size of memory

basic type

Basic types of memory consumption as follows:

 

basic type Memory size (unit: bytes)
boolean 1
byte 1
short 2
char 2
int 4
float 4
long 8
double 8

Share more basic types of Java memory size is specified, a reference type share memory size is not determined, then we look at the share reference type memory size, we start with Java single object memory layout

Java single object memory layout

Object header

In Java, each object contains an object header, the object header contains two types of data: storing the object's own runtime data and pointer data type

  1. Storing runtime data object itself, Mark Word (32-bit and 64-bit operating system and a length of 8 bytes are 4 bytes) contains the following information:
    1. Object hashCode
    2. Object GC generational Age
    3. Lock status flag (lock lightweight, heavyweight lock)
    4. Thread holds the lock (lock lightweight, heavyweight lock)
    5. Related biased locking
  2. Type pointers: a pointer to the object class metadata (32-bit operating system -> 4 byte, 64-bit operating system -> 8 bytes (compressed pointer is not on), 4 bytes (compressed open pointer))
    • JVM is determined by the pointer which is an instance of the object class (Class which is determined according to the object pointer)

Therefore, the 32-bit operating system, the memory size occupied by the object header to a Java object8字节

In the 64-bit operating systems:

  • If the pointer is not on the compression, the size of the object is the head16字节
  • If enabled pointer compression, the size of the object is the head12字节

JVM parameter UseCompressedOopsto control whether to turn on compression pointer function is enabled by default, we look at this parameter.

How do we get a Java object was last occupied memory size has left a problem that new Point()the object occupies memory size Why is 24字节it? The following is the Pointcode:

public class Point {
    private int x; // 4字节
    private int y; // 4字节

    public static void main(String [] args) {
        System.out.println(ObjectSizeFetcher.sizeOf(new Point()));
    }
}

  This Pointclass has two properties xand yare the inttype of type int and share memory size is 4 bytes, then two int type of property occupied by 8 bytes in size, so 24 - 8 = 16字节what is the share of memory?

 

Pointer does not turn on compression

When we execute the following command when:

## does not turn on pointer compression 
Java -XX: -UseCompressedOops -javaagent: ObjectSizeFetcherAgent-1.0-SNAPSHOT.jar com.twq.Point

new Point()The object's size is24字节

 

 Because there is no pointer compression is turned on, so the size of the object head this time is 16字节(note: My computer is a 64-bit operating system). It 24 - 8 = 16字节is the object head occupied by memory size

Open pointer compression

When we execute the following command when:

## Open pointer compression 
java -XX: + UseCompressedOops -javaagent: ObjectSizeFetcherAgent -1.0-SNAPSHOT.jar com.twq.Point

  new Point()The size of an object or24字节

 

 

Because turned pointer compression, so the size of the object head at this time is 12字节. So 24 - 8 = 16字节in 16字节addition to containing the 12字节object header, there are 4字节many, this 4字节is the alignment padding (padding) the.

Alignment padding: JVM required size of the object must be an integer multiple of 8, if not, need to fill bit alignment.

When opening the pointer compression, the object header size 12字节+ 2 Size attribute type int 8字节= 20字节, since 20not a multiple of 8, it is necessary to align the padding 4 bytes, i.e.,24字节

Java single object memory layout summary:

  • Java objects share a single memory size is equal to: subject + object memory size occupied by the first instance of the attribute data memory size occupied by the memory size occupied by alignment padding +
  • Storing object data object header contains a pointer data type and their runtime types of data. In the 64-bit operating system, if enabled, then the pointer compression, the memory occupied by the object header size is 12 bytes; if not open, then the pointer compression, the memory occupied by the object header size is 16 bytes
  • Alignment padding: JVM required size of the object must be an integer multiple of 8, if not, need to fill bit alignment.

static properties modified

We Pointadd a modified static variable, the following code:

class Point {public 
    Private int X; 
    Private int Y; 

    public static Long ID = 3000L; // add a modified static variable 

    public static void main (String [] args) { 
        System.out.println (ObjectSizeFetcher.sizeOf (new new Point ())); 
    } 
}

  When we execute the following command when:

## does not turn on pointer compression 
Java -XX: -UseCompressedOops -javaagent: ObjectSizeFetcherAgent-1.0-SNAPSHOT.jar com.twq.Point 

## open pointer compression 
java -XX: + UseCompressedOops -javaagent: ObjectSizeFetcherAgent -1.0-SNAPSHOT.jar com.twq.Point

  The results obtained were as follows:

 

 

new Point()This object occupies memory size or 24字节to prove static variables belong to the class, does not belong instance, stored in the global data segment, common variable was included in the calculation of Java objects occupy space.

Reference types

Reference type on each 32-bit operating system occupies 4 bytes

In the 64-bit operating systems:

  • Pointer compression does not open, then occupies 8 bytes
  • Open pointer compression, then occupies 4 bytes

We write a called RefTypeSizerclass, which reads:

the Person {class 
} 

public class RefTypeSizer { 
    // this is a reference type 
    Private the Person Person; 

    public static void main (String [] args) throws IllegalAccessException { 
        System.out.println ( "Object new RefTypeSizer () share memory size:" + ObjectSizeFetcher.sizeOf (new RefTypeSizer ()) + " bytes"); 
    } 
}

  We then repackage and then to execute the following command:

## does not turn on pointer compression 
java -XX: -UseCompressedOops -javaagent: ObjectSizeFetcherAgent- 1.0-SNAPSHOT.jar com.twq.RefTypeSizer

  The resulting objects new RefTypeSizer()share memory size24字节

 

 

Because this time there is no pointer compression is turned on, so the object head size 16字节, type references Person personpercentage of memory 8字节, so add size16 + 8 = 24字节

Now, let's open the compression function pointer, the following command:

## Open pointer compression 
java -XX: + UseCompressedOops -javaagent: ObjectSizeFetcherAgent -1.0-SNAPSHOT.jar com.twq.RefTypeSizer

  

The resulting objects new RefTypeSizer()share memory size16字节

 

 Because this time turned pointer compression, so the object head size 12字节, type references Person personpercentage of memory 4字节, so add size12 + 4 = 16字节

Array

In the 64-bit operating system, the object header array object occupies 24 bytes, the pointer to enable compression occupies 16 bytes of memory than conventional objects occupying the reason is because multiple arrays require additional space to store the length of the array.

We look at the code length of the array is calculated as follows:

{class ArraySizer public 
    public static void main (String [] args) { 
        System.out.println ( "new new Integer [0] share memory size:" + ObjectSizeFetcher.sizeOf (new Integer [ 0]) + " bytes" ); 
        System.out.println ( "new new Integer [. 1] share memory size:" + ObjectSizeFetcher.sizeOf (new Integer [ 1]) + " bytes"); 
        System.out.println ( "new new Integer [2 ] share memory size is: "+ ObjectSizeFetcher.sizeOf (new Integer [ 2]) +" bytes "); 
        System.out.println (" new new Integer [. 3] share memory size: "+ ObjectSizeFetcher.sizeOf ( new Integer [3]) + "bytes"); 
        System.out.println ( "new new Integer [. 4] share memory size is:" + ObjectSizeFetcher.sizeOf (new Integer [ 4]) + " bytes"); 
    } 
}

  We then repackage and then to execute the following command:

## does not turn on pointer compression 
java -XX: -UseCompressedOops -javaagent: ObjectSizeFetcherAgent- 1.0-SNAPSHOT.jar com.twq.ArraySizer

  The results obtained are as follows:

 

 

 We can see new Integer[0]the size 24字节, because the array length is zero, so the size is the size of the head of the array array object, but also because there is no pointer compression is turned on, so the array object head size 24字节, other lengths to explain the array share memory:

 

 

  • new Integer[1]The size: object header 24 bytes + 1 byte = 8 reference type size 32 bytes
  • new Integer[2]The size: object header 24 bytes + 2 bytes reference type size 16 bytes 40 =
  • new Integer[3]The size: object header 24 bytes + 3 bytes reference type size 24 bytes 48 =
  • new Integer[4]The size: object header 24 bytes + 4 bytes reference type size 32 bytes 56 =
  • new Integer[]{2, 3, 4, 5}Bit size: new Integer[4]the size of 56 bytes + 4 * (Integer Integer object header + 16 bytes in size attribute type int + 4 bytes 4 bytes alignment padding) = 152 bytes

Then we turned pointer compression, execute the following command:

## Open pointer compression 
java -XX: + UseCompressedOops -javaagent: ObjectSizeFetcherAgent -1.0-SNAPSHOT.jar com.twq.ArraySizer

  The results obtained are as follows:

 

 

We can see new Integer[0]the size 16字节, because the array length is zero, so the size is the size of the head of the array array object, but also because opened pointer compression, so the array object head size 16字节, other lengths to explain the array share memory:

  • new Integer[1]The size: object header 16 bytes + 1 byte + 4 reference alignment Type Size 4 bytes = 24 bytes added
  • new Integer[2]The size: object header 16 bytes + 2 bytes reference type size of 8 bytes = 24
  • new Integer[3]Size of: the first 16 bytes of the object reference type Size + 3 + 12 bytes = 4 bytes alignment added 32 bytes
  • new Integer[4]The size: object header 16 bytes + 4 bytes reference type size 16 bytes 32 =
  • new Integer[]{2, 3, 4, 5}Bit size: new Integer[4]the size of 32 bytes + 4 * (Integer object header byte 12 + Integer type int attribute size of 4 bytes) = 96 bytes

to sum up

Java single object memory layout summary:

  • Java objects share a single memory size is equal to: subject + object memory size occupied by the first instance of the attribute data memory size occupied by the memory size occupied by alignment padding +
  • Storing object data object header contains a pointer data type and their runtime types of data. In the 64-bit operating system, if enabled, then the pointer compression, the memory occupied by the object header size is 12 bytes; if not open, then the pointer compression, the memory occupied by the object header size is 16 bytes
  • Alignment padding: JVM required size of the object must be an integer multiple of 8, if not, need to fill bit alignment.
  • static variables belonging to the class, not belonging to the instance, stored in a global data segment, common variable was included in the calculation of the space occupied by the object Java
  • Reference type on each 32-bit operating system occupies 4 bytes in the 64-bit operating systems:
    • Pointer compression does not open, then occupies 8 bytes
    • Open pointer compression, then occupies 4 bytes
  • In the 64-bit operating system, the object header array object occupies 24 bytes, the pointer to enable compression occupies 16 bytes of memory than conventional objects occupying the reason is because multiple arrays require additional space to store the length of the array.

The above is a single, simple Java objects calculate the size of memory occupied for complex Java objects share memory size meter

Guess you like

Origin www.cnblogs.com/tesla-turing/p/11487785.html