What did the JVM do after the Java object was created--new?

Source: Internet
Author: User

Java object creation procedure 1. Class Load Check

Virtual opportunity to a new directive, first checks whether the parameter of the directive can be positioned in a constant pool for a symbolic reference of a class, and checks whether the class represented by the symbol reference has been loaded, parsed, and initialized. If not, then the class loading process is appropriate. (I will then write a blog about the class loading sequence and process, and add the connection address here)

2. Allocating memory space

After the class load check passes, the JVM allocates memory for the new object in the heap. The size of the memory required by the object is fully determined after the class is loaded (about how to calculate the amount of memory required for an object I'll write a blog post and add a link later). Allocating space to an object is equivalent to dividing a certain size of space on the Java heap. If the Java heap is continuously allocated, the object is stored on one side, the other side is idle, and a pointer is placed in the middle to serve as the indicator of the cutoff point, then allocating space for the object only needs to move the position of the object in the size of the pointer to the idle side. This method is called "Pointer Collision" (bump the pointer). If the Java heap is not continuously allocated, the virtual machine needs to maintain a list of each assigned object and unallocated spatial information. When you need to allocate space, find a large enough free space to allocate to the new object and then update the records on the list, which is called the "Free list".
The choice of which allocation method is determined by whether the Java heap is continuous, and whether the Java heap is structured and whether the garbage collector used has a compression collation function. Therefore, when using serial, parnew and other compact process collectors, the system uses a pointer collision, and the use of CMS based on the mark-sweep algorithm of the collector, usually using a free list.

3. Set the object basic information

After the object is allocated to space, the JVM initializes the allocated memory space to a value of 0 (excluding the object header). The object header is not initialized with the 0 value setting because the virtual machines have the necessary basic information set for the object. Information such as which class the object is an instance of, if the metadata information for the class is found, the object's hash code, the object's GC generational age, and so on. This information is stored in the object's object header and is called "Mark Word". (The memory layout of the object I will write two blog details on the next, and here to add the connection address.) )

4. Initialization of the programmer's wishes and calling constructors

Once the above work is done, an object has been created for the JVM. But from the Java program perspective, object creation is just beginning: first the program initializes the object according to the programmer's definition (such as defining the member variable private int age = 10, which sets the value of age to 10) so that a truly usable object is fully generated. The program then calls the constructor method. At this end, an object is completely complete with the creation and initialization work and can be used.

Example
public   class  initdemo  { private  int  age = 10 ; public  initdemo         () {System.out.println ( "before:"  + age);        Age = 20 ;    System.out.println ( "after:"  + age); } public  static   void  main  (string[] args) {Initdemo demo = new Initdemo (); /** output Result: before:10 after:20 */}}}  

Disclaimer: The article is a summary of the "in-depth understanding of Java Virtual Machine--JVM advanced features and Best practices" section of the second edition of the relevant chapters.

What did the JVM do after the Java object was created--new?

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.