Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the concept of object allocation, layout, and access in the JVM heap

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the relevant knowledge of "what is the concept of object allocation, layout and access in the JVM heap". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Creation of object

Java is an object-oriented language, and objects are created all the time in the process of running Java programs. From a language point of view, creating an object is just a new keyword, but what is the process of creating an object (limited to ordinary Java objects, excluding arrays, Class objects, etc.) in a virtual machine?

Take the Hotspot virtual machine as an example. When the virtual machine encounters a bytecode instruction, it will first check whether the parameter of the instruction can locate a symbol reference in the constant pool, and check whether the class represented by the symbol reference has been loaded, parsed and initialized. If not, the corresponding class loading process will be performed first.

After the class load check passes, the virtual machine allocates memory for the new object. The amount of memory required for an object can be completely determined during class loading, and the task of allocating space to an object is actually tantamount to dividing a determined area of memory from the Java heap and allocating it to the object. There are two main ways to divide memory:

Pointer collision method

That is, assuming that the memory in the Java heap is absolutely regular, the used memory is stored on one side, and the free memory is on the other side, with a pointer in the middle as an indicator of the demarcation point, then when you need to allocate memory, you only need to move the pointer to the direction of the free space by a distance equal to the size of the object.

Free list method

This situation is that the memory in the Java heap is not regular, and the used memory and free memory are interlaced with each other. At this point, the virtual machine must maintain a list, record which memory blocks are available, find a large enough space from the list to allocate to the object instance, and update the list record.

Thus it can be seen that the choice of allocation mode is determined by whether the Java heap is regular or not, and whether the Java heap is regular or not is determined by whether the garbage collector used has the ability to compress and organize the space.

In addition to how to divide the available space, there is another problem to consider: object creation is a very frequent behavior in the virtual machine, even if it is only to change the position of the pointer, it is not safe in the case of concurrency. It is possible that memory is being allocated to object A, the pointer has not yet been modified, and object B uses the original pointer to allocate memory at the same time. There are two ways to solve this problem: one is to synchronize the action pieces that allocate memory space. In fact, the virtual machine uses CAS with a failed retry mechanism to ensure the atomicity of the update operation. The other is to divide the memory allocation into different spaces according to threads, that is, each thread pre-allocates a small piece of memory in the Java heap, which is called Local Thread allocation buffer (Thread Local Allocation Buffer), referred to as TLAB. Which thread allocates memory is allocated in the local buffer of which thread. Only when the local buffer is used up, does it need synchronous locking when allocating new buffers. Whether the virtual machine uses TLAB can be set with the-XX:+/UseTLAB parameter.

After the memory allocation is complete, the virtual machine must first initialize the allocated memory space (but not the object header) to zero, which can also be done in advance during TLAB allocation if TLAB is used. This operation ensures that the instance fields of the object can be used directly without initial values in the Java code, so that the program can access the zero values corresponding to the data types of these fields.

Next, the Java virtual machine also needs to set the object header (Object Header) of the object, which stores some information, such as which object is the instance of the object, how to find the metadata information of the class, the hash code of the object, the GC generation age of the object, and so on.

After the above work is finished, from the perspective of the virtual machine, a new object has been born, while from the point of view of the Java program, the creation of the object has just begun, the constructor of the object, that is, the method of the Class file, has not yet been executed, and all fields are zero by default. After the new instruction, the method is then executed to initialize the object, so that a truly available object is fully constructed.

2. Memory layout of objects

In a HotSpot virtual machine, the storage layout of objects in heap memory can be divided into three parts: object headers (Header), instance data (Instance Data), and alignment padding (Padding).

1. Object head

The object header of the Hotspot virtual machine object includes two types of information. The first type is used to store the runtime data of the object itself, such as hash code, GC generation age, lock status flag, thread holding lock, bias thread ID, orientation timestamp and so on. The length of this part of data is 32 bits in 32-bit virtual machines and 64 bits in 64-bit virtual machines, which is officially called "Mark Word".

But in fact, objects need to store a lot of data to be run, which has exceeded the maximum that can be recorded by 32-bit and 64-bit Bitmap structures, so Mark Word is designed to have a dynamically defined data structure in order to reuse its own storage space as much as possible, such as in 32-bit JVM:

The other part of the object header is the type pointer, the pointer of the object to its type metadata, which is used by the Java virtual machine to determine which class the object belongs to. However, not all virtual machines retain type pointers on the object header, in other words, finding the object's metadata information does not have to pass through the object itself. In addition, if the object is an array, there must be a piece of data in the object header to record the length of the array

two。 Instance data

The instance data part is the valid information really stored by the object, that is, the various types of field content that we define in the code, whether inherited from the parent class or defined in the subclass, must be recorded.

3. Align fill

Aligned padding is not inevitable, it only acts as a placeholder, because the automatic memory management mechanism of the Hotspot virtual machine requires that the size of the object must be an integer multiple of 8 bytes. Therefore, if the object instance data part is not aligned, it needs to be completed by alignment padding.

3. Access location of objects

The Java program manipulates specific objects on the heap through the reference data on the stack. In the Java virtual machine specification, it simply states that the reference type is a reference to an object, not how it is implemented. Therefore, the object access method is independently implemented by the virtual machine, and the main access methods are handle and direct pointer:

Handle access

A piece of memory is divided in the Java heap to serve as a handle pool. What is stored in the reference is the handle address of the object, and the handle contains the specific address information of the object instance data and the type data.

Direct pointer

What is stored in reference is the address of the object, so if you only access the object itself, you can avoid multiple overhead. In addition, if direct pointer access is used, the memory layout of objects in the Java heap must consider how to place relevant information for accessing type data

The advantage of using handles is that stable handle addresses are stored in reference, and if the object is moved (such as garbage collection), only the instance data pointer in the handle will be changed, but the reference itself does not need to be modified. The advantage of using a direct pointer is that it is faster and saves time. Direct pointer is mainly used for object access in HotSpot virtual machine.

That's all for "what are the concepts of object allocation, layout, and access in the JVM heap?" Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 298

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report