File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes What is the right use of finally block ?  + Performance of Program. Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "What is the right use of finally block ?  + Performance of Program." Watch "What is the right use of finally block ?  + Performance of Program." New topic
Author

What is the right use of finally block ? + Performance of Program.

Jigar Naik
Ranch Hand

Joined: Dec 12, 2006
Posts: 744
Hi,

Please have a look at bellow method which i have written for createing xml Document Object.



I just wanted to know whether i am using the finally block corrently or not. correctly in the sense is this the right use of finally block.

does checking all the object for null and making it null effect my program's performance ?

If it is not right use of finally. what are the things which we need to clean in finally block.

Jigar Naik


Jeremie Blais
Greenhorn

Joined: May 21, 2006
Posts: 14
Hi Jigar,

The finally clause is usually used to "clean" resources, as you mentioned. Cleaning means setting your variables to null and closing resources. In your example, I would add documentBuilder.close() to the finally clause. It will affect performance, as the garbage collector will be able to detect unreferenced variables more quickly if you always assign null to your variables once you are done using them. It is essential to close all resources when you are done using them.



Regards,

Jeremie
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56179
    
  13

What is the purpose of setting these variables to null?


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24051
    
  13

Note that there is absolutely, positively no benefit to setting local variables to null right before a method exits like this. As soon as the method returns, the variables are effectively nulled out, and they don't count as live references anymore. I would say this is a misuse of finally. It's especially silly to test whether a variable is null before setting it to null -- if you want it to be null, just set it!

Using finally to call close(), though, is an excellent practice. I'd reduce your finally block to nothing more than



[Jess in Action][AskingGoodQuestions]
Jeremie Blais
Greenhorn

Joined: May 21, 2006
Posts: 14
When you set a variable to null, you are telling the garbage collector that you don't need that variable anymore. When the garbage collector notices that you don't need that variable, it will consider the memory taken by the variable as free and it will allocate it for something else when it is needed.

Let me know if you have more questions,

Jeremie
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56179
    
  13

Jeremie Blais wrote:When you set a variable to null, you are telling the garbage collector that you don't need that variable anymore. When the garbage collector notices that you don't need that variable, it will consider the memory taken by the variable as free and it will allocate it for something else when it is needed.
That will happen anyways when the variable goes out of scope. Setting it to null in such a case is just plain silly.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24051
    
  13

Jeremie Blais wrote:When you set a variable to null, you are telling the garbage collector that you don't need that variable anymore.


Returning from the method in which the local variable is declared has the same effect, without running any extra code. This kind of thing is a terrible anti-pattern; it's pretty much cargo-cult programming.

Now, setting long-lived variables to null when they're not needed can make sense. More often, it's removing unused items from a widely-accessible collection, rather than explictly nulling variables. But in any case, it's about getting rid of references which, if you didn't discard them, would prevent an object from being collected for some period of time. Here, the variables will disappear immediately after the nulling code, and so that's just silly.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24051
    
  13

Is there an echo in here?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56179
    
  13

Is there an echo in here?

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 16483
    
    2

Ernest Friedman-Hill wrote:This kind of thing is a terrible anti-pattern; it's pretty much cargo-cult programming.


It can get worse; a couple of years ago I inherited some code in which a class overrode the finalize() method simply to set an instance variable to null. Not only is that just plain silly, it interferes with the normal garbage collection process just to be silly.
Jeremie Blais
Greenhorn

Joined: May 21, 2006
Posts: 14
Ernest Friedman-Hill wrote:

Returning from the method in which the local variable is declared has the same effect, without running any extra code. This kind of thing is a terrible anti-pattern; it's pretty much cargo-cult programming.

Here, the variables will disappear immediately after the nulling code, and so that's just silly.


I agree with you here. I just prefer to be explicit. There is no harm in always nulling variables out. It takes two seconds to write a line of code that will null out a variable. Plus, it is an easy way and straightforward way to explain the concept of garbage collection and nulling out values to a beginner.

I agree with your programming style, I am just expressing my point of view. Different people have different programming styles.

Correct me if I'm wrong.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56179
    
  13

Jeremie Blais wrote:There is no harm in always nulling variables out.
I disagree. It adds to code bloat and makes the code unnecessarily complex and confusing. Especially to those that don't know any better. Look at the code that started this thread. You find all the unnecessary try and catching in it acceptable?
Jeremie Blais
Greenhorn

Joined: May 21, 2006
Posts: 14
Bear Bibeault wrote:I disagree. It adds to code bloat and makes the code unnecessarily complex and confusing. Especially to those that don't know any better. Look at the code that started this thread. You find all the unnecessary try and catching in it acceptable?


My question is, if we always have to explicitely initialize variables, why is it bad to explicitely null out variables when we are done with them?
Jeremie Blais
Greenhorn

Joined: May 21, 2006
Posts: 14
Bear Bibeault wrote:Look at the code that started this thread. You find all the unnecessary try and catching in it acceptable?


I agree the catch( Exception e ) is bad design. We should catch explicit exceptions or declared them in a throws clause. That is being explicit. Why shouldn't we be explicit by nulling out values?
Jigar Naik
Ranch Hand

Joined: Dec 12, 2006
Posts: 744
Hi Jeremie ,Bear, Paul & Ernest

Thanks a lot for your time and giving me such a wonderfull clarification.

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56179
    
  13

Jeremie Blais wrote:My question is, if we always have to explicitely initialize variables, why is it bad to explicitely null out variables when we are done with them?
Already answered. Unnecessary. Confusing. Obfuscating. Bloated. Silly.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is the right use of finally block ? + Performance of Program.
 
Similar Threads
ProcessingInstruction - how to place before root elem
XML String to Document
DocumentBuilder.parse not working for String input
Document to Representation
Coversion of String data into a xml file