Creation of objects in a Java virtual machine

Source: Internet
Author: User

When a virtual opportunity comes to a new instruction, first check that the parameter of the directive is able to locate the symbolic reference of a class in the constant pool, and that the symbol reference indicates whether the class has been loaded, parsed, and initialized. If you do not have to perform the class's load process.

After the class load check, the virtual machine then allocates memory for the new object. The amount of memory required for an object is fully determined after the class is loaded, and the task of allocating space to an object is equivalent to splitting a certain size of memory from the Java heap. The way Java memory is allocated is divided into "pointer collisions"(Bump the Pointer) and "Free list"in two ways.

pointer Collisions : Assuming that the memory in the Java heap is perfectly structured, that all used memory and free memory are placed on both sides with a pointer as the indicator of the cutoff point, then allocating memory simply moves that pointer over the free space to a distance equal to the size of the object.

Idle list : If the memory in the Java heap is not structured, the memory used and the free memory are interleaved, there is no way for a simple pointer to move, the virtual machine must maintain a list, record those memory is available, those are unavailable, At the time of allocation, find a large enough space from the list to the object instance and update the records on the list. When using collectors with compact processes such as serial and Parnew, the system uses the allocation method as a pointer collision, while the use of CMS, which is based on the mark-sweep algorithm, usually uses a free list.

In order to prevent the memory allocation unsafe in the concurrency situation, the Java virtual Machine takes two methods to solve: one is to allocate the memory space the action synchronizes---actually the virtual machine uses the CAs to match the failure retry the way guarantees the update operation the atomicity The other is to divide the memory allocation actions into different spaces, that is, each thread preselected a small chunk of memory in the Java heap, called the local thread allocation buffer (thread local Allocation buffer,tlab). Which thread allocates memory, On which thread the Tlab is allocated, only the Tlab needs to be locked when it runs out and re-allocates memory. If the virtual machine uses Tlab, it can be done by-xx:+/-usertlab parameters.

After the memory allocation is complete, the virtual machine needs to initialize the 0 value to the allocated memory space. This step ensures that the object's real-field fields can be used directly in Java code without assigning an initial value, and the program can access the 0 value corresponding to the data type of those fields. Next, the virtual machines make the necessary settings for the object, such as which instance of the class is listed, how to find the metadata information of the class, the hash code of the object, the age of the GC for the object, and so on. This information is stored in the object header.

After the above work is complete, from the virtual machine point of view, a new object has been generated, but from the Java program perspective, object creation has just begun---<init> method has not been executed, all fields are not zero. So, in general, after executing the new command, the <init> method is followed and the object is initialized according to the programmer's wishes so that a real object is completed.

Memory layout of objects: Object Header (header), Real-column data (Instance), and aligned padding (Padding)

The object header of the hotspot virtual machine includes two pieces of information, the first part is used to store the object's own run-time data, such as hash code (HASHCODE), GC generational age, lock state mechanism, thread-held lock, biased thread ID, bias timestamp. Officially known as "Mark Word"

Store content Flag bit State
Object hash code, object generational age 01 Not locked
Pointer to lock record 00 Lightweight locking
Pointer to weight lock 10 Heavy-Lock
Empty, do not need to log information 11 GC Flag
Bias thread ID 01 can be biased

The other part of the object header is the type pointer, the pointer to its metadata, which the virtual machine uses to point to its class metadata pointer, through which the virtual machine determines which class the object is from.

The Instance data section is the valid information that the object actually stores, which is the various types of field information defined in the program code.

The third part, there is no special meaning, only play the role of the placeholder, because the hotspot VM Automatic Management system requires that the object address must be a 8-byte integer, in other words, the size of the object must be 8-byte integer times. You need to get through the whole.

Object Access Location:

The object is created for use, and our Java program needs to manipulate the concrete objects on the heap through the reference data on the stack. Because the reference type specifies a reference to the object in the Java Virtual Machine specification, and returns the exact location in which the reference should be used to locate and access objects in the heap, this access is also determined by the virtual machine implementation. There are two ways to access the current mainstream, using a handle and a direct pointer.

If you use a handle to access, then the Java heap will be divided into a block of memory as a handle pool, reference is stored in the object's handle address, and the handle contains the object instance data and the type of data specific address information.

If you use direct pointer access, the layout of the objects in the Java heap must consider how to place information about the access type data, and the object address is stored in reference.

There are advantages to both object access, and the greatest benefit of using a handle to access is that the reference is stored in a stable handle address that changes only the instance data pointer in the handle when the object is moved, and reference itself does not need to be modified.

The biggest benefit of using direct targeting is faster, which saves time spent on pointer positioning, and because objects are accessed very frequently in Java, this kind of overhead can also be very large. In the case of Sun HotSpot, it uses the second way to access objects.

Creation of objects in a Java virtual machine

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.