Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Release resources in .Net C#

I'm new to C# and .NET, ,and have been reading around about it.

I need to know why and when do I need to release resources? Doesn't the garbage collector take care of everything? When do I need to implement IDisposable, and how is it different from destructor in C++?

Also, if my program is rather small i.e. a screensaver, do I need to care about releasing resources?

Thanks.

like image 608
zaidwaqi Avatar asked May 04 '10 09:05

zaidwaqi


People also ask

How do you release an object in C#?

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

How do you release unmanaged resources in C#?

The following are two mechanisms to automate the freeing of unmanaged resources: Declaring a destructor (or Finalizer) as a member of your class. Implementing the System. IDisposable interface in your class.

Do I need to call Dispose C#?

Rule of thumb: if a class implements IDisposable you should always call the Dispose method as soon as you have finished using this resource. Even better wrap it in a using statement to ensure that the Dispose method will be called even if an exception is thrown: using (var reader = conn. ExecuteReader()) { ... }

What are managed resources in C#?

Managed resources are those that are pure . NET code and managed by the runtime and are under its direct control. Unmanaged resources are those that are not. File handles, pinned memory, COM objects, database connections etc.


2 Answers

The garbage collector is only aware of memory. That's fine for memory, because one bit of memory is pretty much as good as any other, so long as you've got enough of it. (This is all modulo cache coherency etc.)

Now compare that with file handles. The operating system could have plenty of room to allocate more file handles - but if you've left a handle open to a particular file, no-one else will be able to open that particular file for writing. You should tell the system when you're done with a handle - usually by closing the relevant stream - as soon as you're finished, and do so in a way that closes it even if an exception is thrown. This is usually done with a using statement, which is like a try/finally with a call to Dispose in the finally block.

Destructors in C++ are very different from .NET finalizers, as C++ destructors are deterministic - they're automatically called when the relevant variable falls out of scope, for example. Finalizers are run by the garbage collector at some point after an object is no longer referenced by any "live" objects, but the timing is unpredictable. (In some rare cases, it may never happen.)

You should implement IDisposable yourself if you have any clean-up which should be done deterministically - typically that's the case if one of your instance variables also implements IDisposable. It's pretty rare these days to need to implement a finalizer yourself - you usually only need one if you have a direct hold on operating system handles, usually in the form of IntPtr; SafeHandle makes all of this a lot easier and frees you from having to write the finalizer yourself.

like image 59
Jon Skeet Avatar answered Sep 21 '22 13:09

Jon Skeet


Basically, you need to worry about releasing resources to unmanaged code - anything outside the .NET framework. For example, a database connection or a file on the OS.

The garbage collector deals with managed code - code in the .NET framework.

Even a small application may need to release unmanaged resources, for example it may write to a local text file. When you have finished with the resource you need to ensure the object's Dispose method is called. The using statement simplifies the syntax:

using (TextWriter w = File.CreateText("test.txt"))
{
    w.WriteLine("Test Line 1");
}

The TextWriter object implements the IDisposable interface so as soon as the using block is finished the Dispose method is called and the object can be garbage collected. The actual time of collection cannot be guaranteed.

If you create your own classes that need to be Disposed of properly you will need to implement the IDisposable interface and Dispose pattern yourself. On a simple application you probably won't need to do this, if you do this is a good resource.

like image 44
Joe Ratzer Avatar answered Sep 17 '22 13:09

Joe Ratzer