[Android Memory] Explanation of shallow heap size calculation

Source: Internet
Author: User

Reprinted from: http://blog.csdn.net/sodino/article/details/24186907

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

Translation: Shallow heap is the memory consumption of an object, depending on the OS system architecture, an object may require 4 bytes or 8 bytes. The integer type consumes 4 bytes, and a long consumes 8 bytes. Because of the heap dump format, the allocation size will be larger than the actual VM consumption.

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 {       int int_num = 0;   }

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

                                                                 Figure 1. Demo1 Shallow heap value display   To resolve the above question, continue to add member variables inside the class to observe the display size of the shallow heap.
   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, the English version of Java Objects memory Structure.

The conjecture is verified by this.

Since the object is allocated with 8 bytes for granularity, Demo1 only uses an int of 4 bytes, which is still free of 4 bytes, and this spare 4 bytes 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 8 bytes 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

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.