If you want to read more about GC, please take a look on this article: How the Garbage Collector works.
Dispose GUI elements
- Dialog forms. In .NET, a dialog form is a form opened by calling the ShowDialog() method. Unlike modeless forms, the Close method is not called by the .NET Framework when the user clicks the close form button of a dialog box or sets the value of the DialogResult property. Instead the form is hidden and can be shown again without creating a new instance of the dialog box. Because a form displayed as a dialog box is not closed, you must call the Dispose() method of the form when the form is no longer needed by your application.
GDI+ resources. The GDI+ resources, like: pens, brushes and fonts, need to be disposed because they encapsulate GDI+ handles in them and GDI+ is build in C++ (unmanaged code). To release the GDI+ resources, we must call the Dispose() method for each of them.
The proper usage is to create, use and dispose of an item in the right place. For example:
void override OnPaint(PaintEventArgs e)
Pen p = new Pen(Color.Blue, 1); // create
e.Graphics.DrawLine(p, 0, 0, 20, 20); // use
p.Dispose(); // dispose
Don't use GDI+ resources like this:
g.DrawLine(new Pen(Color.Blue, 1), 0, 0, 20, 20);
The Pen object in this case is created inline and never disposed of. However, when using one of the ready-made pens from Pens or SystemPens, you must never dispose the object! This is also true for ready-made brushes.
The Graphics object, if is returned by Control.CreateGraphics() method, must be also disposed as soon as it is not needed anymore. If the Graphics object is accessed thru PaintEventArgs you don't have to call Dispose() because .NET will handle that for you. You can read more about GDI+ in this article: GDI+ Windows rendering API.
Icon.FromHandle. When using the Icon.FromHandle() method you must dispose of the resulting icon using the DestroyIcon method in the Win32 API to ensure the resources are released.
[DllImport("user32.dll", CharSet = CharSet.Auto)]
extern static bool DestroyIcon(IntPtr handle);
private void GetHicon_Example(PaintEventArgs e)
// Create a Bitmap object from an image file.
Bitmap myBitmap = new Bitmap(@"c:\FakePhoto.jpg");
// Draw myBitmap to the screen.
e.Graphics.DrawImage(myBitmap, 0, 0);
// Get an Hicon for myBitmap.
IntPtr Hicon = myBitmap.GetHicon();
// Create a new icon from the handle.
Icon newIcon = Icon.FromHandle(Hicon);
// Set the form Icon attribute to the new icon.
this.Icon = newIcon;
// Destroy the Icon, since the form creates
// its own copy of the icon.
Dispose data sources
- Database connections. Dispose any database connection you don't need anymore to ensure no resources are being held by it. Also, the Dispose method of a connection object automatically calls its Close method.
- Stream objects. This includes files and memory streams, anything inheriting from the Stream class. Calling Dispose allows the resources used by the stream to be reallocated for other purposes. Before releasing all the resources, this method disposes the stream, by writing any changes to the backing store and closing the stream.