The creation of objects in memory, the structure of objects, and how they are accessed.
I. Creation of objects
At the language level, the creation of objects is nothing more than a new keyword, so what is the process in a virtual machine?
(a) determine whether the class is loaded . Virtual opportunity to a new command, first check whether the parameters of this directive can be located in the constant pool of a class symbol reference, and check whether the symbol represents the class is loaded, parsed and initialized. If you do not complete this procedure, you must perform the loading of the corresponding class.
(ii) Allocate space on the heap for the object . The amount of space an object needs is determined after the class is loaded. The object is then allocated a fixed-size space on the heap. There are two ways of assigning it:
i. first if using Serial parnew Span style= "font-family: Arial" > etc with compact java The heap in memory is regular, just move a distance as a pointer to the dividing point of use and unused space.
Ii. Second If you use a CMS based on the mark-sweep algorithm,Java memory is not structured, A virtual machine maintains a list of memory usage, which is called a "free list".
Virtual machines allocate space for objects very often, and if you assign objects to multiple threads at the same time, concurrency security control is involved. There are generally two solutions:
(1) The first is to synchronize the allocation of memory space actions - use cas match the failure retry method to ensure atomicity of the update operation.
(2) The second is to allocate memory allocations in different spaces, with each thread java pre-allocated a small chunk of memory in the heap, Called the local thread allocation buffer ( thread local allocation buffer, Tlab tlab On the assignment. Only tlab finished and needs to be assigned a new tlab
(iii) initialize the memory space . After the memory allocation is complete, the virtual opportunity initializes the allocated space to a value of 0 (excluding the object header), which can also be done in advance to tlab allocation if Tlab is used .
(iv) Sets the object header for the object . Next, the virtual machines set the object's object header. This includes the object's hash code, class element information,GC generational age, and so on. This information is placed in the object header.
(v) Executive <init> Method to initialize the members within the object.
After performing these five steps, an object is actually generated.
Second, the memory layout of the object
in memory, the object storage layout can be divided into three parts: object Header Sample Data (instance data) and align padding (Padding)
i. Object Header : Includes two pieces of information. The first section is used to store the runtime data of the object itself, such as hash code, GC generational age , lock status, thread hold lock, and so on. The length of this part of the data is in 32 or Span style= "font-family: ' Times New Roman '" >64 bit, officially called " mark word ". The other part of the object header is the type pointer, which is a pointer to its class element, through which an instance of the class is determined by this pointer. (If java An array when the object The object header must also have a piece of data to record the length of the array. Because java Array metadata does not have an array size record)
Ii. Instance Data : This part is the place where the object is actually stored for effective information.
Iii. Align fills : This part is not required, but it acts as a placeholder. Because the HotSpot virtual machine requires that the object start address must be a multiple of 8 bytes.
Iii. how objects are accessed
We can manipulate the concrete objects on the heap by using the reference data on the stack. There are two ways to access a specific object: a handle and a direct pointer.
handle: A handle pool is partitioned in the Java heap that is used to hold the object's instance address and type address. the reference in the stack is just the address of one of the handles in the handle pool. The advantage is that when a garbage collection is made and moved, the object address changes and reference data is not changed.
direct pointer : reference points directly to the address of an object. The advantage is that the speed is fast, saving the time cost of a location.