[Java] [MAT] Explanation of shallow heap size calculation

Source: Internet
Author: User

When viewing the mat document, this describes the shallow heap: shallowheap is the memory consumed by one object. An object needs + or (depending on the OS architecture) per reference, 4 bytes per Integer, 8 bytesper Long, etc. Depending on the heap dump format the size is adjusted (e.g.aligned to 8, etc ...) to model better the real consumption of the VM.

According to this, on Android, the shallow heap cost per object should be 8 bytes plus the memory overhead of each internal member variable of that object.

   Class Demo0 {}    class Demo1 {       intint_num = 0;   }

With the above two classes as an example, the Demo0 object shallowheap size should be 8bytes, then the shallow Heap size of Demo1 should be 8 + 4 = 12bytes. But the MAT results show a value of bytes, such as:

Figure 1. Demo1 Shallow Heap value display

To resolve these questions, continue to add member variables inside the class to observe the display size of the shallowheap.

   Class Demo12 {       int int_num= 0;       int int_num1= 0;   }    Class Demo13 {       int int_num= 0;       int int_num1= 0;       int int_num2= 0;   }

Of the above two classes of objects, the shallow Heap corresponds to a size of 2, respectively, as shown in

Demo12:16 bytes;

demo13:24 bytes;


Figure 2.

At this point, it is not a guess that the JVM allocates memory with 8 bytes as its granularity .

Google article: Java object memory structure, English version javaobjects memory Structure.

The conjecture is verified by this.

Since the object is allocated at 8 bytes for granularity, Demo1 only uses a 4bytes int, still free of additional 4bytes, this free 4bytes can then declare the 2nd INT member variable to Demo12. If you want to declare the 3rd INT member variable again, you need to allocate another 8bytes of space, that becomes the Demo13 of 24bytes.

The problem is that thespace allocated by the VM to an object is not necessarily the size and magnitude of the memory that the member variables in the current object occupy, and it is most likely a large one.

The number of bytes required to attach various data types is shown in the table below.

Type

bytes

Long

8

Int

4

Short

2

Char

2

Boolean

1

Byte

1

Ref (Reference)

4

[Java] [MAT] Explanation of shallow heap size calculation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.