In-depth understanding of Java Virtual Machine (2) Creation of Hot Spot Java objects, memory layout and access methods

Source: Internet
Author: User

In-depth understanding of Java Virtual Machine (2) Creation of Hot Spot Java objects, memory layout and access methods

In-Memory Object creation, object structure, and access method.

I. Object Creation

At the language level, object creation is just a new Keyword. What is the process in a virtual machine?

(1) determine whether a class is loaded. When the VM encounters a new command, it first checks whether the parameter of this command can locate a Class symbol reference in the constant pool, check whether the class represented by this symbol is loaded, parsed, and initialized. If the process is not completed, the corresponding class must be loaded.

(2) Allocate space for objects on the heap. The size of the space required by the object can be determined after the class is loaded. A fixed size of space is allocated to the object on the heap. There are two allocation methods:

I. first, if the collector with Compact processes such as Serial and ParNew is used, the Java memory heap is regular, you only need to move the pointer as a distance between the used and unused space.

Ii. second, if the Java memory is not regular when using the CMS-based Mark-Sweep algorithm-based collector, the virtual machine must maintain a list to record the memory usage, this method is called "Idle list.

Virtual machines frequently allocate space to objects. If multiple threads allocate objects at the same time, concurrency security control is involved. There are generally two solutions:

(1) The first method is to synchronize the actions in the allocated memory space-use CAS with retry failures to ensure the atomicity of the update operation.

(2) The second approach is to allocate the memory allocation actions in different spaces. Each thread allocates a small block of memory in advance in the Java heap, it is called the Local Thread Allocation Buffer (TLAB ). The TLAB of the thread where the memory is to be allocated is allocated. Synchronization locks are required only when TLAB is used and a new TLAB needs to be assigned.

(3) initializing memory space. After the memory allocation is complete, the virtual opportunity will initialize the zero value (excluding the object header) in the allocated space. If you use TLAB for allocation, this process can also be done in advance when TLAB is allocated.

(4) set object Headers. Next, the virtual machine needs to set the object header. Including the object's hash code, class Element Information, GC generational age, and so on. The information is stored in the object header.

(5) Execution MethodInitialize the members in the object.

After these five steps are completed, an object is actually generated.

Ii. Object Memory Layout

In memory, the OSS layout can be divided into three parts:Object Header(Header ),Sample Data(Instance Data) andAlignment Filling(Padding ).

I.Object Header: Contains two parts. The first part is used to store the data in the running hours of the object, such as the hash code, GC generational age, lock status, thread holding lock, and so on. The length of this part of data is 32 or 64-bit, officially referred to as "Mark Word ". Another part of the object header is the type pointer, that is, the pointer to the class element of the object. This pointer is used to determine the instance of the class when the object is located. (If a Java object is an array, the object header must also have a piece of data used to record the length of the array. Because the Java array metadata does not have an array size record)

Ii.Instance data: This part is actually used to store valid object information.

Iii.Alignment Filling: This part is not mandatory, but serves as a placeholder. Because the HotSpot VM requires that the starting address of an object must be a multiple of 8 bytes.

Iii. Object Access methods

We can use reference data on the stack to operate specific objects on the stack. There are two ways to access a specific object: a handle and a direct pointer.

Handle: A handle pool is defined in the Java heap to store the instance address and type address of the object. The reference in the stack is only the address of a handle in the handle pool. The advantage is that after garbage collection and moving, the object address changes and the reference data does not need to be changed.

Direct pointer: The reference directly points to the address of an object. The advantage is that the speed is fast, saving a time overhead for positioning.

Related Article

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.