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 memory layout and access location of Java objects?

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Today, I will talk to you about the memory layout and access location of Java objects, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.

In the HotSpot virtual machine, the layout of objects in memory is divided into three areas: object header (Header), instance data (InstanceData), and alignment padding (Padding).

Memory layout of the object

The object header includes two parts of information. The first part is used to store the runtime data of the object itself, such as hash code, GC generation age, lock status flag, lock held by thread, biased thread ID, biased timestamp and so on. Officially, this part of data is called MarkWord. The other part is the type pointer, which is the pointer of the object to its class metadata, which is used by the virtual machine to determine which class the object is. However, not all virtual machine implementations retain type pointers on object data, which means that metadata information for finding objects does not have to pass through the object itself, which will be discussed in the following object positioning methods. In addition, if the object is a Java array, the length of the array is recorded in the object header to determine the size of the Java object.

HotSpot virtual machine object header MarkWord

The real valid data for the object, that is, the field content defined in the code. This part of the stored data is affected by the virtual machine allocation policy parameters and the order in which fields are defined in the Java source code. The default allocation policy for HotSpot virtual machines is longs/doubles, ints, shorts/chars, bytes/booleans, oops. Fields of the same width are always assigned together. Under this premise, the variables defined in the parent class appear before the child class. If the CompactFields parameter is true, narrower variables in the subclass may also be inserted into the gaps of the parent variable.

Alignment padding is not inevitable and has no special meaning, it is just a placeholder. Because the automatic memory management system of HotSpotVM requires that the starting address of the object must be an integral multiple of 8 bytes, that is, the object size must be an integral multiple of 8 bytes. So when the object instance data is not aligned, it will be filled by alignment padding.

Access location of object

The object is created to use the object, and the Java program needs to manipulate the object on the Java stack through the reference data on the Java stack. The reference type is a reference to an object. The way to access the object depends on the implementation of the virtual machine. At present, there are two main access methods: handle and direct pointer.

If you use handle access, a piece of memory will be divided in the Java heap as a handle pool. What is stored in the reference is the handle address of the object, and the handle contains the address of the instance data and type data of the object.

If you use direct pointer access, the address of the object is stored directly in reference, but pointers to access type data are also placed in the object.

These two object access methods have their own advantages and disadvantages. If handle access is used, a stable handle address is stored in reference. Moving objects during garbage collection will only modify the instance data pointer in the handle, but reference itself does not need to modify it.

The biggest advantage of using direct pointer access is faster, saving a pointer positioning time, object access is very frequent, this kind of savings is also considerable.

The HotSpot virtual machine uses direct pointer access.

After reading the above, do you have any further understanding of the memory layout and access positioning of Java objects? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

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: 0

*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

Development

Wechat

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

12
Report