Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What is the benefit of setting object to nothing after their use?

Status
Not open for further replies.

chuckdiesel

Programmer
Jun 17, 2003
5
US
Won't the garbage collector notice that they are not attached to anything and clean them up just the same?

Maybe the = nothing will somehow flag the garbage collector but will that really matter considering that we have no control over when it collects?

Also doesn't the garbage collector collect when it needs the memory...not just to clean up?

I guess I am asking why anyone would want to go to the trouble of setting each old object to nothing?

 
If you don't set an object to nothing, how is the garbage collector to know it can be collected or not? Hence set to nothing.

Note, you can control when it collects via the GC.Collect method.

Craig
 
This what MSDN is saying


The biggest worry for a program in the managed world is memory retention. Some of the problems that you'll find in unmanaged environments are not an issue in the managed world: memory leaks and dangling pointers are not much of a problem here. Instead, programmers need to be careful about leaving resources connected when they no longer need them.

The most important heuristic for performance is also the easiest one to learn for programmers who are used to writing native code: keep track of the allocations to make, and free them when you're done. The GC has no way of knowing that you aren't going to use a 20KB string that you built if it's part of an object that's being kept around. Suppose you have this object tucked away in a vector somewhere, and you never intend to use that string again. Setting the field to null will let the GC collect those 20KB later, even if you still need the object for other purposes. If you don't need the object anymore, make sure you're not keeping references to it. (Just like in native code.) For smaller objects, this is less of a problem. Any programmer that's familiar with memory management in native code will have no problem here: all the same common sense rules apply. You just don't have to be so paranoid about them.


-Kris
 
"If you don't set an object to nothing, how is the garbage collector to know it can be collected or not? Hence set to nothing.

Note, you can control when it collects via the GC.Collect method."


I believe that your entire post if false....isn't it?


"If you don't set an object to nothing, how is the garbage collector to know it can be collected or not?"

This is the entire premise behind a garbage collector....it cleans up your unused objects for you. If you have to tell it to clean up every object then we are back in C land again. The garbage collector to my knowledge scans back and forth on the heap and checks to see which objects still have references and which don't (even if the object is not set to nothing the garbage collector will know if it is not referenced in your code) and then can possibly (if memory is needed) clean that unused object up. But if memory is not needed it will do little to no cleanup. I have read a little on the garbage collector and that I what I have come away with. If I am wrong then tell me cause I want to know.


"Note, you can control when it collects via the GC.Collect method."


Also, this sentence if false because when you call GC.Collect you are merely telling the GC to check and see if any objects need to be cleaned up due to memory constraints...but if you have plenty of memory the GC will not clean them up. I don't think there is any way possible to tell when the GC will run....as a matter of fact I don't really care..all I care is that .NET is taking care of that in the backgroud...isn't that the benefit of a virtual machine language?
 
Objects are eligible for garbage collection when they go out of scope, or when they're explicitly set to nothing.

Reasons for setting them to nothing?
1) The object is a wrapper for a unmanaged resource (you should call Dispose on it first) such as a database connection or stream handle.
2) You want to minimize your memory footprint, so you free the memory as soon as you're done with it, rather than waiting for the end of the method where it will go out of scope.

Sometimes objects won't be garbage collected for a long time -- usually when the object is very large (> 85k is the number I've seen), because the CPU cost of compacting memory after freeing it would be too expensive.

Chip H.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top