Memory Management in Windows Forms Application : Loading and Closing Forms

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Memory Management in Windows Forms Application : Loading and Closing Forms



This is my first time asking on a forum... An advanced apology if I'm too detailed...



I have created a program that loads some forms (like a menu for a system) and a part of the program (a form) displays the contents of a table in a database in a listview (this part is working fine). When I access that form at the start of the program, it finishes loading for about 4-5 seconds.



Here's the problem... When I access parts of the program a lot of times (e.g. going back and forth around two forms) then I access the form that displays the table, the loading gets slower...


InitializeComponent();
//I just did this to travel back and forth to two forms easier...
if (MessageBox.Show("", "", MessageBoxButtons.OKCancel, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1) == DialogResult.OK)

Form2 f = new Form2();
this.Hide();
f.ShowDialog();
this.Dispose();
//this.Close(); //I'm not exactly sure which one to use here... but whichever I use, I get same results.

//there's another one of this in the first form...
//so they loop to each other until i press cancel...



When I focused on the messagebox and pressed 'enter' for some time (dunno... about 30 seconds to 1 minute) and access the form that displays the table. it loads very slowly. (there's a progressbar to see how long I have to wait. i waited for about 1 minute for it to load when I pressed 'enter' for about 2 minutes)



Also, like what is seen on the code, I'm not sure the difference between Dispose() and Close() on a form (if there is one). From what I've read, Dispose() ...well... disposes the memory allocation of the object it is used on. I'm not sure, but i think Close() does this as well...



What should I do to improve performance?





Disposing or closing (same thing) a form in the constructor does not work. It's pointless to do so.
– Hans Passant
Jun 12 '11 at 18:40




4 Answers
4



you can use using statement for creating form ,using Statement (C# Reference)
and this


using(Form2 f = new Form2())

this.Hide();
f.ShowDialog();



But for Improving performance seperate code from UI, i meant the part of code that loads data nd keep it in seperate class



Gerald,



Close() will close the form (i.e. remove it from view). Dispose() will (as you suggest) deallocate its resources so the window can be garbage collected. So, call Dispose() if you no longer need it, call Close() if you might want to show it again later on.



One thing worth noting: ShowDialog() is a blocking call. In other words, the dialog window will be shown, but the code won't hit the next line until the dialog window is closed.



Because of this, each time you open a new window, the previous window is being kept in memory- therefore all of the data from the database displayed on the window is likely being kept in memory too. I think that this is probably the cause of your performance problems.



If you want to clean up the current window each time you open a new one, try replacing your ShowDialog() with Show(), as this opens the new window and returns immediately.



First of all, check the memory usage. The simplest way is to look in Task Manager under the Processes tab and see how the memory usage is increasing for your application. This way you can detect if there's a memory leak or not.



If it's a memory leak (the allocating memory is increasing constantly without being released), then you should check your form for memory leaks. A common mistake is to keep your form referenced by some static variable from outside the form, or some event reference, etc.


form.Show()


Application.Run(new Form())


form.Close()


Dispose()


form.ShowDialog()


Close()


form.Dispose()


Use form.Show()


form.ShowDialog()


ShowDialog()


form.Hide()


Show()






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard