The object creation process in Java

Source: Internet
Author: User

The object creation process described in this article is limited to ordinary Java objects and does not include arrays and class objects.

1. Class Load Check

Virtual opportunity to a new directive, first check whether the parameters of the directive can be located in the constant pool of a class's symbolic reference, and check whether the symbol reference represents the class has been loaded, parsed and initialized.

2. Allocating memory for new objects

The size of the memory required for an object can be fully determined after the class is loaded, and the task of allocating space to an object is equivalent to dividing a size of memory from the Java heap. There are two main ways of dividing:

A, the pointer collision

If the memory in the Java heap is absolutely regular, all the used memory is on one side, the unused memory is on the other side, and a pointer is placed in the middle to serve as the indicator of the demarcation point, the allocated memory is simply the

The pointer points to the free space over there to move a distance equal to the size of the object.

B, Idle list

If the memory in the Java heap is not regular, the memory used and the unused memory are interleaved with each other, the pointer collision cannot be used, and the virtual machine must maintain a list to record which memory is available, at the time of allocation

Find a large enough space in the list to divide the object instance and update the records on the table.

3. Follow-up work

After the memory allocation is complete, the virtual machine needs to initialize the allocated memory space to a value of 0 (excluding the object header) and then make the necessary settings for the object (primarily the setting of the object header information).

At this point, a new object appears in the virtual machine, but from the programmer's point of view, object creation is just beginning, and the <init> method needs to be executed to initialize the object according to the programmer's wishes.

Appendix

The layouts that objects store in a virtual machine can be divided into 3 areas: Object headers, instance data, and alignment fills.

The object header contains two pieces of information: The first part is used to store the object's own run-time data (such as hash code, GC generational age, lock status flag, thread-held lock, biased thread ID, and bias timestamp, etc.). Object Header

The other part is the type pointer, which is a pointer to its class metadata, and if the object is a Java array, the object header must also have a piece of data to record the length of the array.

The instance data section is a valid piece of information that the object actually stores, that is, the various types of field content defined in the program code.

The alignment padding does not necessarily exist, only the placeholder, because the hotspot VM's automatic memory management system requires that the object start address must be an integer multiple of 8 bytes.

The object creation process in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.