About Java object creation

Source: Internet
Author: User

Java is an object-oriented language, and Java programs are created with objects all the while running. At the language level, creating objects (cloning, deserialization) is a new keyword, but not at the virtual machine level. Let's take a look at the steps to create an object at the virtual machine level:
(1) The virtual opportunity to a new directive, first to check whether the parameter of this directive can be located in a constant pool of a class symbol reference, and check whether the symbol reference represents the class has been loaded, parsed and initialized. If not, the initialization of the class must be performed first.

(2) After the class load check passes, the virtual machine allocates memory for the new object. The amount of memory required for an object can be fully determined after the class is loaded, and allocating space for an object is nothing more than dividing a certain size of memory from the Java heap. This place will have two problems:
① If the memory is structured, the virtual machine will use a pointer collision method to allocate memory for the object. It means that all used memory on one side, free memory on the other side, the middle of a pointer as a demarcation point indicator, allocating memory is simply to move the pointer to the idle side of the size of the object is equal to the distance. If the garbage collector chooses serial, parnew this compression-based algorithm, the virtual machine uses this method of allocation.
② If the memory is not structured, the memory used and the unused memory are interleaved with each other, then the virtual machine uses the Idle list method to allocate memory for the object. This means that the virtual machine maintains a list of which memory blocks are available, and then allocates a large enough space from the list to the object instance, and updates the contents on the list. If the garbage collector chooses a CMS based on the tag-purge algorithm, the virtual machine uses this allocation method.
Another issue guarantees thread safety in the new object in a timely manner. Because the virtual machine may be allocating memory to object A, the pointer has not yet been modified, and object B uses the original pointer to allocate memory. The virtual machine uses CAS with the failure retry method to ensure the atomicity of the update update operation and Tlab two ways to solve the problem.
(3) The memory allocation ends, and the memory space that the virtual machine allocates is initialized to a 0 value (excluding the object header). This step ensures that the instance fields of an object can be used directly in Java code without assigning an initial value, and the program can access the 0 values corresponding to the data types of those fields.
(4) Make the necessary settings for the object, such as which instance of the class it is, how to find the metadata information of the class, the hash code of the object, the age of the object's GC, and so on, which is stored in the object header of the object.
(5) Execute the <init> method to initialize the object according to the programmer's wishes.

To this, a truly usable object is fully generated.
The above objects are created to use objects, and Java programs need to manipulate the concrete objects on the heap through the reference (reference) data on the stack. For example, we wrote a sentence:

Object obj = new Object ();

After the new Object (), there are two parts, some of which are class data (such as class objects representing classes), and part of the instance data.
Since reference is simply a reference to object new object () in the Java Virtual Machine specification, and does not specify the way in which obj should be located and access the object's location in the heap, the way objects are accessed depends on the virtual machine. There are two main ways of doing this:
(1) Handle access. The Java heap divides a handle pool, and 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. Object that stores all the instance data and the address of the class data, obj points to the object
The hotspot virtual machine uses the latter, but the former object access method is also very common.

About Java object creation

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.