2

Since the garbage collector uses a Mark-Sweep algorithm:

The actual GC is done using a Mark-Sweep algorithm. This is done using a bitvector to track which objects are reachable initially marking root objects as reachable. Then the GC does a sweep over the objects in the heap in address order marking. For each reachable object it sweeps over it marks all objects reachable from that object as reachable. If the new object has a lower address it is added to a work queue. After the initial sweep the work queue is

So it will pass by every object that has a reference to the root and every other will be collected is there any case when putting a reference to null on an Activity is useful?

Putting on onDestroy I assume that has no advantage correct? What about putting them as null on onPause (after validating isFinishing) on "heavy" activities. Will it help the resources to get collected sooner?

neteinstein
  • 17,529
  • 11
  • 93
  • 123
  • 1
    you can check the following link it convey what you are looking for http://stackoverflow.com/questions/850878/does-setting-java-objects-to-null-do-anything-anymore – Punit Sharma Feb 01 '16 at 10:32
  • why not to trim unneeded memory from its process when the system really says its needed? see `Activity#onTrimMemory` – pskink Feb 01 '16 at 10:33
  • @pskink that not exactly the use case. I'm talking about a use-case when you have a heavy memory activity (ies) and when you destroy it for any reason (moving to another, rotate...) you want it to be GC ASAP. – neteinstein Feb 01 '16 at 10:38
  • If you need something to be GCd instantly it may be worth looking at why you need so much memory in the first place. This is pretty much always Bitmap related and if so check this out http://developer.android.com/training/displaying-bitmaps/manage-memory.html – Dori Feb 01 '16 at 10:43
  • @Dori This is not a current problem I have, it's just a theoretical use-case which I find useful to know the best practices.. to avoid having such problems in the first place :-) – neteinstein Feb 01 '16 at 10:54
  • With the application VM limits on todays devices I doubt you will have many issues with memory unless you are using Bitmaps. Most apps with large data requirements either utilise Databases or REST apis so you are not loading a whole data set into memory at once. If you did have to utilise a large amount RAM inside an activity nullifying it in `onPause` would make little difference, apart from maybe it being GCd a few ms early on some occasions. Premature optimisation springs to mind. – Dori Feb 01 '16 at 16:39

2 Answers2

3

So it will pass by every object that has a reference to the root and every other will be collected is there any case when putting a reference to null on an Activity is useful?

Yes. If its static

Putting on onDestroy I assume that has no advantage correct?

I would say this is negligible and therefore pointless as the Activity will be GC-root-less itself momentarily

...What about putting them as null on onPause on "heavy" activities. Will it help the resources to get collected sooner?

If an object does not have a GC root it is eligible for GC. OnPause happens at some point before onDestroy (depending on the execution case this may be any range of time) ergo yes to the above question.

BUT! You would not want to do this as your activity may come back to the foreground after onPause and your would have to allocate all your 'heavy' objects again - which would most likely end up in a crappy experience for the end-user.

Dori
  • 18,283
  • 17
  • 74
  • 116
  • I was talking about doing it on onPause after validating the isFinishing - http://developer.android.com/reference/android/app/Activity.html#isFinishing() - method. So that case wouldn't apply. – neteinstein Feb 01 '16 at 15:10
  • You did not mention that in the question :p – Dori Feb 01 '16 at 16:40
0

By making any object = null we are indirectly telling garbage collection that object is not required to be use ( Making it eligible for garbage collection)

Punit Sharma
  • 2,951
  • 1
  • 20
  • 35