Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

closing file from finalize() and explicitly calling finalize()

 
James Clarke
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Iam closing a file from the body of the finalize() method and am not sure what will happen if an exception is thrown by the close() method.

The javadoc state the following (Iam not exactly sure what this means, I mean if the finalization is terminated, then the object is not garbage collected?):
If an uncaught exception is thrown by the finalize method, the exception is ignored and finalization of that object terminates.

Also thinking about explicitly calling the finalize method.

Does anyone know what the best practice guidelines are regarding these two things?

thanks in advance,

James
 
John Donoghue
Ranch Hand
Posts: 45
IntelliJ IDE Mac OS X Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The general wisdom is that should never rely on finalize() to do any important processing (like closing a file). This is especially tricky for people linke me coming from C++ backgrounds, because it looks like a C++ destructor. Joshua Block's book Effective Java has a nice description.

The method is guaranteed to be called once. However, that doesn't mean that the object is garbage collected. Another object may hold a reference to it, making it "reachable", and so the garbage collector won't reclaim it, although finalize has run. On the other hand, the garbage collector may never run, and then

If you were going to call finalize() explicitly, it is probably better to either: just perform the shut-down logic directly in the code that would call finalize(), or have another method that is called explicitly. You can call it, or finalize() can call it, or both can call it (assuming you write it to be safe to call more than once).

If you want to make sure some resource is correctly handled (like shutting a file), take a look at addShutdownHook() in java.lang.Runtime. This registers methods that run as the JVM shuts down, regardless of how the shut-down is initialized (closing the GUI, the user pressing Cntl-C etc.). The SCJD book by Monkhouse & Camerlengo contains a nice description of it.
 
Jens Voss
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by John Donoghue:

If you want to make sure some resource is correctly handled (like shutting a file), take a look at addShutdownHook() in java.lang.Runtime. This registers methods that run as the JVM shuts down, regardless of how the shut-down is initialized (closing the GUI, the user pressing Cntl-C etc.). The SCJD book by Monkhouse & Camerlengo contains a nice description of it.


Just a little comment here: You cannot be a hundred percent certain that a registered shutdown hook is ever executed either. If you shut down the process using kill or end it via the Windows Task Manager, the shutdown hook will be bypassed, too!

Regards,
Jens
 
James Clarke
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

Thanks for the responses. After digging around a few places this is the conclusion I have come to:

In a commercial environment it would not be acceptable to rely on finalize(0) to perform any critical operations as there is no guarantee when or even if it will ever be called, since this is dependant on the JVM in use.

The doubt I have though is that the project will be run on a standard JVM, will this have "normal" garbage collection which calls the finalize method?

I did find another way to close the file, but to close some of my sockets I still rely on finalize(), if I can find out about the above doubt, it would be great since I will know about the use of finalize() for sure.

thanks,

James
 
Chandrakant Dhome
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi James,
As you are trying to close the file & sockets in finalize(), my understanding is that, you are keeping them open throughout the life of a program? If this is not entirely necessary, I will advice you to avoid that approach.(If file is small enough, you can load the file contents in memory block and use them afterwards). Anyway this is not the question here.
In regular practice I always open file/sockets inside required method using try block and close it in finally. Again file close operation is inside finally, also enclosed inside small try-catch which, if failed, will log the error. There can be an exception while closing the file/socket, but atleast logs will be able to show you what went wrong...This is the best approach I can think of...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic