Memory layout for Java objects

Source: Internet
Author: User

Recently in writing a C + + and Java Socket Communication program, you need to transfer the bytes received to an object, extended the problem, found some online information, summarized as follows

This article contains only the memory layout of simple Java objects, regardless of inheritance


An instance of the Java class contains object headers, non-static data members, and alignment data in memory.


static data members, which are shared by all instances of the class and are not stored in an object instance .


The size of the object header depends on the implementation of the JVM, which differs from the object header size of the same JVM, ordinary objects, array objects, and internal objects.


The size of the base data type JVM specification is clearly defined, but the reference type is 4 bytes under the 32bit JVM, and the 64bit JVM is 8 bytes (no pointer compression parameter usecompressedoops is enabled).


The 32bit JVM and the 64bit JVM are generally 8-byte aligned.

A 64bit JVM inside a basic object memory layout might look like this

The object header of an array object is 24 bytes, minus 16 bytes of basic information, plus 4 byte length data, and 4 byte aligned data. In addition, each element of an object array holds a reference to the object.

The object header of a non-static inner object is also 24 bytes, 16 bytes of basic information, plus a reference to an external class of 8 bytes, a 64bit JVM is 8 bytes, and a 32bit JVM is 4 bytes plus alignment data 4 bytes.

If the object's members contain other objects such as arrays, strings, this object only holds references to these member objects, and the memory of those member objects is not directly contained in the object's memory, outside of it.


Does a non-static member have a consistent position and declaration order in object memory? may not


I found an article about the blog, written in 2008 http://www.importnew.com/1305.html

It mentions that Sun VMs (which are now Oracle VMS,,) do not have memory layouts in the order in which they are declared, in order to save memory in the following order

1. Double type (doubles) and long Integer (longs)

2. Integral type (ints) and float type (floats)

3. Short integer type (shorts) and character type (chars)

4. Boolean type (booleans) and byte type (bytes)

5. Reference type (References)

This blog mentions 5 rules.

Rule 1: Any object is 8 bytes aligned to a granularity.

Rule 2: Class attributes are arranged according to the following precedence: long and double types, integer and float, character and short, byte type and Boolean type, and finally reference type. These properties are aligned according to their respective units.

Rule 3: Members in a non-homogeneous inheritance relationship cannot be mixed. The members of the parent class are processed first by rule 2, followed by the members of the subclass.

Rule 4: When the last member of the parent class and the first member of the subclass have not enough 4 bytes, they must be extended to a base unit of 4 bytes

Rule 5: If the first member of a subclass is a double or long integer, and the parent class does not run out of 8 bytes, the JVM breaks Rule 2, filling in the unfilled space in the Order of shaping (int), short integer (shorter), byte type (byte), reference type (reference).

So this rule is still more complicated.

This blog is written in 2008, the implementation of the JVM is likely to have changed, and the JVM version is numerous, it is more difficult to understand so many of their own specifications, from a practical point of view, you can only understand the layout of an object

OPENJDK provides a tool to view the memory layout of an object, which is described in this blog http://blog.csdn.net/aitangyong/article/details/46416667, which can be analyzed using this tool.


Description

This article was published by Giantpoplar in Csdn

Article Address http://blog.csdn.net/giantpoplar/article/details/47657377

Reproduced please retain this note


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Memory layout for Java objects

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.