Home  >  Q&A  >  body text

android - java中使用MAT是shallow size 单位是什么

java中shallow size 单位是什么

伊谢尔伦伊谢尔伦2744 days ago853

reply all(1)I'll reply

  • 天蓬老师

    天蓬老师2017-04-18 10:51:59

    If you want to explain shallow size clearly, you must mention Retained Size.
    shallow size: Indicates the size occupied by its own object.
    retained size: the retained size of the own object + the reference object.

    Let’s borrow a picture first. This picture can more clearly represent the calculation relationship between them.

    B’s shallow size = B;

    B’s retained size = B shallow size + C retained size + D retained size;

    Before giving an example, we must first understand the storage of JAVA objects in the heap. Let’s take the 32-bit JVM virtual machine as an example:
    The JAVA object consists of 3 parts in the heap:

    1. Object header
      The object header contains two parts of data;
      One: runtime data. The 32-bit JVM is 32-bit, which is 4byte. The 64-bit is 8byte.
      2: Type pointer.

    2. Instance data
      is the data that stores instance variables. Variable types include two types: basic type variables and reference variables.
      Let’s not talk about the size of bytes occupied by basic type variables. For reference variables, we store pointers.

    3. Filling data
      The object storage space is an integer multiple of 8byte. If the object header + instance data is less than an integer multiple of 8byte, it will be filled.

    Speaking of reference type size, a reference occupies 4 bytes in a 32-bit virtual machine. If pointer compression is not enabled in a 64-bit virtual machine, a reference occupies 8 bytes.

    Let’s explain below based on examples:

    public class TestObjSize {
        private int a = 1;
        private boolean b = true;
        private TestObjSize testObjSize;
        
        public static void main(String[] args) {
            Object object = new Object();
            TestObjSize test = new TestObjSize();//这个new出来的对象记为obj1
            test.testObjSize = new TestObjSize();//这个new出来的对象记为obj2
            
            System.out.println(object.hashCode());
            System.out.println(test.hashCode());
            
            try {
                Thread.sleep(3000000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    Because the shallow size has nothing to do with whether the instance variable has a reference, we first analyze the shallow size of **TestObjSize,
    shallow size = 4byte (object header) + 4byte (type pointer) + 4byte (int a) + 1byte (boolean b ) + 4byte (TestObjSize reference) = 17;**
    17 is not an integer multiple of 8, so there will be 7 bytes of padding data. The final shallow size of the instance object of TestObjSize type = 24;

    According to the example, we see that the testObjSize of the obj2 object = null,
    obj2’s retained size = obj2’s shallow size = 24;
    The testObjSize of the obj1 object is obj2, so obj1’s retained size = obj1’s shallow size + obj2’s retained size = 48;

    Attach a heapdump at the end!

    If there are any errors, please tell me.

    reply
    0
  • Cancelreply