wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes set references to null in finalize method? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "set references to null in finalize method?" Watch "set references to null in finalize method?" New topic
Author

set references to null in finalize method?

Jon Dornback
Ranch Hand

Joined: Apr 24, 2002
Posts: 137
in any given class, should instance objects be set to null in the finalize() method? or does the compiler do that automatically, making this idea redundant?


use the [CODE] tags - it makes it much easier for people to help you.
Blake Minghelli
Ranch Hand

Joined: Sep 13, 2002
Posts: 331
I never use the finalize method and I think it's redundant. I also remember reading from another thread that doing so may actuallly slow your program down.


Blake Minghelli<br />SCWCD<br /> <br />"I'd put a quote here but I'm a non-conformist"
Jon Dornback
Ranch Hand

Joined: Apr 24, 2002
Posts: 137
the finalize method is actually very useful in some classes, particularly for database connections, threads, etc., that need to be closed when the class dies. usually this is taken care of by the VM (db connections, i believe, are automatically closed) - but i feel it is good programming practice to take care of things like that. consider daemon threads - if you don't explicity kill them, they will continue to run even after main() exits. so the question is, is it better to always take care of your objects (a good habit, especially if you ever use another language that doesn't automatically gc), or let the VM take care of things that you know it will automatically do?
any opinions/discussion?
Blake Minghelli
Ranch Hand

Joined: Sep 13, 2002
Posts: 331
I definitely think that resources should be closed explicitly by the programmer, especially db connections, as soon as they are no longer needed. I just don't think you need to set references = null.
Jeremy Thornton
Ranch Hand

Joined: Feb 21, 2002
Posts: 91
Much of this discussion depends on the environment that you are programming under.
For instand, releasing resources is importand in most environments but for most, setting references to null isn't essential. The jre will take care of unused references where possible but if you are programming in an environment such as j2me, you can make a real difference to the execution speed of the program by setting references to null and thereby explicitly telling the garbage collector that it can do its stuff.
The finalise method is **always** called regardless of execution path so is probably the best place to release resources.
Blake Minghelli
Ranch Hand

Joined: Sep 13, 2002
Posts: 331
But, isn't finalize() only called when gc'ed? In other words, it's not called if the program (e.g. Main method) terminates normally or if the program terminates abnormally (e.g. user does a ctrl+c). I'm not convinced that finalize is the best place to put resource clean-up, especially since gc is not guaranteed to happen right away???
:roll:
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
If the program runs from the command line, and the user hits control-C, what exception is thrown?


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Jeremy, is it possible you're confusing finalize with finally? As Blake notes, there's no guarantee that finalize will actually run unless the object is actually GC'd - and there's no guarantee that will ever happen. In general, I would only use finalize() if I can't find an explicit way to guarantee that resources are released when they're no longer needed. Often this is best achieved using try/finally. But for some applications it's not possible (or at least, not easy) to do this, and you have to rely on garbage collection and finalize(), hoping for the best.
I also agree with Blake that setting references to null in a finalizer is generally unnecessary. They're all going to be null shortly anyway. As Jeremy notes, you may be able to speed GC up slightly in some cases - but I would only bother with this for objects containing links to other particulary resource-heavy objects which need to be freed ASAP. I wouldn't advocate setting null in finalizers as a general policy for objects - you'll just get a lot of bloated code that doesn't accomplish much.
Note that it's certainly possible to achieve significant effects by setting references to null in places other than finalizers. (Which may be what Jeremy was referring to; not sure.) I'm just skeptical that it makes much difference in finalizers.
[ September 20, 2002: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4447
    
    5

Y'all might want to read this JavaWorld article by Bill Venners to clear up your doubts and misconceptions.
Note that the article mentions that "By default, the garbage collector will not execute the finalizers of any objects left on the heap when the application exits." That means that it's possible that an object will NOT be finalized at all. Also note that System.runFinalizersOnExit method has been deprecated because it is unsafe.


Junilu - [How to Ask Questions] [How to Answer Questions]
Jeremy Thornton
Ranch Hand

Joined: Feb 21, 2002
Posts: 91
Yup, totally confusing finalize with finally.
Doh!
... and I blame bad API design for that!
[ September 20, 2002: Message edited by: Jeremy Thornton ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: set references to null in finalize method?
 
Similar Threads
When can we use the method finalize();?? and why do we use it???
what do we do in unreferenced()?
Garbage Collection
Finalization Method
finalize method