views:

249

answers:

8

Hi, Can anyone please tell me how I can free objects in C#? For example, I have an object:

Object obj1 = new Object();
//Some code using obj1
/*
Here I would like to free obj1, 
after it is no longer required 
and also more importantly 
its scope is the full run time of the program.
*/

Thanks for all your help

+2  A: 

You don't. You simply stop referencing them.

That's the job of the garbage collector.

Implement IDisposable on types that utilise unmanaged resources.

Wrap any instance that implements IDisposable in a using statement.

Mitch Wheat
or just use C++/CLI instead, so you don't have to remember which types implement IDisposable and which don't, it will automatically call Dispose when the object goes out of scope (if not declared as a handle)
Ben Voigt
hardly a compelling reason to use C++/CLI! :)
Mitch Wheat
@Ben Voigt If you implement IDisposable correctly then the Dispose function will be called when it goes out of scope in C# also.
ThanosPapathanasiou
@Thanos: nonsense. A little code example will demonstrate: C# void fn(void) { FileStream s = new FileStream("in.dat"); } s.Dispose is not called. C++ void fn(void) { FileStream s("in.dat"); } s.Dispose is called. Note that "put a using block in" isn't at all equivalent, the C++ syntax is identical for IDisposable and non-IDisposable objects, where C# doesn't allow using for non-IDisposable types, resulting in a huge maintenance if a type ever begins to implement IDisposable in a later version.
Ben Voigt
@Ben Voig: check this first: http://msdn.microsoft.com/en-us/library/system.idisposable.aspxwhen you have the using command its translated to try{x = new x() }finally{ x.Dispose() } and in the other case when it goes out of scope the destructor calls the private Dispose(bool disposing). In any case, Microsoft's correct implementation is what you can see in the link I provided.
ThanosPapathanasiou
@Thanos: MSDN documentation, unfortunately, isn't 100% accurate. That page says "It is a version-breaking change to add the IDisposable interface to an existing class, because it changes the semantics of the class." That's true in C# but not in C++/CLI. In C++/CLI your stack semantic variables get Disposed if they're IDisposable, no change to the client source code is needed if IDisposable is added. And you can make a smart pointer that calls delete on a handle, Dispose will get called if possible but you don't need special code to handle the difference between IDisposable and not.
Ben Voigt
@Thanos: Were you talking about C++/CLI or C#? C# has no destructor so it can't call Dispose from the destructor when the variable goes out of scope.
Ben Voigt
I was talking about C# and it does have destructors.
ThanosPapathanasiou
A: 

You stop referencing them and let the garbage collector take them.

When you want to free the object, add the following line:

obj1 = null;

The the garbage collector if free to delete the object (provided there are no other pointer to the object that keeps it alive.)

Arve
Don't bother nulling references unless the reference is a static. The reference will eventually go out of scope which will make the instance eligible for garbage collection.
Brian Rasmussen
+9  A: 

You don't have to. The runtime's garbage collector will come along and clean it up for you. That is why you are using C# and not unmanaged C++ in the first place :)

Chris
Will GC clean it up only if there are no references (or links) to the object anymore, or will it also free the object when GC detects that after a point the object is not used by the code at all (even if its scope is say the whole man method, because of which its lifetime will be the full run time of the program)?
assassin
@assasin: It collects when there are no references, period. GC doesn't do compiler's job. So it can't collect if there is a reference, even if it won't be used in the future. That's why we have to be careful about static objects. But in your main method example, it depends. References from stack have main scope. But the objects they point to may or may not depending on whether your references get reassigned.
Fakrudeen
A: 

You can use the using statement. After the scope the reference to the object will be removed and garbage collector can collect it at a later time.

rahul
This only applies to objects implementing IDisposable
Arve
+1  A: 

You do not. This is what a garbage collector does automatically - basically when the .NET runtime needs memory, it will go around and delete objects that are not longer in use.

What you have to do for this to work is to remove all linnks to the object.

In your case....

obj1=null;

at the end, then the object is no longer referenced and can be claimed from the garbage collector.

You can check http://en.wikipedia.org/wiki/Garbage_collection_(computer_science) for more details.

Note that if the object has references to unmanaged ressources (like open files etc.) it should implement the Disposable pattern (IDisposable interface) and you should explicitely release those references when you dont need the object anymore.

TomTom
There is no need to perform obj1=null;
Mitch Wheat
I was going to suggest assigning to null but a little reading says that can actually *delay* garbage collection in C#. How interesting. See this article for more on why you shouldn't assign to null in C#: http://blogs.msdn.com/csharpfaq/archive/2004/03/26/97229.aspx
Ian C.
+2  A: 

whatever you do, don't get tempted to force garbage collection via:

GC.Collect();
thekaido
It is not recomended to call GC.Collect. See http://blogs.msdn.com/ricom/archive/2004/11/29/271829.aspx
Arve
I wouldn't do it myself either, but he asked and it is a valid way to force objects to be freed.
thekaido
Suggesting someone call GC.Collect() is bad advice IMO.
Mitch Wheat
is this better?
thekaido
A: 

As others have mentioned you don't need to explicitly free them; however something that hasn't been mentioned is that whilst it is true the inbuilt garbage collector will free them for you, there is no guarantee of WHEN the garbage collector will free it.

All you know is that when it has fallen out of scope it CAN be cleaned up by the GC and at some stage will be.

Michael Shimmins
A: 

As Chris pointed out C# does most of the garbage collection for you. Instances where you would need to consider garbage collection is with the implementation of the IDisposable interface and when using WeakReference. See http://msdn.microsoft.com/en-us/library/system.idisposable.aspx and http://msdn.microsoft.com/en-us/library/system.idisposable.aspx for more information.

Cornelius