Java object Model (go to 32-bit system)

Source: Internet
Author: User
Tags memory usage object model

In the Sun JVM, objects (other than arrays) have a head of two machine characters (words). The first word contains the marked hash code for this object and other identity information such as lock status and so on, and the second word contains a reference to the object's class. In addition, any object is 8 bytes aligned for granularity. This is the first rule of the object memory layout:

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

For example, if you call new Object (), because the Object class does not have other members that can be stored, only 8 bytes in the heap are used to hold the two-word header.

Memory layout of classes that inherit object

In addition to the 8-byte head mentioned above, the class attribute is immediately followed. Attributes are usually arranged according to their size. For example, an integral type (int) is aligned in 4 bytes, and a long integer (length) is aligned in 8 bytes. This is designed for performance reasons: Typically, if the data is aligned in 4 bytes, it is more cost-effective to read 4 bytes of data from memory and write to the processor's 4-byte register.

To conserve memory, the Sun VM does not have a memory layout in the order of the attribute declaration. In fact, attributes are organized in memory in the following order:

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

2. Integral type (ints) and floating-point type (floats)

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

4. Boolean (Booleans) and byte-type (bytes)

5. Reference type (References)

Memory usage is optimized through this mechanism. For example, declare a class as follows:

1 2 3 4 5 6 7 8 9 10 11 12-13 Class MyClass {byte A;           int C;           Boolean D;           Long e;            Object F; }

If the JVM does not disrupt the declaration order of attributes, its object memory layout will look like this:

1 2 3 4 5 6 7 8 9 [Header:8 bytes] 8 [a:1 byte] 9 [padding:3 bytes] [c:
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.