algorithmsMadness
algorithmsMadness

Reputation: 71

Garbage collector and nulling out references in onDestroy

Does nulling out references (for example to a bitmap) in onDestroy/onStop make any difference in the speed of GC clean up?

Upvotes: 0

Views: 259

Answers (1)

Stephen C
Stephen C

Reputation: 718986

In a word. No.

The GC will not go faster if you null out fields in objects that are already going to be unreachable.

For example, suppose that you have a large data structure with lots of interior references and only one reference held outside of the data structure. When the outside reference disappears, then the entire data structure becomes unreachable. Nulling the internal references will achieve nothing.

The only possible benefits of nulling are:

  • Under certain circumstances - nulling a field can make the corresponding referenced object unreachable sooner. Using the example above, this might happen if there were multiple outside references, and some of them were "hidden" in long lived data structures.

  • The GC will typically run faster if there are fewer reachable objects. So steps that make objects unreachable sooner will reduce GC overheads.


Does nulling out references (for example to a bitmap) ... Android bitmaps are / contain a non-heap resource that needs to be handled carefully. This Q&A explains:

Note that the solution isn't to simply "null" the bitmap reference. That will typically have no effect.

Upvotes: 1

Related Questions