Shallow Size Vs Retained Size

Eclipse MAT (Memory Analyzer Tool) is a powerful tool to analyze heap dumps. It comes quite handy when you are trying to debug memory related problems. In Eclipse MAT, two types of object sizes are reported:

Sep 02, 2017 Shallow size – size of the object itself including only references to other objects, without objects which are referenced. Retained size – size of the object including references to other objects. Particle size grading. Historically a sequential series of screens was used to sort particle sizes. Each of these screens has a differing number of wires and openings per inch. As material passes through one screen and is retained on another we can sort them into size categories. ANSI B74.12 defines the size distribution of particles. The grain size characteristics of soils that are predominantly coarse grained are evaluated by a sieve analysis. A nest of sieves is prepared by stacking test sieves one above the other with the largest opening at the top followed by sieves of successively smaller openings and a catch pan at the bottom.

  1. Shallow Heap
  2. Retained Heap

In this article, let's study the difference between them and explore how they are calculated

Fig 1: Objects in memory

It’s easier to learn new concepts through example. Let’s say your application has an object model, as shown in Fig #1:

  • Object A is holding a reference to objects B and C.
  • Object B is holding a reference to objects D and E.
  • Object C is holding a reference to objects F and G.

Let’s say each object occupies 10 bytes of memory. Now, with this context, let’s begin our study.

Shallow Heap Size

Remember: the shallow heap of an object is its size in the memory. Since, in our example, each object occupies about 10 bytes, the shallow heap size of each object is 10 bytes. Very simple.

Retained Heap Size of B

From Fig #1, you can notice that object B is holding a reference to objects D and E. So if object B is garbage collected from memory, there will be no more active references to object D and E. It means D and E can also be garbage collected. Retained heap is the amount of memory that will be freed when the particular object is garbage collected. Thus, the retained heap size of B is:

Size

= B’s shallow heap size + D’s shallow heap size + E’s shallow heap size

= 10 bytes + 10 bytes + 10 bytes

= 30 bytes

Thus, the retained heap size of B is 30 bytes.

Retained Heap Size of C

Object C is holding a reference to objects F and G. So, if object C is garbage collected from memory, there will be no more references to object F and G. It means F and G can also be garbage collected. Thuthe s, retained heap size of C is:

= C’s shallow heap size + F’s shallow heap size + G’s shallow heap size

= 10 bytes + 10 bytes + 10 bytes

= 30 bytes

Thus the, retained heap size of C is 30 bytes as well.

Fig 2: Objects Shallow and Retained Heap size

Size

Retained Heap Size of A

Object A is holding a reference to objects B and C, which, in turn, are holding references to objects D, E, F, and G. Thus, if object A is garbage collected from memory, there will be no more reference to object B, C, D, E, F, and G. With this understanding, let’s complete a retained heap size calculation of A.

Thus,the retained heap size of A is:

= A’s shallow heap size + B’s shallow heap size + C’s shallow heap size + D’s shallow heap size + E’s shallow heap size + F’s shallow heap size + G’s shallow heap size

= 10 bytes + 10 bytes + 10 bytes + 10 bytes + 10 bytes + 10 bytes + 10 bytes

Shallow Size Retained Size Java

= 70 bytes

We can then conclude that the retained heap size of A is 70 bytes.

Retained heap Size of D, E, F, and G

Retained heap size of D is 10 bytes, however, this only includes their shallow size. This is because D doesn't hold any active reference to any other objects. Thus, if D gets garbage collected, no other objects will be removed from memory. As per the same explanation objects, E, F, and G’s retained heap sizes are also only 10 bytes.

Let’s Make Our Study More Interesting

Now, let’s make our study a little bit more interesting, so that you will gain a thorough understanding of shallow heap and retained heap size. Let’s have object H starts to hold a reference to B in the example. Note object B is already referenced by object A. Now, two guys A and H are holding references to object B. In this circumstance, lets study what will happen to our retained heap calculation.

Fig 3: New reference to Object B

In this circumstance, retained heap size of object A will go down to 40 bytes. Surprising? Puzzling?

If object A gets garbage collected, then there will be no more reference to objects C, F, and G only. Thus, only objects C, F, and G will be garbage collected. On the other hand, objects B, D, and E will continue to live in memory because H is holding an active reference to B. Thus, B, D, and E will not be removed from memory even when A gets garbage collected.

Thus, the retained heap size of A is:

= A’s shallow heap size + C’s shallow heap size + F’s shallow heap size + G’s shallow heap size

= 10 bytes + 10 bytes + 10 bytes + 10 bytes

= 40 bytes.

The total retained heap size of A will become 40 bytes. All other objects retained heap size will remain undisturbed, because there is no change in their references.

Hope this article helped to clarify Shallow heap size and Retained heap size calculation in Eclipse MAT. You might also consider exploring HeapHero – another powerful heap dump analysis tool, which shows the amount of memory wasted due to inefficient programming practices such as duplication of objects, overallocation and underutilization of data structures, suboptimal data type definitions,….

VS stack retention size _ deep-in-depth shallow size end (Endianness)

tags: VS stack keeps the size

really don't think about it?

4103 words in the full text, is expected to read time 12 minutes

Before entering this topic, I will tell the gang first:

'Endian' The word earliest appeared in Jonathan Swift, in the novel, small people for boiled eggs from big end (Little-End) Stripping and arguing, the two sides of the debate are called 'big end' and 'small end'.

'The book is described in 1726, the two major powers of Lilliput and BLEFUSCU have been suffering in the past 36 months. The war begins because of the following reasons: We all believe that before eating eggs, the original method is to break the eggs. One end, but the grandfather of the emperor today eats eggs, and I happen to break a finger when playing eggs in ancient times. So his father, the emperor at the time, gave it, ordered all the ministers to eat eggs to break the egg. The smaller end, the violation is heavy. The people are extremely anti-sense of this order. History tells us that there have been 6 rebels, one of which emperors sent life, and another lost the throne. These rebels are mostly It is incited by the King of Blefuscu. After the rebellion calm, the people in exile always flee into the empire to seek to avoid refuge. It is estimated that there are 11,000 people in a few times, and they will not be able to break the egg. For this dispute, there have been several hundred works, but the big-ended books have been banned. The law also stipulates that anyone should not be official. '-Wiki

After hundreds of years, how to eat eggs is not important, but there is another dispute in the computer field: memory size. Want to fully understand the size of the size, first understand a concept called byte (endianness), we extracted the definition of endianness on Wiki:

In computing, endianness is the ordering or sequencing of bytes of a word of digital data in computer memory storage or during transmission.

Wikipedia

Due to the computer system, the data stores in bytes, such as the int occupy 4 bytes in the C language, the word sequence is described in the four bytes stored in the memory. After understanding this concept, we will enter the memory of the memory to see what is the size of the size ~

1

What is the size of the size?

In the computer, the byte sequence is primarily divided into large end storage (Little-endian). Similarly, in order to deepen, we extracted the definition of these two concepts in Wiki:

·A big-endian system stores the most significant byte(MSB) of a word at the smallest memory address and the least significant byte(LSB) at the largest.

Shallow Size Vs Retained Size Model

·A little-endian system, in contrast, stores the least significant byte(LSB) at the smallest address.

Wikipedia

For shallow, you can first look at an example, suppose we define a unsigned int type data DATA:

The unsigned int type takes four bytes, then in the memory, the spatial allocation of Big-endian and Little-Endian is:

Shallow Vs Retained Size

It can be seen that the BIG-Endian mode is stored in the low address, and the Little-Endian mode is to store the low storage of the data in a high address, and the rule is so simple. The following figure can help you more image:

Image Source: https: //thebittheories.com/little-endian-vs-big-endian-b4046c63e1f2

  • NOTICE: The size of the size is only valid for the byte of the data type itself, and the storage of the array does not work.

2

Is the size of the CPU or OS?

The size of the size is defined by the data in the underlying memory space, so it is definitely related to the CPU.

For example, if the CPU only supports the small-end mode and the OS is not to store data according to the big end mode (this is certainly crazy), the size of the size will cost a certain resource. The efficiency will be reduced. Therefore, the current OS is compliant with the mode supported by the CPU to store data. However, the size of the size cannot be said that the OS has no relationship. For example, the processor of the ARM kernel can support the size of two modes at the same time. At this time, the OS can select the appropriate storage method according to the operating environment. . The answer to this question is: the size of the size is the platform-related (depending on the CPU + OS operating environment).Size
  • Support Big-Endian CPU: IBM, Freescale Coldfire, AVR32, OpenRISC

  • Support Little-Endian CPU: X86, AMD64 / X86-64, Qualcomm HEXAGON

  • At the same time, BIG-LITTLE-Endian's CPU: PowerPC, MIPS, ARM AARCH64, RISC-V

3

Shallow

Why do I need to specify the size of the size?

Question out: This question is like asks why the operating system is also divided into Windows, Mac OS and Linux, everyone uses Linux not good? (Who doesn't want to have a hundred years of mountain rivers, but the minister is not there, then you can only play with a rule.

Let's take a look at what features of the size of the size:

  • big-endian:

    • It is more likely to achieve comparison and division operations for variables of the same length

    • Handling of network communication packets, more friendly (such as TCP / IP)

    • The judgment of the symbol is more convenient

  • little-endian:

    • Easy to achieve, minus, and multiply

    • Reduce the number of data for data at a specific occasion

    • Handling for low data is more friendly

It seems that there is a thousand autumn, but in fact, it is actually the reason for differentiation: Early computers when communicating network communication, due to the high performance, the data buffer is not large, using large-end mode to make the computer receive When the data packet header can analyze the nature, length and other information of the data faster, and the buffer pool can be left to subsequent data fill in the earlier analysis, so many CPUs are used in large-end models. And the communication protocol adopts large-end model habits to now, causing almost all of the communication protocols that are now larger end mode.

Later, Intel also designed a small-end mode CPU as X86, because the small-end mode adder is easier to implement, and can simplify the design of the hardware circuit in the ALU unit, and reduce costs can enter the majority of consumer groups (after all in that year. A transistor can save a lot of money).

Later, with the birth of Moore's law and the development of integrated circuits, the current computer performance is already early index, so the size of the size is not so important in today's computer, but the manufacturer still according to the previous design. Experience and application scenarios to select size-ended CPUs, such as MCUs, or small-end CPUs to reduce overall costs (saying that the bottom is the inevitable event in the historical process. If you are unified, I am unified, my article is still written? )

4

What is the size of the size?

There are many situations involving the size of the size, and three more typical examples are given here:

Network communication

The current network communication protocol is basically larger end mode. It is well known that the data is transmitted in bytes in the network or air, so the sender is packaged in accordance with the large end rules when the data is packaged. The party must also be subjected to data reorganization in terms of large end rules. For example, in C language uses Union to complete multi-byte data reorganization, you should take into account this problem, and use the bus to transmit data between different CPU platforms to fully consider the byte order of the respective CPUs.

2. Code format

The most typical is the character encoding UTF-16, UTF-32, and both by 2 bytes and 4 bytes respectively encode the problem, which is the problem of solving the word sequence, both of the first few characters. The sequence identity is to indicate the size of the size. (Soft in order to unify the UTF format, put the UTF-8 before adding a BYTE ORDER MARK, causing the encoded file to open it on other platforms)

3.IO operation and register reading and writingThis problem often encountered in Linux's drive writing or MCU development is that the program must match the CPU size terminal mode when reading and writing the CPU peripheral register, including configuring the external chip through the bus. The size of the external chip should be considered when writing.

  • In fact, there is also a place where you need to consider the size of the size, that is, in communication, bit sequence inside one byte. For example, the bit sequence of the I2C and the SPI protocol follows the big end rules, that is, first send high bit bits to send low, and the UART protocol follows the small-end rules, first low-bit bits to send high. This concept involves comparative underlying things, generally do not use small partners who are underground embedded development, and will not let more detail here.

5

How do I know the word sequence of the current platform?

The most direct way is to check the manual of the CPU or the current platform. Here we talk about how to use C / C ++ to determine the word sequence of the platform.

1. Nature judgment using UNION shared memory:

2. Using the pointer judgment:

The results compiled in the Win10 + GCC6.3.3.0 environment are 40, 40 as the lowest byte of the data i, so it can be judged that the platform is a small-end storage mode.

6

How to complete the conversion of the size

I don't pull this piece, directly on the C procedure:

Chrome Inspect Shallow Size Vs Retained Size

[1].https://thebittheories.com/little-endian-vs-big-endian-b4046c63e1f2

[2].https://www.sohu.com/a/334140526_465219

[3].https://en.wikipedia.org/wiki/Endianness

[4].https://www.zhihu.com/question/275738523/answer/382407340

[5].https://www.cnblogs.com/Alandre/p/4878841.html

Intelligent Recommendation

Big endian 1. The size is Big-end storage mode: The lower bits of data are stored in the high address of the memory, and the high bits of the data are stored in the low address of the memory. Little-e...

The size of the end

The so-called big-endian mode (Big-endian), It refers to the high byte of data, stored in the lower memory address in And the lower byte of data, stored in the high address memory, such a bit pattern ...

1, determine the size of the end: Big endian: low byte stored in high address, high byte of the status of the data stored in the low little endian mode: the low byte stored in low address, high byte o...

The size of the end

The size of the end is where they come concept big end, the small end is the concept of 'byte order' inside. It can be divided into big endian byte ordering, the little-endian, the mixing se...

Size

Shallow Size Vs Retained Size Measurement

Due to various problems caused by different computer architectures storage mechanism byte, word, or the like. Endian Storing a sequence of bytes is greater than the types of data in memory. Storage mo...

More Recommendation

The so-called big end: low-address high data storage small end: lower address data stored `` low Why divide the size of the end pattern of it? This is because in the computer system, we are bytes, eac...

On the size of the end

How to treat a type generally do? We should go to consider the following two aspects: 1> the size of open space 2> how to identify data in memory We want to know how the data is stored in memory...

Shallow copy: Copy is the copy, the equivalent of all the contents of an object, a copy to another object, direct copy, or that is the address of an object to another object they point to the same, th...

Shallow vs. deep copy

Heap (heap) and stack (stack) difference: It is part of the heap and stack memory, have different roles, and a program needs to be allocated in this area. Stack memory: a first stack memory area of ​​...