Talking about Java Object Creation

  Java is an object-oriented language. Objects are created all the time during the running of Java programs. At the language level, creating an object (cloning, deserializing) is just a new keyword, but not at the virtual machine level. Let's take a look at the steps for creating objects at the virtual machine level:
  (1) When the virtual machine encounters a new instruction, first check whether the parameters of this instruction can locate a symbolic reference of a class in the constant pool, and check that the symbolic reference represents Whether the class has been loaded, resolved and initialized. If not, then the initialization process of the class must be performed first.

  (2) After the class loading check is passed, the virtual machine allocates memory for the new object. The size of the memory required by the object can be completely determined after the class is loaded. Allocating space for an object is nothing more than dividing a block of memory of a certain size from the Java heap. There will be two problems in this place:
  ① If the memory is regular, then the virtual machine will use the pointer collision method to allocate memory for the object. It means that all the used memory is on one side, the free memory is on the other side, and a pointer is placed in the middle as an indicator of the demarcation point. Allocating memory is just moving the pointer to the free side by a distance equal to the size of the object. If the garbage collector chooses the compression algorithm based on Serial and ParNew, the virtual machine adopts this allocation method.
  ② If the memory is not regular, the used memory and the unused memory are interleaved, then the virtual machine will use the free list method to allocate memory for the object. It means that the virtual machine maintains a list to record which memory blocks are available. When re-allocating, it finds a space large enough to be allocated to the object instance from the list, and updates the contents of the list. If the garbage collector chooses the mark-sweep algorithm based on CMS, the virtual machine adopts this allocation method.
  Another problem is to ensure the thread safety of new objects in time. Because the virtual machine may be allocating memory to object A, the pointer has not had time to be modified, and object B also uses the original pointer to allocate memory at the same time. The virtual machine uses CAS coupled with failed retry to ensure the atomicity of update operations and TLAB to solve this problem.
  (3) At the end of memory allocation, the virtual machine initializes the allocated memory space to a zero value (excluding the object header). This step ensures that the instance fields of the object can be used directly in Java code without assigning initial values, and the program can access the zero values ​​corresponding to the data types of these fields.
  (4) Make necessary settings for the object, such as which class instance this object is, 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, which are stored in the object of the object. in the head.
  (5) Execute the <init> method to initialize the object according to the programmer's wishes.

  At this point, a truly usable object is completely generated.
  The purpose of establishing the object above is to use the object, and the Java program needs to operate the specific object on the heap through the reference data on the stack. For example, we wrote:

Object obj = new Object();

  After new Object(), there are actually two parts, one is the class data (such as the Class object representing the class), and the other is the instance data.
  Since reference is only a reference obj pointing to the object new Object() in the Java virtual machine specification, it does not specify how obj should be located and the specific location of the object in the heap, so the object access method also depends on the virtual machine. Depends. There are two main ways:
  (1) Handle access. A handle pool is divided in the Java heap, obj points to the handle address of the object, and the handle contains the address of the class data and the address of the instance data
  (2) Pointer access. The address where all instance data and class data are stored in the object, obj points to this object, and the
  HotSpot virtual machine uses the latter, but the former's object access method is also very common.  

Guess you like

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