3

I was reading about the finalize() method and was curious:

What is the difference between the task of cleaning up objects ( setting them to NULL ) in finalize, and removing an object from memory?

Dimitri
  • 8,122
  • 19
  • 71
  • 128
Chris Okyen
  • 529
  • 2
  • 6
  • 22

3 Answers3

10

What is the difference between the task of cleaning up objects ( setting them to NULL ) in finialize

setting to null removes ONE reference to the object. if NO more references to an object exists, the garbage collector is allowed (not required) to remove the object

and removing an object from memory?

there is NO explicit way in java to remove (destroy, delete) an object. The garbage collector will do it when he likes. Especially the time from removing the last reference to remove/destroy the object is indefinite

There is NO need to set references to null in finalize method. when the garbage collector call finalize the objects and its references will gone soon anyway.

I never wrote an own finalize method during my very long java experience.

The rare occasion in which it make sense to wrote an own finalize method appear if your object is dealing with os-resources. However, in general you use standard packages for os accesss

stefan bachert
  • 9,413
  • 4
  • 33
  • 40
  • Also, doing _anything_ in a `finalize` method slows down GC tremendously. Effective Java quotes a benchmark indicating a 430x slowdown of garbage collection -- yes, that's over two orders of magnitude. – Louis Wasserman Jun 16 '12 at 22:03
1

You don't "clean up" an object when you set it to null, you're just setting the reference to null, consider:

Object a = new Object();
Object b = a;
a = null;
System.out.println(b);

Once an object loses all references, it will be collected on the next GC pass. Finalize is a method that gets called when this happens, and you should avoid using it.

Just don't keep extra references around and let the GC do it's job.

Pablo Fernandez
  • 103,170
  • 56
  • 192
  • 232
  • How does it loose all the reference. and what is collection by the GC look like in a semi low level explaination? I know I could not have any more reference by not calling b but how does the gc get rid of all references – Chris Okyen Jun 16 '12 at 18:01
  • By setting the variable a to null, it becomes eligible to be free by the garbage collector – Dimitri Jun 16 '12 at 18:39
0

finalize() is called by garbage collector when an object has no more references. You can override it and best practice is to use it in a try-catch-finally block to free non java resources like files. Anyway if you use it this way you should also call super.finalize() to ensure class hierarchy finalization.

This method is always for advanced use and shouldn't be used in normal production code. Free your resources in finally clauses in methods using those resources.

Vincenzo Maggio
  • 3,787
  • 26
  • 42
  • 4
    best practice is to never deal with `finalize` – Pablo Fernandez Jun 16 '12 at 17:50
  • also, this doesn't answer the question – Pablo Fernandez Jun 16 '12 at 17:52
  • If I am just learning java, should I not worry about the great details of how in memory, the object is removed? I found this tutorial http://javarevisited.blogspot.com/2011/04/garbage-collection-in-java.html. Is knowledge of the garbage collector's inner workings neccessary for writing Android Apps? – Chris Okyen Jun 16 '12 at 17:53
  • I find this hard to understand - are you saying best practice is for us as developers to call the finalize() method? Or are you saying that the implementation of finalize should be used if the object holds onto files until GC:ed (and that that is best practise)? – esej Jun 16 '12 at 17:54
  • Nope, you should never mess directly with finalize() because it's called by garbage collection and you should free resources in normal code or in finally clauses. Nevertheless, Java exposes this functionality for advanced use. I must say that my collegues that develop in Java always say "GC is always smarter than you"! – Vincenzo Maggio Jun 16 '12 at 17:56
  • BTW how could he set pointer to NULL in finalize()? this = null; ?! – Vincenzo Maggio Jun 16 '12 at 17:57
  • Your are answer is confused, I'm not totally sure you even know what you are saying. (It is for example technically wrong, it is not called by GC when an object has no more references, it is called by GC when it is getting GCed - which can be years after is has no more references - remember GC being smarter than you.) – esej Jun 16 '12 at 18:00
  • I don't understand the downvote: from the point of view of the programmer having no more references assure that at some point of time GC will collect it. You are confused and you downvote? I don't think that's the correct approach. – Vincenzo Maggio Jun 16 '12 at 18:04
  • BTW "the method is called by the garbage collector when it determines no more references to the object exist" http://www.janeg.ca/scjp/gc/finalize.html – Vincenzo Maggio Jun 16 '12 at 18:06
  • Sorry, what do you want me to do? What you have written is both plain wrong and confusing. Your explaination in comments makes it a littel better - but I still feel this could be dangerous to new/starting java coders. Ill obv. change my vote if the answer gets updated and my problems go away. Edit: I'm pretty sure your source is wrong as well - but I could possibly be convinced otherwise, and would have to change my understanding of GCs. – esej Jun 16 '12 at 18:08
  • About the plain wrong, just tell me the part that is wrong with proof, not opinion. BTW, updated to reflect the fact that users shouldn't use it in code. – Vincenzo Maggio Jun 16 '12 at 18:11
  • In fact @VincenzoMaggio is not totally wrong. The method finalize is called just before the object is deleted by the GC, it's not when the object has no more references. Some objects may have no more references but still exist on the heap. You cannot make supposition about when the JVM will call the GC. It's totally discouraged to use the finalize method because you can write a code that reassings a reference to an object that was previously nulled, effectively uneligiblizing the object for garbage collection. – Dimitri Jun 16 '12 at 18:51
  • @Dimitri I know it and I updated the answer to reflect the fact that it shouldn't be used in normal production code. BTW, I repeat, finalize is "Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. A subclass overrides the finalize method to dispose of system resources or to perform other cleanup.". Source: the actual owners of Java http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Object.html#finalize() – Vincenzo Maggio Jun 16 '12 at 20:25