[Switch] JVM object creation

Reprinted from: http://blog.csdn.net/u010723709/article/details/47281349

 

In a Java program, creating an object usually requires a new keyword, but in a virtual machine, this process is a bit complicated, including a series of steps such as class loading, memory allocation, and initializing zero values.

 

Let's take a look at how the JVM creates an object (the objects in this are 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 locates a symbolic reference of a class in the constant pool, and checks whether the class represented by the symbolic reference has been loaded and resolved. and initialized. If not, the initialization of the class must be performed first.

 

1.2 Divide the space

 

The next step is to divide a space in the heap. The size of this space is determined by the class. After the class is loaded, the size of an object is already determined. For this division, there may be two cases

 

(1) The space of the heap is regular, the used part is on one side, and the unused part is on the other side. There is a pointer to the head of the unused part. It is enough to move this pointer every time. This method is called for "pointer collision" .

 

(2) The space of the heap is scattered, and the used and unused parts are arranged alternately. At this time, it is necessary to maintain a list to record which memory is available, and find a large enough memory for allocation when allocating. This kind of The method is called " free list". Obviously this way will generate a lot of memory fragmentation .

 

Which allocation method is actually used is determined by the garbage collection algorithm adopted by the virtual machine , and mainly depends on whether the garbage collector has a compaction function (campact) . Therefore, when using Serial and ParNew to wait for the collector of the Compact process, the system adopts pointer collision, while using CMS, a collector based on the Mark-Sweep algorithm, adopts the free list.

 

How to guarantee thread safety of object creation?

 

Object creation is a frequent behavior in virtual machines. How to ensure thread safety when moving pointers? There are two solutions to this problem

 

1 Using CAS+failure retry to ensure the atomicity of updating the certificate

 

2. Divide the memory allocation action in different spaces according to the thread. A small block of space is allocated in the heap for each memory, called the Thread Local Allocation Buffer (TLAB). When a thread allocates memory, it allocates on its own TLAB. When the TLAB is used up, it uses a synchronization lock.

 

1.3 Assign zero value

 

After the memory allocation is completed, the virtual machine initializes the allocated memory space to a zero value. If TLAB is used, this process is performed in TLAB. This step ensures that Java instance fields can be used with different assignments in Java code

 

1.4 Set the object header

 

The virtual machine needs to make necessary settings for the object, such as which class instance is the object, how to find the metadata 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 header.

 

1.5 <init> directive

 

After the above work is completed, for the virtual machine, an object has been created, and from the perspective of Java objects, it has not been initialized, the <init> method has not been executed, and all fields are still zero. This <init> method can be understood as the constructor of the object

 

2 Memory layout of objects

 

In the HotSpot virtual machine, the layout of objects stored in memory is divided into three parts: object header (Header), instance data (Instance Data) and filling it

 

2.1 Object header

 

The object header includes two parts. The first part is used to store the runtime data of the object itself. The length of this part is 32bit and 64bit in 32-bit and 64-bit virtual machines respectively, officially called "Mark Word". The object header is designed as a data structure independent of the object structure , and the 32bit storage contents are as follows:

 

 

HotSpot virtual machine object header
store content flag bit condition
Object hash code, object generation information 01 not locked
pointer to lock record 00 Lightweight Lock
pointer to weight lock 10 Heavyweight Lockdown
Empty, no need to record i information 11 GC marker
Bias thread ID, bias timestamp, object generation information 01 can be biased

Another part of the object header is the type pointer, that is, the pointer to the object's class metadata, and the virtual machine uses this pointer to determine which instance of the class the object points to. Not all virtual machine implementations must keep this type pointer on the object data, that is, looking up the metadata information of the object does not necessarily have to go through the object itself. In addition, if the object is an array, the object header must also have a piece of data for recording the length of the array.

2.2 Instance data field

 

This part is the effective information that the object actually stores, that is, the field contents of various types defined in the program code. Whether it is inherited from the parent class or defined in the subclass, it needs to be recorded. The storage order of this part is affected by the order in which the virtual machine allocation strategy parameters and fields are defined in the Java source code. The default allocation strategy of the HotSpot virtual machine is long/doubles, ints, shorts/chars, bytes/booleans, oops. From the perspective of the allocation strategy, fields of the same width are always allocated together . Under this premise, the variables of the parent class will appear before the child class . If the compactFileds parameter is specified as true, then the narrower variables in the subclass may be inserted into the gap of the parent class variable.

 

2.3 Align padding

This part doesn't have to be there, and it doesn't really make sense, just to populate the data. The automatic memory management system of HotSpot VM requires that the starting address of the object must be an integer multiple of 8 bytes, that is, the size of each object must be an integer multiple of 8 bytes. If the instance domain is not satisfied, it must be supplemented.

 

Objects are created in order to use objects. My Java objects need to manipulate specific objects on the heap through the reference data on the stack.

 

Summarize:

Suppose there is a class named Dog

1. Even if the static keyword is not explicitly used, the constructor is actually a static method. Therefore, when an object of type Dog is created for the first time (the constructor can be regarded as a static method), or when the static method/static field of the Dog class is accessed for the first time, the Java interpreter must look up the classpath to locate the class of Dog.class .

2. Then load Dog.class, and all actions related to static initialization will be performed. Therefore, static initialization is only done once when the Class object is first loaded.

3. When creating an object with new Dog(), it first allocates enough storage space for the Dog object on the heap.

4. This storage space will be cleared, which will automatically set all basic type data in the Dog object to the default value, and the reference will be set to null.

5. Perform all initialization actions that appear in the field definition.

6. Execute the constructor.

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326701666&siteId=291194637