JAVA virtual machine - memory layout of objects

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

  1. Object header (Header) 
  • Runtime data (such as HashCode, GC generation age, lock status standard, thread holding lock, biased thread ID, biased timestamp, etc.)
  • Type pointer (a pointer to a class element, if it is an array object, the object header must also have a piece of data for recording the length of the array)

    2. Instance data (Instancs Data)

  • Stores the real valid information of the object (various types of field contents)
  • The storage order is affected by the allocation strategy parameters of the virtual machine and the order in which the fields are defined in the Java source code (the default allocation strategy is Longs/Doubles, Ints, Shors/Chars, Bytes/Booleans, oops)
  • Allocation strategy:

            (1) Fields of the same bandwidth are allocated together.

            (2) The variables defined by the parent class will appear before the child class (if the precondition 1 is satisfied).

            (3) If the parameter value of CompactFields is true (the default is true), the narrower variables in the subclass may be inserted into the gap of the parent class variable.


    3. Padding
  • It acts as a placeholder. When the data part of the object instance is not aligned, it needs to be filled by alignment padding (because the automatic memory management system of the HotSpot virtual machine requires that the starting address of the object must be an integer multiple of 8 bytes)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325625880&siteId=291194637