Sobes.tech
Senior

How does retained size differ from shallow size?

sobes.tech AI

Answer from AI

Shallow size — this is the size of an object in memory, which directly occupies the object itself, excluding objects it references.

Retained size — this is the total amount of memory that will be freed if this object is deleted, including its own shallow size and all objects accessible only through it (i.e., objects that are not reachable from other parts of the program).

Example:

If object A contains a reference to object B, and B is not used anywhere else, then:

  • Shallow size of A — size of A
  • Retained size of A — size of A + size of B

Thus, retained size shows the actual contribution of the object to memory consumption, considering dependent objects, while shallow size only considers its own size.