Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nulling variables of Fragment on onDestroy() [duplicate]

Should I explicitely set all variables of fragment to null in onDestroy (or onDetach) method to avoid memory leaks? Or it is not necessary, and they will be GCed when fragment is "destroyed". What are the best practices? Thanks in advance!

like image 368
user2758776 Avatar asked Sep 19 '13 20:09

user2758776


2 Answers

Just add-on,

  1. If you have any service running in your code, stop it.
  2. If you have registered to any broadcast or any other listeners, unregister them
  3. If you have any cursors open, close them.
  4. If you have any bitmaps, recycle them.
like image 151
prijupaul Avatar answered Nov 15 '22 17:11

prijupaul


You don't need to worry about this. The GC will take care of it. In Java you pretty much have to TRY to have a memory leak (yes, it's possible, but very rare). So, don't worry about it. If you do set the variables to null you would just end up wasting time on code you don't need, and would add to maintenance down the road. The GC in Java does a great job of destroy variables and objects that aren't referenced.

like image 24
BlackHatSamurai Avatar answered Nov 15 '22 19:11

BlackHatSamurai