Is it needed to set reference to null?

O

omtechguy

Hi,

is it needed to set reference of a class to null when i finish to use
it or it will be done automaticly by the GC?
 
M

Matt

Hi,

is it needed to set reference of a class to null when i finish to use
it or it will be done automaticly by the GC?

Not sure what you mean by "done automatically by the GC". If an
object is garbage collected,it means nothing held a reference to it.
As a result, you can't possibly check it for null, since there would
be
nothing to check.

Matt
 
M

Matt

Actually, it means that the object was not reachable.  There may in fact
still be references to it (i.e. in other unreachable objects), but those
references themselves cannot be accessed and so the object is not
reachable.

Good clarification, thanks. The underlying point is the same, what
difference
would it make to the app as a whole?

Matt
 
A

Arne Vajhøj

Not sure what you mean by "done automatically by the GC". If an
object is garbage collected,it means nothing held a reference to it.

No. It means that it is not reachable.
As a result, you can't possibly check it for null, since there would
be
nothing to check.

Objects are GC'ed and setting the reference to null does not
make the object disappear.

Arne
 
A

Arne Vajhøj

is it needed to set reference of a class to null when i finish to use
it or it will be done automaticly by the GC?

It is not necessary.

And in most cases it is not recommended.

The always set to Nothing paradigm from VB* is a
bad practice in .NET.

Arne
 
M

Matt

[...]
Not sure what you mean by "done automatically by the GC". If an
object is garbage collected,it means nothing held a reference to it.
Actually, it means that the object was not reachable.  There may in fact
still be references to it (i.e. in other unreachable objects), but those
references themselves cannot be accessed and so the object is not
reachable.
Good clarification, thanks. The underlying point is the same, what
difference
would it make to the app as a whole?

I'm not really sure what your question means.

In terms of the question of the difference between "nothing held a
reference to it" and "is unreachable", there's a critical difference: GC
would not work nearly as efficiently if it worked as you described, because
it would take multiple collection iterations to free up objects that were
referenced within one or more levels of indirection.

With the "unreachable" implementation, objects are collected as soon as
they can be.

No argument here.

What I meant was, to the application code, there is no difference
whether the
object is unreachable, or whether it is referred to, since you
wouldn't have any
way to get at it in either case from your own code.

From the system's point of view, of course, you are absolutely
correct.

Matt
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top