Memory layout for Java objects

Source: Internet
Author: User

Memory layout of Java objects: 对象头(Header) , 实例数据(Instance Data) , 对齐填充(Padding) ; additionally: Different environment results may vary, my environment is hotspot virtual machine, 64-bit Windows.

Object Header

The object header occupies 16bytes on a 32-bit system that occupies the 8bytes,64 bit system.

System.out.println ("sizeof (New object ()) =" + sizeof (new object ()));

SizeOf (New Object ()) = 16

Instance data

The memory consumption of the native type (primitive type) is as follows:

The reference type occupies 4bytes each on a 32-bit system and consumes 8bytes per 64-bit system.

Snap To fill

The alignment of the hotspot is 8-byte aligned:

(Object header + instance data + padding)% 8 equals 0 and 0 <= padding < 8

Pointer compression

The memory size that the object occupies is affected by the VM parameter usecompressedoops.

Impact on the object header

The open (-xx:+usecompressedoops) object header size is 12bytes (64-bit machine).

Static Class A {    int A;}

A object takes up memory condition:
关闭指针压缩: 16+4=20 is not a multiple of 8, so +padding/4=24

开启指针压缩: 12+4=16 is already a multiple of 8, no need to padding

Impact on the type of reference

The reference type occupies 8 bytes on a 64-bit machine and 4 bytes after the pointer compression is turned on.

Static class B2 {    int b2a;    Integer-to-business;}

B2 Objects occupy Memory:
关闭指针压缩: 16+4+8=28 is not a multiple of 8, so +padding/4=32

开启指针压缩: 12+4+4=20 is not a multiple of 8, so +padding/4=24

Effects of Array objects

On a 64-bit machine, the object header of an array object occupies 24 bytes, and 16 bytes is used after compression is enabled. More memory is consumed than ordinary objects because additional space is required to store the length of the array.
Consider the memory size of new integer[0], which is 0, which is the size of the object header:
关闭指针压缩: 24bytes

开启指针压缩: 16bytes

Then it is easy to calculate the new integer1,new integer2,new Integer3 and new Integer4:
关闭指针压缩

开启指针压缩

Take new Integer3 to explain specifically:
关闭指针压缩: 24 (object head) +83=48, do not need padding;
开启指针压缩: 16 (object header) +3
4=28,+padding/4=32, others in turn.

The array of custom classes is the same, for example:

Static class B3 {    int A;    Integer b;}

New B33 The amount of memory occupied:
关闭指针压缩: 48
开启指针压缩: 32

Impact on composite objects

Calculating the size of a composite object's memory is actually a matter of using the above rules, just a bit of trouble.
对象本身的大小: Directly calculates the current object footprint, including the current class and superclass base type instance field size, Reference type instance field reference size, instance primitive type array total footprint, instance reference type array reference itself occupies space size; But does not include the size of the object itself that inherits the superclass and the instance reference field of the current class declaration, and the size of the object itself referenced by the instance reference array.

Static class B {    int A;    int b;} Static class C {    int ba;    B[] as = new b[3];    C () {for        (int i = 0; i < as.length; i++) {            as[i] = new B ();}}    }

关闭指针压缩: 16 (object header) +4 (BA) +8 (as reference size) +padding/4=32
开启指针压缩: 12+4+4+padding/4=24

当前对象占用的空间总大小: Recursively calculates the total space occupied by the current object, including the instance field size of the current class and superclass, and the Instance field reference object size. Recursive calculation of the memory occupied by the composite object should be noted that: the alignment of the fill is in each object as a unit, see the following diagram is easy to understand.

Now let's manually calculate how much memory the C object occupies, mainly three parts: the size of the C object itself + the size of the array object +b the size of the object.
关闭指针压缩: (4 + 8+4 (padding)) + (24+ 83) + (16+8)3 = 152bytes
开启指针压缩: (4 + 4 +4 (padding)) + (+ 43 +4 (Array object padding)) + (12+8+4 (b object padding))3= 128bytes

Memory layout for Java objects

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.