The difference between a shallow heap and a retained heap
1 Shallow size
Is the size of the memory occupied by the object itself, and does not contain the object it references. The shadow size for a regular object (not an array) is determined by the number and type of its member variables, while the shallow size of the array is the sum of the size of the array elements.
2 retained size
Retained size= the current object size + the sum of the size of the object to which the current object can be directly or indirectly referenced. (Meaning of indirect reference: A->b->c,c is an indirect reference) and excludes objects that are referenced directly or indirectly by GC roots
In other words, retained size is the amount of memory that the current object can be freed from the heap after it is GC.
However, when you release it, you also exclude objects that are referenced directly or indirectly by GC roots. They will not be treated as garbage for the time being.
GC roots directly references A and B two objects.
The shallow Size of the retained Size=a object of the A object
The retained of the B object is the shallow size of the shallow size + C object of the Size=b object.
Chestnuts??
The retained size of the obj1 is Obj1, obj2, obj4 the sum of shallow size, and does not include OBJ3 's shallow size, because OBJ3 is referenced by GC roots, which means obj3 is referenced in addition to OBJ2, There are other external references as well.
The retained size in obj1 is the sum of the obj3 size for obj1, Obj2, Obj4, Shallow, because OBJ3 has no other external references other than the OBJ2 reference.
Excerpt from: http://blog.csdn.net/a740169405/article/details/53610689
Java heap problem analysis (not finished)