Resources that have to be manually cleaned up in C#? - c#

What resources have to be manually cleaned up in C# and what are the consequences of not doing so?
For example, say I have the following code:
myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Black);
// Use Brush
If I don't clean up the brush using the dispose method, I'm assuming the garbage collector frees the memory used at program termination? Is this correct?
What other resources do I need to manually clean up?

If you don't dispose something, it'll be cleaned up when the garbage collector notices that there are no more references to it in your code, which may be after some time. For something like that, it doesn't really matter, but for an open file it probably does.
In general, if something has a Dispose method, you should call it when you've finished with it, or, if you can, wrap it up in a using statement:
using (SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Black))
{
// use myBrush
}

Handles to internal windows data structures.
Database connections.
File handles.
Network connections.
COM/OLE references.
The list goes on.
It's important to call Dispose or even better yet, use the using pattern.
using (SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Black))
{
// use myBrush
}
If you don't dispose something, it'll be cleaned up when the garbage collector notices that there are no more references to it, which may be after some time.
In the case of System.Drawing.Brush, Windows will keep internal windows structures for the brush loaded in memory until all programs release their handle.

The consequences of not disposing your IDisposables can vary from a negligible performance hit to crashing your app.
The Brush object in your example will be cleaned up by the GC when it feels like it. But your program won't have had the benefit of that bit of extra memory you would have gained by cleaning it up earlier. If you are using a lot of Brush objects this might become significant. The GC is also more efficient at cleaning up objects if they haven't been around very long, because it is a generational garbage collector.
On the other hand, the consequences of not disposing database connection objects could mean you run out of pooled database connections very quickly and cause your app to crash.
Either use
using (new DisposableThing...
{
...
}
Or, if you need to hold on to a reference to an IDisposable in your object for its lifetime, implement IDisposable on your object and call the IDisposable's Dispose method.
class MyClass : IDisposable
{
private IDisposable disposableThing;
public void DoStuffThatRequiresHavingAReferenceToDisposableThing() { ... }
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
//etc... (see IDisposable on msdn)
}

Generally, anything that implements IDisposable should cause you to pause and research the resource you're using.
GC only happens when there's memory pressure, so you cannot predict when. Although an unload of the AppDomain will certainly trigger it.

Technically anything that inherits from IDisposable should be proactively disposed. You can use the 'using' statement to make things easier.
http://msdn.microsoft.com/en-us/library/yh598w02.aspx
Sometimes you will see inconsistent use of IDisposable derived objects in documentation sample code as well as code that is generated by tools (i.e. visual studio).
What's nice about IDisposable is that it gives you the ability to proactively release the underlying unmanaged resource. Sometimes you really want to do this - think network connections and file resources for example.

As others have said, using is your friend.
I wrote this blog entry about how to implement IDisposable in a fairly straightforward way that is less error-prone by factoring out the parts that are the most important.

A trick I use when I can't remember whether a given object is a disposable resource is to type ".Dispose" (at most!) after the declaration to get Intellisense to check for me:
MemoryStream ms = new MemoryStream().Dispose
Then delete the .Dispose and use the using() directive:
using(MemoryStream ms = new MemoryStream())
{
...
}

Well, as long as you use the managed version of the resources and don't call the windows APIs by yourself, you should be OK. Only worry about having to delete/destroy a resource when what you get is an IntPtr, as "windows handles" (and a whole lot other things) are known in .NET, and not an object.
By the way, the resource (as any other .NET object) will be flagged for collection as soon as you leave the current context, so if you create the Brush inside a method, it will be flagged when you exit it.

If it's managed (i.e. part of the framework) you don't need to worry about it. If it implements IDisposable just wrap it in a using block.
If you want to use unmanaged resources then you need to read up on finalisers and implementing IDisposable yourself.
There's a lot more detail under this question

First upon program termination, you can assume that memory used by the process will be eliminated with the process itself.
While using dispose or destructor in.net, one must understand that the time of when the dispose function is called by the GC is non-deterministic. That why it is recommended to use the using or calling the dispose explicitly.
When using resources such as files, memory objects such as semaphors and resources that live outside of the managed world of .net must be freed.
The SolidBrush for example, you need to dispose because it is a GDI object and living outside of the .net world.

The garbage collector does not only free up at program termination, otherwise it would not be really useful (on any decent/recent OS, when the process exits, all its memory is cleaned up automatically by the OS anyway).
One of the big advantage of C# compared to C/C++ is that you don't have to care about freeing allocated objects (most of the time at least); the gc does it when the runtime decides (various strategies when/how to do it).
Many ressources are not taken care of by the gc: file, thread-related ressources (locks), network connections, etc...

One place to be careful is Objects that look small to GC but are not... In the SharePoint API for example, the SPWeb object has a small footprint as far as the GC is concerned and so will have low priority for collection, but it has really grabbed a bunch of memory (in the heap I believe) that the GC doesn't know about. You will run into some fun memory issues if you are foreaching a whole bunch of these for example, always remember to use using or dispose!

Rather than thinking of an object as "holding" resources that need to be released, it's better to think in terms of an object as having altered something (possibly outside the computer!) which will outlive it, in a way could be harmful if it not undone or "cleaned up", but which only the object can clean up. While this alteration commonly takes the form of some concrete object in a pool being marked "busy", its precise form doesn't matter. What matters is that the changes need to be undone, and the object holds information necessary to do that.

The garbage collector will handle any managed resources. In your example, the brush will be cleaned up when the garbage collector decides to, which will happen some time after the last reference to the brush is no longer valid.
There are certain things that need to be manually cleaned up, but those are pointers retrieved from unmanaged sources, such as DLL calls, nothing within the .NET Framework needs this treatment however.

Related

I'm not sure if I need to manually 'clean up' my objects or if the garbage collector will take care of it

I've been researching the IDispose interface and garbage collection however I'm struggling to understand what I need to do (if anything) to ensure my objects are 'taken care of' appropriately in certain instances.
I know that I should use using (i.e. the syntactic sugar for Dispose()) to make sure I am releasing unmanaged resources, however when I create a temporary object I am not sure if I need to do something to actively release it or if the garbage collector will take care of it the same way it would a temporary variable.
For example:-
public void LoadSerializedClass(string filePath)
{
Binary formatter = new BinaryFormatter();
if (File.Exists(filePath))
{
using (Stream input = File.OpenRead(filePath))
{
MySerializedClass mySerializedClass
= (MySerializedClass)formatter.Deserialize(input);
/* ... use the deserialized class ... */
/* ... the deserialized class is no longer needed... */
}
}
}
I use using to make sure the unmanaged resource is disposed of properly.
(At the moment) I do not do anything to dispose of the (intended to be) temporary instance of my MySerializedClass - I (perhaps very mistakenly) thought it would behave as a local temporary variable would, and I would not need to manage it. Am I incorrect? I got the impression from the research I did that I would not have to do anything, and the garbage collector will automatically pick it up; however a colleague gave the impression otherwise and I am obviously struggling to understand.
Thank you!
You only need to Dispose of objects that implement IDisposable. Your variable is fine. Even with objects which implement IDisposable the GC will get rid of them at some point if you do not dispose them. Problem is that the unmanaged resources would not be released in the meantime and you can starve the operating system of that resource or cause other problems. For example if you open a file and don't close it the OS will think the file is in use and won't let the user delete it. The GC will eventually collect the managed object and release the file but that may take quite a long time if your program is not under memory pressure.
For more information on how the GC works you may want to check some article about it. Basically if the GC decides that it needs to free some memory it starts with all the variables that are in scope + all static fields and goes through all the objects they reference and then recursively all the way down. When it is done marking it just deletes the unmarked objects. This is called Mark and Sweep (there are different strategies for GC this is just one of them). The GC does not look for unused objects it looks for used objects and deletes the rest.
As for IDisposable a properly implemented IDisposable will have a Finalizer (syntax is like C++ destructor ~). If the GC sees an object with a Finalizer it makes a note to call it before deleting an object. Normally the finalizer will do what the Dispose method would and release the unmanaged object. However note that the GC is only concerned with memory used by the program so it does not care about file handles or network connections. Maybe the OS needs these resources but from the point of view of the GC everything is fine since there is enough memory and it does not feel like calling these finalizers. This is why we explicitly need to release (dispose) the unmanaged resources.

Memory release with IDisposable and without IDisposable

In my app I have a large object that's created every few seconds. I do with it some job and then I don't need it anymore.
I saw in the task manager that the ram size goes up even if I don't have any reference to the object and it needs to be collected.
After implementing IDisposable the ram goes down immediately.
Why is this? I didn't do GC.Collect, I just released the object and told the GC it doesn't need to call the finalizer for my object.
EDIT:
Here is the code I use for my IDisposable objects:
public void Dispose()
{
base.Dispose();
Dispose(true);
GC.SuppressFinalize();
}
private void Dispose(bool disposing)
{
//here i release unmanaged resources
if (disposing)
{
//here i release all managed resources
}
}
~MyObject()
{
Dispose(false);
}
In my large object, I do myObject.Dispose(); after I don't need it anymore.
My question was not about how to implement IDisposable or how GC works in general. I just want to know how is it possible that there's a difference when I dispose of the object myself or let GC do its job.
With .NET memory doesn't have 2 states as usual (used and unused), but in effect 4: (A) Used by live object, (B) Used by dead object, (C) Not used by any object, but by the framework and (D) not used.
When you create an object, the Framework will first try to use memory of category (C) for it, is there isn't enough usable left of it, it will ask the OS for some of type (D), will convert it into (C) and then use it for your object.
When an object goes out of scope, it will fall from (A) to (B), on the next garbage run it will go from (B) to either (C) or (D). This depends heavily on the internal structures (think memeory fragmentation and friends), of the memory pressure and of the type of object collected (think IDisposable and friends)
What you want to achieve is, that as fast as possible after your big Object goes out of scope, the memory it used goes to (D). Here are some hints:
Make sure your object's size is a multiple of 4K - this makes it much more likely, that no other object shares a memory page with your object, so it can more easily be given back to the OS
Experiment with a AddMemoryPressure() bracket around object disposal, but be aware of side effects. This is similar to forcing GC.Collect(), but less intrusive: It will hint the GC to collect soon, but not exactly now
Rethink your basic concept: Is the big object a Singleton (i.e. only one can exist at any given time)? If yes, think of allocating it once and recycling it: This will make your Applications RAM requirements more predictable - and possible save you from ugly OOM situations while running.
As #Steven pointed out in his comments, IDisposable is something the CLR doesn't care about. By implementing it you simply tell consumers of your object to call its Dispose method when object no longer needed. In terms of mem management you can make up your own interface for this, say IDisposable2, and get the same technical effect. This would however be a stupid thing to do because .net developers should know to call idisposable.dispose when object no longer needed. Also, there is built in language (c#) support for this interface (using {...}).
You write
"I tried to implement IDisposable and then the ram goes down immideitly."
What matters is what your "implement" does. The code there should clean up unmanaged code, windows resources etc.
Using IDisposable means that you are implementing Dispose method where you put all your clean up code for the resources that you dont need anymore. It does not remove your object to the managed heap, the GC will still be the one responsible for freeing up the memory.
When you implement IDisposable, and dispose the object, you are marking the object as available to be garbage-collected.
You can't predict exactly when it will be collected though.

Using destructor/finaliser expensive?

I'm busy getting confused about non-deterministic destruction. In an answer to another question I got the advice that destructors/finalisers (which I assume are the same thing in c#, i.e. the function called ~classname()) are expensive and not required. But looking at this example a destructor is used and from the comment sounds like it may be vital. Anyone got some advice about how this all fits together and should I remove the destructor from my code?
Thanks again.
You should only include a finalizer if you absolutely have to run some cleanup at some point, whether it's performed explicitly or not. In such cases you should always have an explicit way of performing clean-up in a timely manner anyway, and that should suppress finalization anyway, so that "good" clients don't see any performance penalty.
You would normally only need a finalizer if you have a direct handle to unmanaged resources - if you only have a reference to another class which has a handle on the resource (e.g. FileStream) then you should leave it to the other class to have a finalizer.
With the advent of SafeHandle in .NET 2.0, situations where it's worth writing your own finalizer are very rare indeed.
The performance penalty of finalizers is that they make your objects live for longer than they need to: in the first GC cycle where they're otherwise considered eligible for collection, they get put on the finalizer queue - and bumped up to the next generation just like any other object which survives a GC cycle. The finalizer will then run in another thread (at some point) and only then will they be eligible to really be collected. So instead of (say) getting collected in the first gen1 collection, they live on past that until the next gen2 collection, which may be considerably later.
Usually, implementing a destructor is useful in on case: when it is not guaranteed, that client code will close all the resources(file streams, db connections and so on) properly. So, if client code will fail to do so, you will have code, which will close it, which is better than just left the resources open.
You only need the full Disposable pattern when you are dealing with an unmanaged resource directly. And then it is up to your calling code to make sure the destructor is (almost) never used.
When dealing with managed resources (= indirect ownership of unmanaged resources), the destructor is useless:
class FileWrapper
{
private FileStream fs; // managed resource
~FileWrapper()
{
if (fs != null)
fs.Dispose(); // fs is already on the GC finalizer queue
}
}
Whenever a FileWrapper object is being collected by the GC, it is certain that the fs object is in the same batch. So the call to fs.Dispose() is useless, only testing the correct (allowing multiple calls) behaviour of the FileStream.Dispose().
The only useful destructor here is the one in FileStream.

When do I need to use dispose() on graphics?

I'm learning to draw stuff in C# and I keep seeing recommendations to use dispose(), but I don't quite understand what it does.
When should I be using dispose() on a
code-drawn graphic?
What happens if I
don't?
Do I need to call it every
time a graphic is not visible, such
as on a GUI that has tabs and the
user switched to the other tab, and
then redraw it when they switch back?
Will I break things if I call it when I shouldn't?
Will Batman escape the evil clutches of the Joker?
When should I be using dispose() on a code-drawn graphic? Whenever you are completely done with any object that implements IDisposable, you should call Dispose just before it becomes eligible for garbage collection. As others have pointed out, it's best to use the using statement instead of calling Dispose directly.
What happens if I don't? Your program will be slightly less efficient because it will take up slightly more resources than necessary. This is the only drawback to not disposing graphics; however, not disposing other classes can actually cause errors (one famous example is StreamWriter). For this reason, it's best to always dispose any class that implements IDisposable, as a general rule.
Do I need to call it every time a graphic is not visible, such as on a GUI that has tabs and the user switched to the other tab, and then redraw it when they switch back? No. Objects should only be disposed when you are completely done with them.
Will I break things if I call it when I shouldn't? Yes. If you call Dispose before you are done with the object, and then attempt to use the disposed object, you will get an ObjectDisposedException.
Will Batman escape the evil clutches of the Joker? Tune in tomorrow, same bat-time, same bat-channel!
When you ask for a graphical object, Windows will allocate a bit of memory for you. Calling dispose will tidy up that memory for you. If you do not call dispose, all of these handles to memory wil remain open and eventually your system will run out of resources, become slower and eventually stop (closing the program may free them however).
Because you're using .NET, when you have finished using your graphics object, the garbage collector will eventually call dispose for you. The problem with the garbage collector is you never know when it will clean up the object, so it may leave these resources open for longer than necessary.
This said, you should never have to call dispose yourself. Far better will be to put your object in using scope:
using(Graphics g)
{
// do something with the resource
}
Now when you leave this using scope, the object will be destroyed and dispose will be called automatically for you. You should put all objects that have the dispose method defined inside a using scope.
In noob speak, Dispose() is about cleaning up after you're done using an unmanaged resource.
What's an unmanaged resource? It's all the stuff that CLR doesn't manage for you. They're thing like file handles, database connections, network sockets, GDI+ pens, etc. You get access to those things through a typical .NET object, but it will implement IDisposable, to allow you to clean up properly.
Why clean up? Until you've cleaned up after yourself, that resource isn't available by other parts of the program for use. In that respect, you're breaking things, because you're hogging a resource.
Why do this yourself? You should do this yourself as soon as you stop needing the resource, rather than rely on the auto-magic of the garbage collector, because it could take a long (well, unspecified) amount of time before the garbage collector gets to it. Until an object has been disposed of properly, you can't reuse the underlying resource, so you program will not function reliably.
Mitch Wheat says - Always call Dispose() on any object that implements IDisposable. GDI handles used by graphics objects are unmanaged and require disposing when you are finished with them.

Do I need to dispose of a resource which is not actually used?

i have a stupid question, but i want to hear the community here.
So here is my code:
using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
{
return true;
}
My question, is it any different than:
(FtpWebResponse)request.GetResponse();
return true;
Which one is better in general? which one in terms of GC and why?
The first is better, but not in terms of GC. WebResponse implements IDisposable because it has resources that need to be released such as network connections, memory streams, etc. IDisposable is in place because you, as the consumer, know when you are done with the object and you call Dispose to notify the class you're done with it, and it is free to clean up after itself.
The using pattern really just calls Dispose under the hood. E.g. your first example is actually compiled to be this:
FtpWebResponse response = (FtpWebResponse)request.GetResponse()
try
{
}
finally
{
((IDisposable)response).Dispose();
}
return true;
This is separate from the garbage collector. A class could also clean up resources in the finalizer, which gets called from GC, but you should not rely on that to happen for two reasons:
It could be a very long time before GC finally comes around, which means those resources are sitting there being consumed for no good reason.
It might not actually clean up resources in the finalizer anyway
The first one is better.
The difference is that the using statement will call dispose on the object wrapped in it. It will correctly dispose of held resources.
From MSDN, the using statement:
Provides a convenient syntax that ensures the correct use of IDisposable objects.
I appreciate that you are not interested in the response. However, the response object has been created, initialised and returned to your code whether you wanted it or not. If you don't dispose it, then it will hang around consuming resources until the GC eventually gets round to finalizing it.
I agree with what others said, but if you have such hate for the using() syntax, you could do:
((IDisposable)request.GetResponse()).Dispose();
return true;
The GC doesn't care about anything other than memory, so if you've got plenty of memory and are not consuming much of it, it might be a very long time until it does a GC. During that entire time the unmanaged resources, such as network connections, file handles, database connections, graphics handles etc., remain tied up by the objects that are waiting to be GC'd. This can cause you to run out of these resources and the GC will be oblivious because it doesn't monitor unmanaged resources.
So if you put your code in a loop and kept calling it, without calling Dispose, you might find it quickly degrades in performance(processes fighting for scarce resources) or gives you an exception due to a lack of the unmanaged resources. It depends on the context of how it is being called and how you are creating other related objects. Rather than analyze each situation, it is always safest to call Dispose as soon as you are done with the instance.
It is true that eventually Dispose will be called by the GC on an object that has gone out of scope/is no longer referenced, but this is indeterministic. Being indeterministic, you could see different results in how soon resources are free'd when testing vs production and lead to random Exceptions for failure to allocate resources when they run out. If you could choose between consistent deterministic behavior of your application and inderterministic, you'd probably want deterministic, unless maybe you are trying to confuse your Testers.

Categories

Resources