Click here to Skip to main content
15,892,517 members
Articles / Programming Languages / C#
Tip/Trick

Always dispose a form

Rate me:
Please Sign up or sign in to vote.
4.43/5 (9 votes)
23 Nov 2009CPOL 15.8K   9   4
Some time ago, we noticed that in one of our projects the size of memory of our application is growing and it never decreased.After some checking and testing we realized that even if we have a local reference to a form, if we haven't  disposed it explicitly it will remain on the stack and never

Some time ago, we noticed that in one of our projects the size of memory of our application is growing and it never decreased.

After some checking and testing we realized that even if we have a local reference to a form, if we haven't  disposed it explicitly it will remain on the stack and never be collected.  

So now, instead of writing this:

C#
new usersDialog().ShowDialog()

We write: 

C#
using(var dlg=new usersDialog())
    dlg.ShowDialog();

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Iran (Islamic Republic of) Iran (Islamic Republic of)
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralForms Should Be Garbage Collected Fine Pin
AspDotNetDev18-Nov-09 15:42
protectorAspDotNetDev18-Nov-09 15:42 
I'm pretty sure your conclusion (that not immediately deallocating your form was causing a memory leak) is incorrect. Perhaps you have another reference to your form or something like that that is causing it to stay around. Or maybe there is some funky event stuff you have hooked up that is keeping the references alive. The garbage collector should collect the form references just fine.

I just created a test application to see if I could reproduce the memory issue you were having, and I could not. Basically, the application showed the same form a hundred times in a row, with memory going up and down, not increasing. I created two forms... the main form with just one button on it. When that button is clicked, it initiates the logic to launch the second form 100 times in a row. The second form has several hundred controls on it (buttons/labels/textboxes), so it is consuming some memory. If you care to challenge my findings, here is the code for the first form:

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace DeleteMe_TestMemoryUsageFromForms
{
	// This form is in charge of showing the other form.
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}

		private void button1_Click(object sender, EventArgs e)
		{
			Thread t = new Thread(new ThreadStart(delegate()
			{
				// Show the other form 100 times.
				for (int i = 0; i < 100; i++)
				{
					this.Invoke(new ThreadStart(delegate()
					{
						(new TestForm()).ShowDialog();
					}));
				}
			}));
			t.Start();
		}
	}
}



Here is the code for the second form:

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;


namespace DeleteMe_TestMemoryUsageFromForms
{
	// This form has several hundred controls on it.
	public partial class TestForm : Form
	{
		Thread stopMe;

		public TestForm()
		{
			InitializeComponent();
			stopMe = new Thread(new ThreadStart(delegate()
			{
				// Wait 1/10th of a second, then close.
				System.Threading.Thread.Sleep(100);
				this.Invoke(new ThreadStart(delegate()
				{
					this.Close();
				}));
			}));
			stopMe.Start();
		}
	}
}


The first form just has a button on it and the second form has several hundred controls.

Visual Studio is an excellent GUIIDE.

GeneralRe: Forms Should Be Garbage Collected Fine Pin
supercat919-Nov-09 9:13
supercat919-Nov-09 9:13 
GeneralGood point, but typo in headline. Pin
supercat918-Nov-09 6:01
supercat918-Nov-09 6:01 
GeneralRe: Good point, but typo in headline. Pin
AspDotNetDev18-Nov-09 15:18
protectorAspDotNetDev18-Nov-09 15:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.