JVM_07 runtime data area 4-object instantiated memory layout and access location

1. The instantiation of the object

Insert picture description here
1.

  • new

    • The most common way
      Variation 1: Static method of Xxx
      Variation 2: Static method of XxBuilder/XxoxFactory
  • Class's newInstance(): The reflection method can only call the constructor of the null parameter, and the permission must be public

  • Constructor's newInstance (Xxx): in a reflective way, you can call the constructor with empty and parameterized parameters, and there is no requirement for permissions

  • Use clone(): do not call any constructor, the current class needs to implement the Cloneable interface and implement clone()

  • Use deserialization: get a binary stream of an object from a file and from the network

  • Third-party library Objenesis

  • Determine whether the class corresponding to the object is loaded, linked, initialized

  • Allocate memory for the object

    • If the memory is regular and a pointer collides

    • If the memory is irregular:

      • The virtual machine needs to maintain a list
      • Free list allocation
  • Dealing with concurrency security issues

    • Use CAS with failed retry to ensure update atomicity
    • Each thread is allocated a TLAB in advance
  • Initialize the allocated space-all properties are set to default values ​​to ensure that the object instance fields can be used directly when they are not assigned.
    Set the object header of the object

  • Execute the init method to initialize

2. Object memory layout

1. Header: Runtime metadata Markword and KlassInstance type pointers
2. Instance data InstanceData
3. Padding alignment and padding

public class CustomerTest {
    public static void main(String[] args) {
        Customer cust = new Customer();
    }
}

Insert picture description here

3. Access location of the object

How does the JVM access its internal object instances through the object references in the stack frame? -> Positioning, accessed through reference on the stack
Insert picture description here

1. Handle access
Insert picture description here

2. Direct pointer (adopted by HotSpot)
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_43141726/article/details/114853569