Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it necessary to free objects in final block of try-catch block?

Tags:

c#

try-catch

The question is self explanatory :

Foo objfoo;
try
{
  objfoo = new Foo();
  ..........
  .........
}
catch
{

}
finally
{
  objfoo = null;
} 

Is it necessary to free objects like this ?

like image 459
harrisunderwork Avatar asked Dec 13 '22 17:12

harrisunderwork


1 Answers

Note: Setting a local / field to null is not freeing the value. It is instead removing a reference to the value which may or may not make it elligable for collection during the next GC cyle.

To answer the question, no it is not necessary. The JIT`er will calculate the last time a local is used and will essentially remove the local as one of the object's GC roots at that time. Nulling the local out will not speed up this process.

Raymond Chen did an excellent article on this very subject

  • http://blogs.msdn.com/b/oldnewthing/archive/2010/08/10/10048149.aspx
like image 129
JaredPar Avatar answered May 14 '23 13:05

JaredPar