1
public void onDestroy() {
}
protected void finalize() {
}

Does finalize not always get called for fragments?

What about the fields that are inside of the fragment instance? Must I set them to null in onDestroy so that they are reclaimed?

Paul Nikonowicz
  • 3,883
  • 21
  • 39
  • For the most part, don't worry about `finalize()` with Android and in particular the main Android components (Activity, Fragment, Service etc etc). The OS is designed to be very efficient when it comes to cleaning up resources and garbage collection and such. This stuff is all done behind the scenes when the OS determines a particular object is no longer needed. – Squonk Nov 21 '13 at 21:48
  • I'm trying to diagnose a memory spike in my app though. So...I need to know when objects are reclaimed. – Paul Nikonowicz Nov 21 '13 at 22:21
  • I would have the best way to do that would be to use the various Android SDK tools. – Squonk Nov 21 '13 at 22:26

1 Answers1

1

My understanding is that the finalize method is called by the garbage collector and cannot be relied upon to be called at a given time.

These posts may give you more insight:

I tend to do my cleanup in onPause or onDestroy.

Community
  • 1
  • 1
ssawchenko
  • 1,198
  • 1
  • 13
  • 24
  • 1
    Depending on what you mean by "cleanup" I'm not sure I'd recommend doing much in onPause bearing in mind the onPause -> onResume -> onPause cycle is the tightest of an Activity or Fragment life-cycle. – Squonk Nov 21 '13 at 21:53