5

As a java beginner I wanted to ask about if I should set an object reference to null, or call the finalize method in a large project?

For example,

while( obj... some code here){
 //code stuff
}
obj = null; // or obj.finalize();

Since im done with the obj and there are no more references to it what should i do with it? set it to null or call finalize();?

I read the java doc but this paragraph The finalize method is never invoked more than once by a Java virtual machine for any given object. confused me. Does that mean that even if i use it, it won't do anything at all unless GC decides to do so? Then, will my decision to set the obj to null help any?

This all assuming its a large project and the scope has yet to end in order for GC to deallocate it itself. Thanks.

Voqus
  • 159
  • 1
  • 11
  • This may help in understanding the `finalize`-method and GC: http://stackoverflow.com/questions/2506488/when-is-the-finalize-method-called-in-java – wassgren Jan 07 '15 at 17:10
  • Thanks for replying wassgren, i already read the java docs as stated. But there are solutions given like [this](http://howtodoinjava.com/2012/10/31/why-not-to-use-finalize-method-in-java/) that forced the finallize method and actually worked, and i was wondering what should you do on a large project. – Voqus Jan 07 '15 at 17:14
  • Never use **finalize** method. It is totally unpredictable. – IgorGanapolsky Oct 24 '16 at 15:55

2 Answers2

7

Neither. If obj is no longer used further in the code (and nothing else references the same object as obj does), the object will no longer be reachable and will become a candidate for Garbage Collection.

Setting obj to null will not achieve anything (YMMV depending on implementation of garbage collectors).

As for calling finalize(), don't. Let the Garbage Collector take care of that. But note that finalize is a method like any other. What the text you quoted is saying is that the GC will not call it more than once, regardless of the amount of times your code may have invoked it.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • Yes but will the obj be eligible for the GC to pick during the runtime or will it wait for the scope of the method the obj was called it to end and decide? Also as i wrote in another comment, is it worth forcing the finallize method or not? As example stated [here](http://howtodoinjava.com/2012/10/31/why-not-to-use-finalize-method-in-java/) – Voqus Jan 07 '15 at 17:18
  • 1
    @VasilisCh As a beginner, you shouldn't worry about it. Let the automatic GC take care of it. It will detect when the object is no longer in use and delete it. Just stop referencing the object when you're done using it and everything will take care of itself. – Nic Jan 07 '15 at 17:26
  • 1
    @VasilisCh No, scope is different concept that defines where you can use a name. What handles GC eligibility is reachability. – Sotirios Delimanolis Jan 07 '15 at 17:59
1

NEVER invoke an object's finalize() method manually. It is for the VM's use only. The VM will invoke it at an appropriate time (as part of the garbage collection process).

You may set a reference variable to null to possibly make an object eligible for garbage collection sooner than it otherwise would be, but doing so does not cause it to be GC'd immediately. (Note that you use the = operator for assignments; == is a relational operator for testing the equality of its operands.)

If your object maintains state that you must ensure is cleaned up on demand, then implement a different method for that. close() is a popular name for such methods. It is good practice to avoid implementing finalize() if at all possible. The GC will automatically take care of most resources that you might think you want to clean up manually in finalize().

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
  • Will overriding finalize method at that object and call it do any difference? As i read on [this](http://stackoverflow.com/a/18510658/4422074) answer it takes it into consideration, is it true? – Voqus Jan 07 '15 at 17:22
  • 1
    I repeat: **NEVER** invoke an object's `finalize()` method manually. It makes no difference whether it is the one inherited from `java.lang.Object` or an override. The answer you linked is excellent, by the way, and in no way does it contradict anything I said. – John Bollinger Jan 07 '15 at 17:33
  • @VasilisCh Don't override `finalize` unless you have a very good reason for it. Moreover, as a beginner just ignore everything GC-related. It does its job well and anything you do has a chance of making something worse. The cases you need to care about it are pretty rare (see e.g., http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/ArrayList.java#421) and you'll may it maybe once in a month when you do something advanced. – maaartinus Jan 07 '15 at 19:00
  • Thanks for replying @maaartinus, i was just intrigued by it since on C++ we must deallocate and stuff, and since JVM does that for us i thought maybe i could boost the performance a little more if i knew more about the Garbage Collector. Truth is i started low on a swing project of mine but turned out to keep growing since i wanted to add more and more functions to it. So i was just wondering since it already surpassed 40k lines of code, if i could help out in performance. – Voqus Jan 07 '15 at 19:18
  • @VasilisChados You're welcome. Java is very different. You must close streams and dispose graphics (using `try-finally` and not relying on `finalize` as it's not like a destructor; it gets called too late for most uses!), but you hardly ever need to care about memory. With Swing you need to unregister listeners when a component is no more needed. Java performance is much more complicated then C++ because of the JIT compiler. Before optimizing, make sure you know there's a problem and locate it carefully. – maaartinus Jan 07 '15 at 22:06
  • @maaartinus Yea, i noticed. The memory leaks here would be more difficult to pin down than in C++ i assume. Thanks for your time, pal. – Voqus Jan 07 '15 at 22:08