HotSpot virtual machine - object memory creation (discussed in this article is limited to ordinary objects, excluding arrays and Class objects, etc.)

    (1). When the virtual machine encounters the New instruction, it first checks whether the instruction parameter can locate a symbolic reference of a class in the constant pool, and checks whether the class representing the reference has been loaded, resolved and initialized. If not, the corresponding loading procedure must be performed.

   (2). After the class is loaded, the virtual machine allocates memory for the new object, and the memory size required by the object is determined after the class is loaded. The task of allocating memory for an object is equivalent to dividing a fixed-size memory in the Java heap. In the case of a single thread, there are generally two allocation strategies:
        2.1. Pointer collision
            is generally applicable to regular memory (memory Whether it is regular or not depends on the memory reclamation strategy), the work of allocating space is just moving the pointer to the free memory side by the distance of the object size.
        2.2.
            The free list is suitable for the case of irregular memory. In this case, the JVM will maintain a memory list to record which memory areas are free and what their sizes are. When allocating space to an object, go to the free list to find a suitable area and then allocate
       it, but it is impossible for the JVM to run in a single-threaded state all the time, which is too inefficient. Since allocating memory to an object is not an atomic operation, at least the following steps are required: searching the free list, allocating memory, modifying the free list, etc., which is unsafe. There are also two strategies for solving the security problem in concurrency:
        2.3. CAS
            In fact, the virtual machine adopts the method of CAS and failure retry to ensure the atomicity of the update operation. The principle is the same as the above.
        2.4. TLAB
            If CAS is used, it will still affect performance, so the JVM proposes a more advanced optimization strategy: each thread pre-allocates a small piece of memory in the Java heap, which is called the local thread allocation buffer (TLAB), When memory needs to be allocated inside the thread, it can be allocated directly on the TLAB to avoid thread conflict. Only when the memory of the buffer runs out and needs to be reallocated, the CAS operation will be performed to allocate a larger memory space. Whether the virtual machine uses TLAB can be configured through the -XX:+/-UseTLAB parameter (jdk5 and later versions enable TLAB by default.

    (3). After the memory allocation is completed, the virtual machine needs to initialize the allocated memory space to a zero value (excluding the object header). If TLAB is used, this work process can also be performed in advance of TLAB allocation. This step ensures that the instance fields of the object can be used directly in Java without assignment, and the program can access the zero values ​​corresponding to the data types of these fields.

    (4) The virtual machine needs to make necessary settings for the object, such as: which instance of this object is, how to find the element information of the class, the hash code of the object, the GC generation age of the object and other information. This information is stored in the object's object header. Depending on the current running state of the virtual machine, such as whether to enable the bias lock, the object header has different setting methods.

   (5) After executing the New instruction, the <init> method is executed to initialize the object according to the programmer's wishes, so that a real object is completely produced.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325625911&siteId=291194637
Recommended