Deep understanding of the creation of---objects for Java virtual Machines (eight)

Source: Internet
Author: User

1. Object creation Process

Because the class is loaded is a very complex process, so here for the time being skipped, the following will be explained in detail, the default is the loaded class. Emphasis is placed on the object creation process.

Note: The last step of the Init method is a block of code and a constructor method.

The above is the general diagram, the following steps to explain in detail

A: How virtual machines allocate memory for objects

1. Pointer collision: When heap memory is normalized, it can be seen as half the contiguous memory being occupied and half contiguous memory idle. So when creating a new object, allocate space from the free memory to the new object

2. Idle list: Heap memory is not contiguous, this requires the virtual machine to maintain a memory list, used to record whether the current memory is occupied, when the newly created object, query the table to see where there is idle and allocated to new objects occupy, so this way is called the idle list

The virtual machine chooses which memory allocation method mainly depends on the heap memory is structured. Heap memory is structured with garbage collector policy determination. When the garbage collector policy can be implemented to clean up heap memory, then the virtual machine uses a pointer collision mode, otherwise use the Idle list method

B: Thread safety issues

At high concurrency, multiple objects are created at the same time as heap memory. When one of the free memory in the free list is allocated to one of the objects, it has not been available to update the free list. Then the new object comes in and is allocated the same memory space. Then a thread-safe issue arises.

How to resolve:

A: Using thread synchronization (locking), a thread comes in and locks. Don't let the second thread in.

B Local thread allocation cache: Allocates heap memory to each thread by assigning a separate zone, that is, each thread operates its own zone, with no impact.

C: Initialize the object

After any object is created, the value is initialized. The object already exists in the heap after this step

D: Execution Construction method

The constructor method assigns a value to the property in the object.

Deep understanding of the creation of---objects for Java virtual Machines (eight)

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.