In a Java program, creating an object usually requires a new keyword, but in a virtual machine the process is a bit more complicated, including class loading, memory allocation, initialization of 0 values, and a series of steps.
Let's look at how the JVM creates an object (which is limited to different Java objects, excluding arrays and class objects) 1 object creation 1.1 class initialization
When the JVM encounters a new instruction (not a concept with the New keyword), it first checks whether the instruction navigates to a class's symbolic reference in a constant pool, and checks whether the class represented by the symbolic reference has been loaded, parsed, and initialized. If not, you must first perform the initialization of your class.
1.2 Dividing space
Next is to divide a space in the heap, the size of the space by the class to determine, after class loading, the size of an object has been determined. There are two possible scenarios for this Division
(1) The space of the heap is regular, the used part is on one side, the unused part is on the other side, there is a pointer to the unused part of the head, each move this pointer is OK, this method is called "pointer collision".
(2) The heap of space is scattered, using and unused parts of the cross arrangement, this time need a maintenance of a list, record which memory is available, in the allocation of a large enough memory to allocate, this method is called "Free list." It is clear that this approach produces a lot of memory fragmentation.
The actual allocation method is determined by the garbage collection algorithm adopted by the virtual machine, depending on whether the garbage collector has a compression collation function (Campact). Therefore, when using serial,parnew to wait for the compact process collector, the system uses a pointer collision, while using the CMS based on the mark-sweep algorithm collector, using the idle list.
How to guarantee thread security for object creation.
Object creation is a frequent behavior in virtual machines, and how to ensure thread safety when moving pointers. There are two ways to solve this problem.
1 using cas+ failure retry to ensure the atomic nature of update operation certificate
2 The memory allocation action is divided into different spaces according to the thread. Allocate a small chunk of space to each memory in the heap, called the local thread allocation buffer (TLAB). When the thread allocates memory, it is allocated on its own tlab, and when the Tlab is used, the synchronization lock is used. 1.3 Assign 0 value
After the memory allocation is complete, the virtual machine initializes the allocated memory space to a value of 0 and, if Tlab is used, the process is performed in Tlab. This step ensures that the Java instance fields are assigned different assignments in Java code to use 1.4 to set the object header
Virtual machines to make the necessary settings for an object, such as the instance of which class is in the object, how to find the metadata information for the class, the hash code of the object, the age of the object's GC, and so on. This information is stored in the object header. 1.5 <init> Instruction
Upon completion of the above work, for the virtual machine, an object has been created, taken from the Java object's point of view, has not been initialized,<init> method has not been executed, all fields are still 0 values. This <init> method can be understood as an object's construction method 2 object's memory layout
In a hotspot virtual machine, the layout of an object in memory is divided into 3 parts: the object header (header), the instance data (Instance), and the 2.1 object headers are populated
The object header consists of two parts, the first part is used to store the Run-time data of the object itself, which is 32bit and 64bit, respectively, in 32-bit and 64-bit virtual machines, and is officially called "Mark Word". The object header is designed as a data structure independent of the object structure, and the 32bit storage is as follows:
Hotspot Virtual Machine Object Header
| Storing content |
Sign bit |
State |
| Object hash code, object generational information |
01 |
Not locked |
| Pointer to lock record |
00 |
Lightweight lock |
| Pointer to weight lock |
10 |
Heavyweight lock |
| Empty, no need to record I information |
11 |
GC Tags |
| Biased thread ID, biased timestamp, object generational information |
01 |
can be biased |
Another part of the object header is a type pointer, a pointer to its class metadata that the virtual machine uses to determine which class the object points to. Not all virtual machine implementations must retain this type of pointer on the object data, which means that the Lookup object's metadata information does not necessarily go through the object itself. In addition, if the object is an array, the object header must also have a piece of data to record the length of the array.
2.2 Instance data fields
This section is the valid information that the object is actually storing, that is, the various types of field content defined in the program code. Whether it's inherited from the parent class or defined in subclasses, it needs to be documented. The order in which this part is stored is affected by the order in which the virtual machine assigns policy parameters and fields in the Java source definition. The default allocation policy for hotspot virtual machines is long/doubles, INTs, Shorts/chars,bytes/booleans, oops, and from an allocation policy perspective, fields of the same width are always assigned together. Under this premise, the variables of the parent class appear before the subclass. If the Compactfileds argument is specified as true, the narrower variable in the subclass may be inserted into the void of the parent class variable. 2.3 Snap To fill
This part does not necessarily have, and has no practical meaning, simply to populate the data. The HotSpot VM's automatic memory management system requires that the object start address must be an integer multiple of 8 bytes, that is, each object must be an integer multiple of 8 bytes in size, and if the instance field is not satisfied, it should be added to it.