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 understanding of deep pile and shallow pile in JVM?

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

Share

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

JVM deep pile and shallow pile of understanding is what, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

I. concept

Let's give the concept first, and then analyze it.

(1) shallow heap represents the amount of memory occupied by a java object structure, and the java object contains three parts of data: object header, instance data, and alignment padding.

(2) Deep heap represents the amount of memory that can be actually freed after a java object is reclaimed by GC.

If you're a little confused, it's okay. Let's explain the nouns above one by one.

1. Object head

An object is divided into three parts: object header, instance data, and alignment padding. The object header contains two parts: the tag section and the original object reference.

(1) Mark part: including hashcode, gc generation age, lock status flag, thread holding lock, biased thread lock id, bias timestamp, this part is 8byte on 4 byte,64-bit machines on 32-bit machines, generally speaking, our machines are 64-bit, so now the default refers to 8 bytes.

(2) the original object reference is the pointer of the object. Through this pointer to find the instance of the object, the data can be compressed. It is 4byte on the 32-bit machine and enabled by default on the 64-bit machine. The compression size is 4byte.

So we can see that the header size of an java object is 16 bytes when not compressed and 12 bytes when it is compressed.

2. Instance data

Refers to our instance of the java object. This is easy to understand.

3. Align the fill

Alignment filling to trace its root is actually to the discipline of computer system architecture. I learned alignment padding when I was in college, and it's the same thing here. Objects in java are aligned in 8-byte units, so each java object is an integral multiple of 8.

4. Reserved set

Object A can directly or indirectly access other objects, which together are the reserved set of A. When garbage collection occurs, not only An is recycled, but all objects in the retention set are also recycled.

Now that we understand these basic concepts, let's go back and take a good look at what are deep piles and shallow piles.

Shallow heap refers to the memory occupied by the object itself, excluding the size of its internal reference object. The deep heap of an object refers to the sum of (directly or indirectly) the shallow heap of all objects that can only be accessed through the object, that is, the real space that can be released after the object is reclaimed. In the "practical java virtual machine", a case is used, which is quoted here:

A refers to the object CPower D, and object B refers to the object C < E >.

Object A references C and D, and object B references C and E. Then the shallow heap size of object An is only An itself, excluding C and D, and the actual size of An is the sum of A, C and D.

The deep heap size of An is the sum of An and D, and because object C is also accessible through object B, it is not within the deep heap range of object A.

Let's give another example: from 1 to 100, the number divisible by 3 can be sent to object A, and the number divisible by 5 can be sent to object B. Then, when the program exits, garbage collection is carried out. At this time, the deep heap of object A must be less than 33, and the deep heap of object B must be less than 20. This is because there are numbers like 15 and 30, which can be divisible by either 3 or 5. Belong to three regardless of the object.

The code is no longer demonstrated, and the final result is analyzed using the MAT tool.

After reading the above, have you mastered the understanding of deep heap and shallow heap in JVM? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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