Instantiation of objects
1. How objects are created
- New: Objects are created using the new keyword
- Class newInstance() : by reflection, only the constructor with no arguments can be called. The permission must be public
- Constructor newInstance(): Calls to parameterless and parameterless constructors by reflection, with unlimited permissions
- Using Clone () : The class must implement the Cloneable interface and implement the Clone () method, which is shallow-copy
- Create objects by deserializing them
2. Object creation steps
- Determines whether the corresponding class of the object is loaded, linked, or initialized.
- When the JVM encounters a new instruction, it first checks to see if the instruction’s arguments locate a symbolic reference to a class in the constant pool, and to see if the class represented by the symbolic reference has been loaded, parsed, and initialized. If not, in parent delegate mode, use the current class loader and fully qualified name to find the class file. If notfound, report classNotFoundexception. If found, the class is loaded and class objects are generated
- Allocates memory for objects.
- The first step is to calculate how much memory the object occupies, with the exception of 8 bytes for double and long, and 4 bytes for the rest.
- If memory is neat, can use pointer collision hair allocates memory space for the object: all is used within a consciousness, exist within the other side of the idle, there is a pointer to the middle as a cut-off point indicator, allocating memory is merely the pointer for free there move a equals the size of the object, for example.
- If memory is not tidy, the JVM uses the free list method to allocate memory for objects. This means that the virtual machine maintains a list of memory blocks that are available, finds a large enough space from the list to allocate to object instances, and updates the list.
- Dealing with concurrency
- Retry with CAS failure, zone loading ensures atomicity of updates
- Assign a TLAB to each thread
- Initialize the allocated space
- Default initialization of an object to ensure that it can be used without assignment.
- Set the object header
- Data information such as the object’s owning class, object’s Hashcode, and object’s GC information and lock information are stored in the object header
- Execute the init method for display initialization
- Executes the constructor for the class
The memory layout of the object
The memory layout of objects in the heap has a3Part: (1) Object header (2) Instance data (3) Alignment fillCopy the code
- Object header: Contains two parts: (1) runtime metadata and (2) type pointer
- Runtime metadata: hash values, GC allocation age, lock status flags, locks held by threads
- Type pointer: Points to class metadata to determine the type to which the object belongs
- Instance data: store field values of parent and child classes
- Alignment fill: doesn’t mean much
3. Object access positioning
What if an object in the heap is accessed via a reference in a stack frame?Copy the code
- Handle access: The heap maintains a list of pointer addresses and actual addresses of objects in the heap.
- Direct pointer: Finds the address of an object in the heap using a direct pointer.