Instantiating a class in.NET

  • Thread starter Thread starter Carl
  • Start date Start date
C

Carl

Hi,

I have a class in .Net which I instantiate in my code
behind on one of my aspx pages.

When I am finished with the instance, what is teh best
method of taking it out of memory. Should I set it to
nothing or can I dispose of it?

Thanks,
Carl.
 
Set it equal to nothing.
You shouldn't need any fancy disposal stuff unless the class is holding open
unusually valuable resources.
 
True, but it never hurts to be tidy.
In VB6 you theoretically didn't need to clean things up either, but the
reality was that sometimes the automatic cleanup didn't work quite as well
as it claimed.
It seems to behave better in .NET but I'm still not sure if I trust it 100%.
Better to be safe than sorry and clean up your own messes.
 
Steve,

Why even bother setting the reference to Nothing? This isn't COM - it won't
decrement the reference count.
 
My mother always taught me to clean up after myself.
Old habits die hard.
:-)
 
Steve,

Do you clean paper plates before discarding them? Then why set a variable
to null when it's going to disappear as soon as you leave the scope? :-)
 
I agree that it is not a step that is necessary.
But in general it's good practice to free memory as soon as you're done with
it.
By setting the object to nothing, you're permitting the garbage collector to
pick it up immediately if it so chooses, rather than forcing it to do the
deallocation later after the entire page is done processing and it goes
completely out of scope.
Granted, this isn't likely to give you major performance increases, but if
the object is large or holds valuable resources it could theoretically help
in some circumstances.
 
If those are the same Special Circumstances I was in about a year ago (and
for 15 months) then Good Luck.

I found a damp sponge to be very effective, if you then wash the sponge in
hot water...
 
Steve,

I would want to see some research indicating that this is a good practice
"in general". Among other things, there may well be more overhead in
individually setting variables to null than in waiting until they go out of
scope all at once.

Also, our brains only have so much bandwidth. I'd rather spend mine on
writing code which actually _does_ something rather than on remembering to
figure out when I'm "done with" some variable just so I can give the garbage
collector a few more milliseconds to clean up a few thousand bytes on a 2Gb
server system - a few milliseconds it will probably not take advantage of.

Old folks like me need to be careful not to teach the kids our old bad
habits - even those which used to be good habits. The world has changed too
much to remain static. Machines have grown much faster and have larger
memories, and a lot of the habits I learned as a "kid" just don't matter
now. If I'd been learning today instead of 25 years ago, I would not have
been taught to set variables to null after use. I'd wonder what the big deal
was and why we couldn't just wait the few milliseconds until the end of
scope to get allow the Garbage Collector to collect this few megabytes in a
couple of seconds.
 
Non-Sense for some servers that are hit by 10s - 100s of thousands of users
per day
What about Database Connections?
What about FileHandles?
The good old rules still apply
Machines are faster and we have more RAM but We have more demand on these
machines
Things can go out of hand with the new generation
Think about it for few years

J
 
Back
Top