How do i free objects in C#

2020-02-28 03:19发布

问题:

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

回答1:

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



回答2:

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.



回答3:

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.



回答4:

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

GC.Collect();


回答5:

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.



回答6:

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



回答7:

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.



回答8:

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.



回答9:

GC will collect all but unmanaged resources.

The unmanaged resources should implement IDisposable. If you are using an object that implements IDisposable, then you should either call the object's Dispose() method when it is no longer needed or wrap its instance in a using statement.