How do HotSpot virtual machines create, layout, and Access objects in the Java heap? __java

Source: Internet
Author: User
Java memory area and memory overflow exception

1. Overview (Why do you want to know how virtual machines use memory?) )
2. Runtime data region (how the memory in the virtual machine is divided.) )
3. HotSpot Virtual Machine Object Quest (HotSpot virtual machines How objects are created, laid out, and accessed in the Java heap.) )
4. Combat: OutOfMemoryError Exception (which part of the region, what kind of code and operation may cause memory overflow exception.) 3, how HotSpot virtual machines are created, how they are laid out, and how they are accessed in the Java heap.

3.1 Creation of objects

@ Language, creating objects is usually just a new keyword, but in a virtual machine, the creation of objects (limited to ordinary Java objects, excluding arrays and Class objects) is a process.
A: class load – Allocate memory – Initialize – Set object information

refine the above process:
①, virtual opportunity to a new instruction , first check whether the parameter of this instruction can navigate to a class's symbolic reference in a constant pool , and check whether the class represented by the symbol reference has been loaded, parsed, and initialized . If not, then the corresponding class loading process must be performed first.
②, after the class load check passes, the next virtual machine allocates memory for the newborn object. The amount of memory required for an object is completely determined after the class is loaded, and the task of allocating space for the object is equivalent to dividing a certain size of memory from the Java heap. allocation method : pointer collision, idle list.
, after the memory allocation is complete, the virtual machine needs to initialize the allocated memory space to 0 values (excluding the object header), and if you use Tlab, the process can also be done in advance to Tlab allocation .
, Next, the virtual confidential to the object to make the necessary settings , such as what this object is an instance of the class, how to find the class metadata information, the object's hash code, the object's GC generational age and other information. This information is stored in the object header .

Note: after all the above work is done, the virtual machine perspective: A new object has been created. Java Program Perspective: Object creation is just beginning--init method has not been executed, all fields are still zero. After the new instruction is executed, the Init method is followed and the object is initialized according to the programmer's will.

@ Two memory allocation methods (pointer collision, idle list) is a separate process.
A : ①, pointer collision: assuming that the memory in the Java heap is absolutely regular , all the used memory is placed on one side, the free memory on the other side, with a pointer in the middle of the indicator as a demarcation point, The allocated memory is just to move that pointer to the free side of a distance equal to the size of the object.
②, idle list: if the memory in the Java heap is not regular , the memory used and the idle memory are alternating with each other, there is no way to simply collide with the pointer, the virtual machine must maintain a list of which memory blocks are available, Find a large enough space in the list to assign to the object instance and update the records on the list.

what type of allocation should you choose?
A: Whether the Java heap is structured or not, and whether the Java heap is structured is determined by whether the garbage collector used has a compression collation function .
Therefore, in the use of serial, parnew and so on with the Compact process collector , the system uses the allocation algorithm is a pointer collision , and the use of CMS based on the mark-sweep algorithm collector , An idle list is usually used.

thread safety issues that may occur when allocating memory
It is possible that a memory is being allocated to object A, the pointer has not yet been modified, and object B uses the original pointer to allocate memory.

@ How to solve the problem.
Answer: two schemes.
①, synchronize the action of allocating memory space --in fact, the virtual machine uses CAS with failed retries to ensure the atomic nature of the update operation;
②, the action of allocating memory is divided into different spaces by thread , that is, each thread allocates a small chunk of memory in the Java heap, called the Local thread allocation buffer (Tlab). Which thread allocates memory, is allocated on the tlab of which thread, and only needs to sync the lock when Tlab is finished and allocating a new tlab.

@ virtual Machine if using Tlab.
A: You can set it by-xx:+/-usetlab parameters. To add parameters specifically, set the virtual machine startup parameters in the light of Eclipse and idea

@ After allocating memory, why do you want to initialize the operation?
A: This step ensures that the instance fields of an object are used directly in Java code without assigning an initial value, and that the program can access the 0 values corresponding to the data type of those fields.

memory layout for 3.2 objects

The layout that an object stores in memory can be divided into 3 areas: Object Header (header), instance data (Instance), and alignment padding (Padding).

The object header includes two parts of information:
The first part is used to store run-time data for the ① object itself. such as hash code, GC generational age, lock status flag, thread held lock, biased thread ID, biased time stamp, and so on, this part of the data length in 32-bit and 64-bit virtual machine (not open the compression pointer) in the 32bit, respectively, 64bit, the official call it "Mark Word".
The other part is the ② type pointer , the pointer to its class metadata that the virtual machine uses to determine which class the object is an instance of.

@ Why when an object is a Java array, there must also be a piece of data in the object header to record the length of the array .
A: Because a virtual machine can determine the size of a Java object through the metadata information of a normal Java object, the size of the array cannot be determined from the metadata of the array .

The instance data Part is the valid information that the object is actually stored , and the various types of field content defined in the program code , whether inherited from the parent class or defined in a subclass.

@ This part of the data is stored in the order.
A: It is affected by the virtual machine allocation policy Parameters and the order in which the fields are defined in Java source code .
The default allocation policy for hotspot virtual machines is: Longs/doubles, INTs, Shorts/chars, Bytes/booleans, oops (ordinary Object pointers), as can be seen from the allocation policy, ① fields of the same width are always assigned together .
When this prerequisite is met, thevariables defined by ② in the parent class appear before the subclass .
If the Compactfields parameter value is True (the default is True), the narrower variable in the ③ subclass may also be inserted into the void of the parent class variable .

alignment padding does not necessarily exist, nor does it have a special meaning, but only acts as a placeholder.

3.3 Access positioning of objects

We need to use the reference data on the stack to manipulate the concrete objects on the heap .
Because the reference type specifies only a reference to an object in the Java virtual machine specification and does not define how the reference should be positioned to access the object in the heap, the object access depends on the virtual machine implementation and decided.

What are the current ways of accessing the mainstream?
use a handle and a direct pointer of two kinds.
① If you use a handle to access , then the Java heap will be divided into a piece of memory as a handle pool , reference is stored in the object's handle address , and the handle contains object instance data specific address information with the type data .

② If you use Direct pointer access , the layout of the Java heap object must consider how to place the information about the access type data , while the reference store is directly the object address .

@ What are the advantages of the two ways of accessing each other?
Handle Access : The stable handle address is stored in the reference, and the instance data pointer in the handle is changed only when the object is moved (a very common behavior when the object is moved by garbage collection ), while the reference itself does not need to be modified.
Direct pointer access : faster , it saves time on pointer positioning, and because objects are accessed very frequently in Java, this kind of overhead adds up to a significant execution cost.

Finally, as far as the main virtual machine Sun HotSpot is concerned, it is accessed using the second way ( Direct pointers ), but in terms of the scope of software development, various languages and frameworks use handles To visit is very common.

To be continued.

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.