How to calculate the size of an object?

Continue to create, accelerate growth! This is the first day of my participation in the "Nuggets Daily New Plan · June Update Challenge", click to view the details of the event

The main questions of this paper are as follows:

Question: How many bytes does new Object() occupy in memory?

the structure of the object

In the Hotspot virtual machine, the layout of objects stored in memory can be divided into three areas: object header (Header), instance data (Instance Data) and alignment padding (Padding).

  • Object header: such as hash code, age of the object, object lock, lock status flag, bias lock (thread) ID, bias time, array length (only for array objects), etc.
  • Instance data: store the attribute data information of the class, including the attribute information of the parent class;
  • Alignment padding: Due to virtual machine requirements, the object start address must be an integer multiple of 8 bytes . Padding data is not required, it is just for byte alignment.

To be more intuitive:

Object memory structure.png

Object header details

The object headers of the HotSpot virtual machine include:

  • Mark Word

It is used to store the runtime data of the object itself, such as hash code (HashCode), GC generation age, lock status flag, lock held by thread, biased thread ID, biased timestamp, etc. The length of this part of the data is 32 bits. and 64-bit virtual machines are 32bit and 64bit respectively, officially called it "Mark Word". That is 4 bytes for a 32-bit virtual machine and 8 bytes for a 64-bit virtual machine.

What exactly is stored in Mark Word? We can look at the following table (take a 64-bit operating system as an example, which will not be expanded here):

image.png

  • Klass Pointer

Another part of the object header is the klass type pointer, that is, the pointer to the object's class metadata, and the virtual machine uses this pointer to determine which class the object is an instance of. 4 bytes for 32 bits, 4 bytes for 64 bits with pointer compression enabled or max heap memory < 32g, 8 bytes otherwise. In jdk1.8, the length of pointer compression is 4 bytes by default. When pointer compression is turned off in the JVM parameters (-XX:-UseCompressedOops), the length is 8 bytes.

  • Array length (only for array objects)

If the object is an array, there must also be a piece of data in the object header to record the length of the array. The length is 4 bytes.

1653301527690-d9501a52-8f4b-455e-8c98-d4f1735dda51.png

correct answer

How many bytes does new Object() occupy in a 64-bit operating system under JDK8?
Answer: 16 bytes
8 bytes are MarkWord
4 bytes are pointers (jdk 8 enables pointer compression by default) 4 bytes are alignment padding bits

Do it yourself

  1. The jol plugin can print object layout, first we add dependencies:
implementation 'org.openjdk.jol:jol-core:0.9'
复制代码
  1. test code
public class NewObjectLayoutTest {

    public static void main(String[] args) {
        System.out.println(ClassLayout.parseInstance(new Object()).toPrintable());
    }
}
复制代码
  1. output result
java.lang.Object object internals:
 OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
      0     4        (object header)                           01 00 00 00 (00000001 00000000 00000000 00000000) (1)
      4     4        (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)
      8     4        (object header)                           e5 01 00 f8 (11100101 00000001 00000000 11111000) (-134217243)
     12     4        (loss due to the next object alignment)
Instance size: 16 bytes
Space losses: 0 bytes internal + 4 bytes external = 4 bytes total
复制代码

Summarize

Did everyone get it? Theory is the basis of practice, welcome to leave a message.

Guess you like

Origin juejin.im/post/7101658548232257549