Java Create object creation process Analysis

Source: Internet
Author: User
In the process of analyzing this line of code with Object obj = new Object (), what the JVM does behind it. First, check for the existence of a symbol reference for the class in the constant pool, and if not, load the class and parse and initialize allocating memory for objects

The size of the memory is determined when the class is loaded, as shown in the following figure:

issues to consider during the allocation of memory:
2.1 Whether the memory is structured
① assumes that memory in the Java heap is continuously structured, that is, the side of the heap is already used in memory space, while the other side is free space, then use the pointer to start free memory, when you need to allocate new space, just move the pointer backward to create space size position to complete the allocation of memory, This pattern of distribution is called "Pointer Collision”(Bump the pointer
② assumes that memory is not continuously structured, idle and used interleaved, then the JVM needs to record which chunks of RAM are available, allocate large enough space for object instances, and colleagues update the record list. This way is called "Free list"(Free List)
Instead, the method of bump the pointer or free list is determined by the JVM heap, and the regularity of the heap is determined by whether the garbage collector has the function of using the sub-cable. The use of serial, parnew, and so on with the compact process of the collector is a pointer collision, and the use of CMS this gives Mark sweep algorithm collector, using the Free list method.
2.2 objects are created very frequently and how to solve the unsafe problems caused by concurrency
Method One, synchronization, that is, synchronizing the action of allocating memory space ——-using CAS plus failed retries to ensure the atomicity of the update operation.
Method Two, Tlab, the memory allocation action is divided into different spaces on a per-thread basis, that is, each thread allocates a small chunk of memory, this way is called the local thread allocation buffer (thread native allocation buffer), This way you only need to lock up when assigning new Tlab
Whether to use the Tlab method can be determined by using-xx:+/-usertlabthe necessary setup after the memory allocation is completed

Such as: this instance is the instance class of the class's metadata information object's hash code object's GC generational information and so on
The above information is in the header of the object (object header), except that the other fields of the object are 0, so a new object has been generated, but has not yet been initialized. initialization of the object, executing the Init method

At this point, an object completes the creation process

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.