Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

set references to null in finalize method?

 
Jon Dornback
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Blake Minghelli
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jon Dornback
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the program runs from the command line, and the user hits control-C, what exception is thrown?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Junilu Lacar
Bartender
Posts: 7466
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeremy Thornton
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup, totally confusing finalize with finally.
Doh!
... and I blame bad API design for that!
[ September 20, 2002: Message edited by: Jeremy Thornton ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic