Deep understanding of Java Virtual Machines (ii) HotSpot Java object creation, memory layout, and how to access

Source: Internet
Author: User

The creation of objects in memory, the structure of objects, and how to access them.

I. Creation of objects

At the language level. Object creation is just a newkeyword, so what's the process in a virtual machine?

(i) infer whether the class is loaded . Virtual opportunity to a new directive, first checks whether the parameter of this instruction can be positioned in a constant pool for a class's symbolic reference, and checks whether the class represented by the symbol is loaded, parsed, and initialized. Assuming that the process is not complete, you must run the corresponding class load.

(ii) Allocate space on the heap for the object . The amount of space the object needs to be determined after the class is loaded. The object is then allocated a fixed-size space on the heap.

There are two ways of assigning it:

i.  the first hypothesis using Serial parnew Span style= "font-family: Arial" > etc with compact java The heap in memory is regular. It is only necessary to move a distance between the points as the dividing point of the used and unused space.

Ii. another hypothesis when using a mark-sweep algorithm- based collector such as CMS ,Java memory is not structured, The virtual machine maintains a list to record memory usage. Such a way is called the "spare list" way.

Virtual machines allocate space for objects very often, assuming that objects are assigned to multiple threads at the same time. Concurrency security control is involved. Generally there are two ways to solve the problem:

(1) The first is to synchronize the allocated memory space action - Use CAS match the failure retry method to ensure atomicity of the update operation.

(2) The other is to allocate the memory allocation action in different space, and each thread Java a small chunk of memory is pre-allocated in the heap, called the local thread allocation buffer ( Thread Local Allocation Buffer,tlab ).

Which thread will allocate memory. It is allocated on the Tlab of which thread . Only when a Tlab is used and a new Tlab needs to be assigned is it necessary to synchronize the lock.

(iii) initialize the memory space . After the memory allocation is complete, the virtual opportunity initializes the allocated space to a value of 0 (without the object header), assuming that the Tlab allocation is used, and that the process can be advanced to Tlab allocation.

(iv) Sets the object header for the object .

Next, the virtual machines set the object's object header. Contains the object's hash code, class element information,GC generational age, and so on.

This information is placed in the object header.

(v) Operation <init> method . Initializes the member within the object.

After running these five steps, an object is actually generated.

Second, the memory layout of the object

In memory, the object storage layout can be divided into three parts: an object header (header), a Demo sample data (Instance) , and an aligned fill (Padding).

I. Object Header : Contains two pieces of information. The first part is used to store the execution time data of the object itself, such as hash code,GC generational age, lock state, thread hold lock, and so on.

This part of the data is at or above the length , officially known as "Mark Word."

Another part of the object header is the type pointer. A pointer to the class element to which the object is pointing. Use this pointer to determine the instance of the class for this object.

(assuming that a Java object is an array, the object header must also have a piece of data to record the length of the array.)

Because there is no record of the array size in the Java array metadata)

Ii. Instance Data : This part is the place where the object is actually stored for effective information.

Iii. Align fills : This part is not necessary, just the role of placeholder.

Because the HotSpot virtual machine requires that the object start address must be a multiple of 8 bytes.

Third, the object of the interview way

We can manipulate the detailed objects on the heap by using the reference data on the stack. There are two ways to access detailed objects: handles and direct pointers.

handle: A handle pool is partitioned in the Java heap. The instance address and type address specifically used to hold the object. the reference in the stack is simply the address of one of the handles in the handle pool. The advantage is that when garbage collection is made and moved. The object address changes and reference data is not changed.

Direct pointer :reference points directly to an object's address.

The advantage is that speed is fast. Saves time spent on positioning.

Deep understanding of Java Virtual Machines (ii) HotSpot Java object creation, memory layout, and how to access

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.