• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Ranch Hand
Posts: 762
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the purpose of setting these variables to null?

 
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Dario Stepanos
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there an echo in here?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there an echo in here?

 
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dario Stepanos
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Dario Stepanos
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Dario Stepanos
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 762
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jeremie ,Bear, Paul & Ernest

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

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!