This is the sixth day of my participation in Gwen Challenge
The structure of an object in memory
1. Object instantiation
Object creation:
- Constructor creation (new)
- reflection
- Clone () : No constructor needs to be called. The current class implements the Cloneable interface and its Clone () method. (Latent copy)
- Use deserialization to retrieve a binary stream of objects from a file or network
- Third-party libraries
Object creation steps:
- Determine whether the object is loaded, linked, initialized: The JVM determines whether the class is loaded, and if not, the class’s bytecode files are loaded in parent delegate mode, and the corresponding objects are generated.
- Allocate space for objects: Calculate the size of objects, and then allocate a chunk of heap space for objects.
- If the memory is neat: using the pointer collision method to allocate memory for objects, that is, the used memory and unused memory are separated, with a pointer as the dividing point in the middle, allocating memory is to move the pointer to the free memory side of an equal distance of the object.
- If the memory is not tidy: The virtual machine needs to maintain a list of available memory, find a block of memory from the list to allocate to the object instance, and update the list. This method is also called the free list method.
- Handling concurrency security issues:
- Using CAS mechanism, retry on failure, region locking to ensure atomicity.
- Each thread is allocated a TLAB.
- Initialize allocated space: All properties are initialized by default.
- Set object headers: Metadata information for classes, HashCode, GC information for objects, and lock information are stored in object headers
- Execute the init() method to initialize
2. Memory layout of the object
- Object header (two parts) :
- Runtime metadata:
- Hash code (mainly used to refer to the address of the current object)
- GC Generational age (age counter)
- Lock status identification
- The lock held by the thread
- Biased thread ID
- Bias timestamp
- Type pointer: Points to metadata to determine the type of the object
- If it is an array object, the length of the array is also recorded
- Runtime metadata:
- Instance data:
- Store the true valid information of an object, define types, fields, etc. (including those inherited from the parent class)
- Rule: Variables defined by a parent class appear before subclasses; Fields of the same width (bytes) are allocated together; By default, a narrow variable of a subclass may be inserted into a gap of a parent variable.
- Fill it: it is not necessary to be there and acts as a placeholder.
3. Object access location
How does a JVM access an instance of an object inside it via an object reference in a stack frame?
Two ways:
- Handle access:
- There is a handle pool in the heap space, one object corresponds to one handle, mainly recording two information, a pointer to the object instance, and a pointer to the object type (method area) data
- Disadvantages: Disadvantages are the need to maintain an additional handle pool space in the heap space and the need to maintain a variable area access entity in the handle, which is inefficient.
- Advantages: The object address maintained by the stack space is relatively stable. If the object in the stack space moves, only the variables in the handle need to be changed, and the references in the stack need not be changed.
- Direct pointer:
- Unlike handle access, an object reference points directly to an object instance, which has a pointer to the object type data in the method area.
- Disadvantages: If the object is moved, the reference address of the stack space needs to be changed.
- Advantages: Compared with handle access, direct access is more efficient and saves some memory.