خرید بک لینک

Vote count: 11

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

asked Mar 9 '10 at 5:07

8 Answers

Vote count: 16

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 :)

answered Mar 9 '10 at 5:11

Vote count: 6

You don't have to. You simply stop referencing them, and the garbage collector will (at some point) free them for you.

You should implement IDisposable on types that utilise unmanaged resources, and wrap any instance that implements IDisposable in a using statement.

answered Mar 9 '10 at 5:09

Vote count: 4

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 liks 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 patte (IDisposable interface) and you should explicitely release those references when you dont need the object anymore.

answered Mar 9 '10 at 5:12

Vote count: 3

It's not recommended, but if you really need to, you can force garbage collection via:

GC.Collect();

answered Mar 9 '10 at 5:17

Vote count: 1

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.

answered Mar 9 '10 at 5:11

Vote count: 0

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.)

answered Mar 9 '10 at 5:09

Vote count: 0

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.

answered Mar 9 '10 at 5:34

Vote count: 0

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.

answered Mar 9 '10 at 6:00

برچسب: نویسنده: استخدام کار تاريخ: جمعه 7 خرداد 1395 ساعت: 13:54

صفحه بندی