java--Object Memory Layout

Source: Internet
Author: User

In a hotspot virtual machine, the storage layout of objects in memory can be divided into 3 areas: object header, instance data, and alignment padding.

  First, the header of the object header layout

Mark Word
Class pointer

In the 32-bit system, the above two parts each accounted for 4B;

In a 64-bit system, Mark work occupies the 4b,class pointer at 4B when the pointer is turned on, or 8B when the pointer is compressed.

  

1. Mark Word stores the runtime data information of the object itself, such as hash code, GC generational age, lock status flag, thread-held lock, and bias thread ID.

2, class pointer, through which the pointer determines which class the object is an instance of.

In addition, if the object is an array, there will also be a data to record the length of the array 4B (32bit)/8b (64bit).

Second, the data section

  In this field, whether it is inherited from the parent class or the class itself, it needs to be recorded, and the order of storage for this part is affected by the virtual machine's allocation policy and the order in which it is defined.

Default Assignment Policy: Reference, Byte/boolean, Short/char, Long/double, Int/float, and so on

If-xx:fieldsallocationstyle=0 is set (the default is 1), then the reference is placed at the front:

Reference, Long/double, Int/float, Short/char, Byte/boolean

 Conclusion: The allocation strategy is always arranged in the order of the width from large to small, and the same width is put together.

  In an object that has an inheritance relationship, there is only one header, and the property of the parent class is assigned to that object in order to access his properties through the parent class. If Compactfields is turned on, the narrower variable in the subclass is inserted into the void of the variable in the parent column.

  [[note]] the space occupied by a static property is usually not calculated into the space of the object itself, assuming that his reference is in the method area.

  property is aligned, for example, Byte occupies a byte of space, then the 3-byte implementation alignment is populated. (32bit System)

Third, align the fill

  Java objects are aligned in memory by 8 bytes, which means that the total size of the object must be an integer multiple of 8 bytes.

java--Object Memory Layout

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.