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 structure of the java object object in heap

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

Share

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

What is the structure of java object objects in heap, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can get something.

Brief introduction

In the previous article, we introduced the use of the JOL artifact to parse the memory addresses occupied by java classes or java instances.

Today, we will take a step further and analyze the deeper details that have not been covered in the previous article. Let's take a look.

Objects and their hidden secrets

Java.lang.Object should be familiar to everyone. Object is the ancestor of all objects in java.

Next, we will conduct a detailed anatomical analysis of the ancestor of this java object in order to understand the deep secrets of JVM.

The tool, of course, uses JOL:

@ Slf4jpublic class JolUsage {

@ Test public void useJol () {log.info ("{}", VM.current (). Details ()); log.info ("{}", ClassLayout.parseClass (Object.class). ToPrintable ()); log.info ("{}", ClassLayout.parseInstance (new Object ()). ToPrintable ());}}

The code is simple. We print the information for JVM, Object class, and a new Object instance.

Look at the output:

From the above results, we know that in 64-bit JVM, an Object instance takes up 16 bytes.

Because there are no references to other objects in the Object object, we see that the Object object only has a 12-byte object header. The remaining 4 bytes are padding bits.

Object object header

So what are these 12-byte object headers for?

If you want to get an in-depth understanding of these 12-byte object headers, of course, you need to take a look at JVM's source code: src/share/vm/oops/markOop.hpp.

Those who are interested can go and have a look. If you are not interested, it doesn't matter. Here is a summary picture for you:

The object header size of the javaObject object varies slightly depending on whether you are using a 32-bit or 64-bit virtual machine. Here we use a 64-bit virtual machine as an example.

The object header of Object is divided into two parts. The first part is Mark Word, which is used to store the runtime data of objects, such as hashcode,GC generation age, lock status, holding lock information, lock-biased thread ID and so on.

In a 64-bit virtual machine, Mark Word is 64bits, and in a 32-bit virtual machine, Mark Word is 32bits.

The second part is that Klass Word,Klass Word is a type pointer to the metadata of class, and JVM uses Klass Word to determine which class instance the object is.

Wait a minute!

Some friends may have found a problem. When we parsed Object objects with JOL, the Object head size was 12 bytes, that is, 96bits. How do you write 128bits here?

Yes, if COOPs is not enabled, it is 128bits. If COOPs is enabled, the size of Klass Word is reduced from 64bits to 32bits.

Remember the COOPs we talked about before?

COOPs is the technology of compressing object pointers.

An object pointer is used to point to an object that represents a reference to that object. Generally speaking, on a 64-bit machine, a pointer occupies 64 bits, or 8 bytes. On a 32-bit machine, a pointer occupies 32 bits, or 4 bytes.

In real time, in applications, there are so many pointers to such objects that if the same program is run on a 32-bit machine, it takes up completely different memory to run on a 64-bit machine. 64-bit computers may use 1.5 times more memory than 32-bit computers.

To compress the object pointer means to compress the 64-bit pointer to 32-bit.

How to compress it? The object address of the 64-bit machine is still 64-bit. The compressed 32-bit memory is only relative to the displacement of heap base address.

We use a 64-bit heap base address + 32-bit address displacement to get the actual 64-bit heap address.

Object pointer compression is enabled by default in Java SE 6u23. Before that, you can use-XX:+UseCompressedOops to turn it on.

Array object header

There is a very special object in java called an array. What's the difference between the object header of an array and Object?

Let's look at it again with JOL:

Log.info ("{}", ClassLayout.parseClass (byte [] .class) .toPrintable ()); log.info ("{}", ClassLayout.parseInstance ("www.flydean.com" .getBytes ()) .toPrintable ())

In the above example, we parsed the examples of the class and byte arrays of the byte array, respectively:

See the difference? We find that the object header of the array is 16 bytes, which is 4 bytes more than the object header of normal objects. These four bytes are the length of the array.

The structure of the entire object

All right, let's sum up here that the structure of java objects can be divided into normal java objects and array objects:

The array object has an extra 4-byte length field in the object header.

You can see that the last byte is the padding padding byte, why populate it?

Because JVM is done in 8-byte units, it needs to be completed if it is not an integer multiple of 8 bytes.

More wonderful content

one

Look at the algorithm of animation: sorting-merging sorting

two

Computer Secrets: network Classification and performance Analysis

three

JVM explains it in detail: running constant pool

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, 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

Internet Technology

Wechat

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

12
Report