The retained size of an object doesn't equal the retained size of all the objects referred by it.
Here is what is happening:
A >> B
Retained size of an object is its shallow size plus the shallow sizes of the objects that are accessible, directly or indirectly, only from this object. In other words, the retained size represents the amount of memory that will be freed by the garbage collector when this object is collected.
shallow size: the size of the object itself. retained size: the size of the object itself, plus the size of other objects that are kept alive by this object.
Let me give you an example.
First of all, you need to understand what retained size is. From the official documentation:
Retained size of an object is its shallow size plus the shallow sizes of the objects that are accessible, directly or indirectly, only from this object. In other words, the retained size represents the amount of memory that will be freed by the garbage collector when this object is collected.
In a simple the retained size of an object will indeed be the sum of the objects referred by it. In the picture below the retained size of Obj1 is the sum of the Obj1 shallow size, and Obj2 and Obj3 retained size:
This is not the case in more complicated referencing models. If Obj6 starts referencing Obj5, then Obj5 will not be accessible only from Obj2. So the retained size of Obj2 will now include only Obj4, and will exclude Obj5. The retained size of Obj1 will stay the same. If garbage collector frees Obj1, it'll free the whole references graph of size 41. However, if garbage collector frees only Obj2, it will not free Obj5, because it would be still referenced by Obj6.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With