"In-depth understanding of virtual machine" reading notes-object memory allocation and layout

Object memory allocation and layout

  • Create (new, clone, reflection, deserialization)
    • new-> The constant pool locates the symbol reference, and checks whether the class represented by the symbol reference is loaded, resolved, and initialized. If not, the class loading process is performed
    • The required memory of the object can be completely determined after the class loading is completed.
    • Object memory allocation method is determined by whether the garbage collector has a compression and sorting function
      • Pointer collision: The heap memory is absolutely regular, and the used memory and free memory use the pointer as the demarcation point. Allocating memory only needs to move the pointer accordingly. Serial, parNew and other collectors with Compact process.
      • Free list: record free memory addresses. Update the list at the same time when allocating memory. cms is a collector based on the Mark-Swap algorithm.
    • Thread safety of object memory allocation
      • Synchronization when allocating memory--The virtual machine adopts CAS plus failure retry method
      • Thread Local Allocate Buffer
        • The heap is pre-allocated to a certain memory space of the thread, and this part is used first when allocating memory.
        • Only when TLAB is used up, it is synchronized when it is redistributed.
        • Whether to use or not is determined by the -XX: +/- UserTLAB parameter
  • Memory layout
    • Object header
      • MarkWord
        • Store the runtime data of the object itself, such as hashcode, GC generation age, lock status, lock held by the thread, biased thread ID, biased timestamp
        • 32-bit and 64-bit in 32-bit and 64-bit virtual machines (without compressed pointers)
        • Non-fixed data structure, multiplexing storage space according to object status

      • Klass pointer
        • The object points to a pointer to its class metadata, which is used to determine which class instance the object is
        • Not completely through the object itself, the handle of the handle pool is saved to both instance data and type data pointers
        • If the object is a Java array, you need a space to record the length of the array, because it cannot be determined by the metadata information of ordinary Java objects
    • Example data
      • The effective information that the object really stores, that is, the contents of various types of defined fields
      • Storage order is affected by the order in which virtual machine allocation strategies and fields are defined in the source code
        • HotSpot default allocation (fields of the same width are allocated together): longs / doubles, ints, shorts, chars, bytes / booleans, oops (ordinary object pointers)
        • The field defined by the parent class is before the subclass
        • If the value of the CompactFields parameter is true, the narrower variables of the subclass will be inserted into the gaps of the variables of the parent class
    • Align padding
      • Placeholder role
      • Hotspot stipulates that the starting address of an object must be an integer multiple of 8 bytes, that is, the object must be an integer multiple of 8 bytes
      • The object header is a multiple of 8 bytes (1x or 2x), so this part is to fill in the gaps in the instance data when needed
  • Access targeting
    • The JVM manipulates the objects on the heap through the reference data on the stack, and its essence is just a reference to the object
    • The way is determined by the virtual machine, the current two mainstream
      • Handle access
        • The memory is divided in the heap as the handle pool, and the reference stores the handle address of the object
        • Handle includes address information of object instance data and type data
        • The advantage is stability, when the object is moved, only the instance data address of the handle is changed
      • Direct pointer access (Hotspot)
        • Object address stored by reference
        • The corresponding heap object must store information about the type data (Klass pointer)
        • The advantage is high efficiency, saving the time of pointer positioning
Published 24 original articles · praised 0 · visits 104

Guess you like

Origin blog.csdn.net/jiangxiayouyu/article/details/105614174