Complex Java objects share memory size

We explained to calculate the size of a single simple Java objects share a single memory Java objects in memory layout. Then this article is to explain the main computational complexity of Java objects share memory size, we inheritance, composite objects called complex object

Inheritance Object

the Parent {class 
    protected int X; //. 4 bytes 
    protected int y; // 4-byte 

    protected boolean flag; // 1-byte 
} 

class the extends the Parent Child { 
    Private int Z; //. 4 bytes 
} 

public class ExtendsObjectSizer { 
    static void main public (String [] args) { 
        System.out.println ( "inherited object size is:" + ObjectSizeFetcher.sizeOf (new Child ( )) + " bytes"); 
    } 
}

  

 

Then repackage, execute the following command:

The pointer ## is not turned on compression 
Java -XX: -UseCompressedOops -javaagent: ObjectSizeFetcherAgent-1.0-SNAPSHOT.jar com.twq.ExtendsObjectSizer

The results obtained were as follows:

 

 It can be seen new Child()memory size is 40 bytes, then 40 bytes is how come it? We look: 

 

 

 

 

 

16 + 40 bytes = (4 + 4 + 7 + 1) + 7 + 4

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

  The results obtained were as follows:

 

 It can be seen new Child()memory size is 32 bytes, then 32 bytes is how come it? We look: 

 

 

32 bytes = 12 + 4 + 4 + 4 + 7 + 1

Summarize : objects inherit only one object head, the object inherits the share memory size consists of four parts:

  1. Object header
  2. Father attribute the share memory size, need to be aligned supplement to a multiple of 8
  3. Child property occupied by memory size
  4. Align supplement

Compound Objects

Next we look at complex objects share memory size, look at the following code:

{the Employee class 
    Private int Age; //. 4 bytes 
    private double high; // 8-byte 
} 

class Dept's { 
    Private int NUM; //. 4 bytes 

    Private the Employee [] = new new the Employee the Employees [. 3]; 

    public Dept's () { 
        for (int I = 0; I <employees.length; I ++) { 
            the Employees [I] = the Employee new new (); 
        } 
    } 
} 

public class CompositeObjectSizer { 
    public static void main (String [] args) {IllegalAccessException throws 
        the System.out .println ( "compound object memory size is:" + ObjectSizeFetcher.sizeOf (new Dept ( )) + " bytes"); 
        System.out.println ( "total memory size of the object compound is:" + ObjectSizeFetcher.fullSizeOf ( new Dept ()) + "bytes");
    }
}

  Then repackage, execute the following command:

The results obtained were as follows:

 

 

Composite objects can be seen that the size of memory is32字节 = 对象头16字节 + num属性4字节 + employees引用类型的大小8字节 + 对齐补充4字节

This is only 32 bytes Dept directly calculate the current size of the object space, the size of this array does not contain employeesall of Employeethe memory size, the total size of the object to the composite 176字节, the size of the array includes employeesall of Employeethe memory size, and recursive calculation is the total size of the current object space.

Recursive computation memory occupied by the object compound should be noted that when: alignment padding is performed in units of each object, it is easy to see the following FIG apparent: 

 

 

According to the figure, we manually calculate new Dept () a total memory size, divided into three parts:

  • Examples of the object itself Dept size is: 16 + 4 + 8 + 4 = 32 bytes
  • Employees Array object size: 24 * 3 + 8 = 48 bytes
  • 3 Employee object size is: 3 * (16 + 4 + 4 + 8) = 96 bytes

So the total memory size is: 32 + 48 + 96 = 176 bytes

Let us look at the turn of the compound object pointer memory compression function of size, we execute the following command:

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

  

The results obtained were as follows:

 

 Composite objects can be seen that the size of memory is24字节 = 对象头12字节 + num属性4字节 + employees引用类型的大小4字节 + 对齐补充4字节

In the case of pointer compression is turned on, the object compound Dept's total size is 128 bytes, as shown below: 

 

 

According to the figure, we manually calculate new Dept () a total memory size, divided into three parts:

  • Examples of the object itself Dept size is: 12 + 4 + 4 + 4 = 24 bytes
  • Employees Array object size: 16 * 3 + 4 + 4 = 32 bytes
  • 3 Employee object size is: 3 * (12 + 4 + 8) = 72 bytes

So the total memory size is: 24 + 32 + 72 = 126 bytes

 

ArrayList

Here are some of the source ArrayList:

public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> {
    protected transient int modCount = 0;
}

public class ArrayList<E> extends AbstractList<E>
        implements List<E>, RandomAccess, Cloneable, java.io.Serializable
{
    private static final long serialVersionUID = 8683452581122892189L;

    /**
     * Default initial capacity.
     */
    private static final int DEFAULT_CAPACITY = 10;

    /**
     * Shared empty array instance used for empty instances.
     */
    private static final Object[] EMPTY_ELEMENTDATA = {};

    private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};

    transient Object[] elementData; // non-private to simplify nested class access

    /**
     * The size of the ArrayList (the number of elements it contains).
     */
    private int size;
}

  

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 Java object space, a storage array elements for Object [], an int size, there is a parent class of type int modCount. therefore:

  • In the 64-bit operating system, and does not turn on the premise pointer compression, the new ArrayList<Integer>()memory size is as follows: the parent object header 16 bytes + 4 bytes size class attribute modCount + 4 bytes + supplementary alignment subclass Object [] reference type the 8-byte int + subclass type alignment attribute size 4 bytes + 4 bytes = 40 bytes added
  • In the 64-bit operating system, and turn on the premise pointer compression, the new ArrayList<Integer>()memory size is as follows: target parent head 12 bytes + 4 bytes + size attribute modCount added 4-byte alignment + subclass Object [] reference type the 4-byte int + subclass type attribute size of 4 bytes + 4 bytes alignment = 32 bytes added

 

 

Guess you like

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