Groups | Blog | Home
all groups > dotnet clr > may 2005 >

dotnet clr : Garbage Collector: set strong reference to null, why?


Scott M.
5/11/2005 12:00:00 AM
I'm not sure that anyone is recommending that you explicitly set your object
references to null. Everything I've seen and read in my experience with
..NET (since early betas) suggest that the only reason to explicitly set your
object references to null ("Nothing" in VB.NET) is when you want to let the
GC know that you are done with an object BEFORE the object reference would
NORMALLY go out of scope.

If you had an object reference that was taking up precious system resources
and you were done using it, but the procedure still had a lot of work to do,
you could "potentially* get the object cleaned up sooner than normal by
destroying the reference yourself prior to the end of the procedure.

Under normal circumstances, you do not need to worry about explicitly
destroying your object references.


[quoted text, click to view]

Manikkoth
5/11/2005 5:24:03 PM
Hello,
Why is it recommended to set the strong reference to null once the use is
over? Will the garbage collector be able to collect all my weak reference
objects, if I set it to a strong reference again and again without setting
the strong reference to null in between?

somewhat like this
StrongReferenceXYZ=weak1;
StrongReferenceXYZ=weak2;
StrongReferenceXYZ=weak3;

Do I need to set StrongReferenceXYZ=null in between?
clyon NO[at]SPAM online.microsoft.com (
5/12/2005 5:06:09 PM
Hi Manikkoth,

Scott is right, there are very few instances where setting a reference to
null actually makes a difference. See the blog post at:
http://blogs.msdn.com/clyon/archive/2004/12/01/273144.aspx for more
information.

Hope that helps
-Chris

--------------------|
| I'm not sure that anyone is recommending that you explicitly set your
object
| references to null. Everything I've seen and read in my experience with
| .NET (since early betas) suggest that the only reason to explicitly set
your
| object references to null ("Nothing" in VB.NET) is when you want to let
the
| GC know that you are done with an object BEFORE the object reference
would
| NORMALLY go out of scope.
|
| If you had an object reference that was taking up precious system
resources
| and you were done using it, but the procedure still had a lot of work to
do,
| you could "potentially* get the object cleaned up sooner than normal by
| destroying the reference yourself prior to the end of the procedure.
|
| Under normal circumstances, you do not need to worry about explicitly
| destroying your object references.
|
|
[quoted text, click to view]
| > Hello,
| > Why is it recommended to set the strong reference to null once the use
is
| > over? Will the garbage collector be able to collect all my weak
reference
| > objects, if I set it to a strong reference again and again without
setting
| > the strong reference to null in between?
| >
| > somewhat like this
| > StrongReferenceXYZ=weak1;
| > StrongReferenceXYZ=weak2;
| > StrongReferenceXYZ=weak3;
| >
| > Do I need to set StrongReferenceXYZ=null in between?
| >
|
|
|
AddThis Social Bookmark Button