当前位置:网站首页>Object creation process of JVM

Object creation process of JVM

2022-06-11 21:05:00 Just number six Z

Java Object creation process ( Five steps )

1. Class loading check

virtual machine Meet a new When the command , First, we will check whether the parameters of this instruction can locate the symbol reference of this class in the constant pool , And check whether the class represented by this symbol reference has been loaded 、 Parsed and initialized . without , The corresponding class loading process must be performed first .

2. Allocate memory

After the class load check , The virtual machine is new The object that comes out allocates memory .

The memory size required for the new object is determined after the class is loaded , What we need to do now is to convert a certain size of memory from Java Divide it up in the heap .

There are two ways of distribution : Pointer collision and Free list .

  • If the memory space is regular

    If memory is regular , So the virtual machine will use Pointer collision method (Bump The Pointer) To allocate memory for objects . It means that all used memory is on one side , Free memory on one side , There is a pointer in the middle as an indicator of the dividing point , Allocating memory is just an example of moving the pointer to the free side for a period equal to the size of the object . If the garbage collector chooses Serial、ParNew This compression algorithm is based on , Virtual machines are distributed in this way , Generally used with compact( Arrangement ) Process collector , Use pointer collisions .

  • If the memory is out of order

    If memory is not regular , Used and unused memory interlace with each other , Then the virtual machine uses the space-time free list method to allocate memory for the object . The virtual machine maintains a list , Record the memory blocks that can be used , When reallocating, find a large enough space in the list to divide it into the formation instance , And update the list . This distribution becomes “ Free list (Free List)”.

  • Select which allocation method is determined by Java Whether the pile is regular or not , and Java Whether the heap is regular or not depends on whether the garbage collector adopted has compression and collation function .

Add : Memory allocation concurrency problem

There is a very important problem when creating objects , It's thread safety , In actual development , Creating objects is a frequent thing , As a virtual machine , You have to be thread safe .

  • CAS + Failure to retry

    CAS It's an implementation of optimistic lock . Do not lock each time, but assume that there is no conflict to complete an operation , If the conflict fails, try again , Until we succeed .

    Virtual machine adopts CAS The atomicity of update operation is guaranteed by the way of failure retry .

  • TLAB

    In advance for each thread Eden Allocate a piece of memory ,JVM When allocating memory to objects in a thread , First, in the TLAB Distribute , When the object is greater than TLAB Memory remaining in or TLAB When I ran out of memory , Then use the above CAS Distribute .

3. Initialize zero value

After the memory allocation is complete , The virtual machine needs to initialize the allocated memory space to zero ( Object headers are not included ), This step ensures that the instance field of the object is in the Java Code can be used directly without initial value , The program can access the zero value corresponding to the data type of these fields .

4. Set object header

The virtual machine should make necessary settings for the objects , For example, this object is an instance of that class 、 How can I find the metadata information for a class 、 Object's hash code 、 Object's GC Information such as generational age . This information is stored in the object header . in addition , Depending on the current running state of the virtual machine , Such as whether to enable biased lock, etc , Object headers can be set differently .

5. perform init Method

stay Java From the perspective of procedure , Initialization just started . Initialize member variables , Execute instantiation code block , Call the constructor of the class , The first address of the object in the heap is assigned to the reference variable .

原网站

版权声明
本文为[Just number six Z]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206112054043998.html