Windows Develop Bookmark and Share   
 index > Windows Forms General > Memory consumption growing too fast (WinForm app)
 

Memory consumption growing too fast (WinForm app)

I am working in a new WinForm's app.
right now i have 12 forms, and 6 custom components, you know, custom textbox, custom listbox, etc.
when I check memory usage, i get 35 Mb. this app is going to have 120 forms more or less, but I can not release this system if this one are going to consume 100 or 200 Mb of memory, not all my customers have new pc with 500 mb or 1gb of memory.
my Q is: how can I reduce memory consumption or how can I only run a few winforms and then release from memory and load another.

thanks in advance for your help,
E
Edward1  Friday, February 02, 2007 5:24 PM

Hi,

in C# you have the garbage collector, once there are no application roots (local variables, static variables etc) that point to an instance of an object it is eligable for garbage collection. In your case even though you have 120 forms you are not going to have 120 forms loaded into memory at once (hopefully), once you no longer need a form and there is enough memory pressure that the garbage collector feels it has to run then it will run and reclaim memory that is no longer in use. So even though your app memory increases you do not need to worry since the GC will clean it up, with systes with more memory your footrint will get larger before the GC cleans up, for systems with less RAM the GC will run more frequently.

There are some things you need to watch out for:

1. If you are using events, and have a static object which you have subscribed to events on it, make sure you unsubscribe your object otherwise they will not get garbage collected for the entire lifetime of the application (really app domain, but you probably o not create new app domains)

2. To release underlying unmanaged resources as soon as possible, call the Dispose method on all classes the implement IDisposable. User controls all have a Dispose method, you should call this once you have finished using a Form, this will not release the memory used by the form in .Net but will release all of the underlying windows handles etc used by the OS. Also calling this will mean objects in memory will not be in memory as long as if you did not call Dispose after they become eligable for garbage collection. Basically if an object has a finalizer then it will take two GC calls to clean up the object, by calling Dispose there will be a call to GC.SupressFinalize since Dispose does the same work as the Finalizer, so now the object will be removed on the first GC call. If you are interested this is a great article on GC http://msdn.microsoft.com/msdnmag/issues/1100/gci/

Mark.

Mark Dawson  Friday, February 02, 2007 8:07 PM

You can use google to search for other answers

Custom Search

More Threads

• Control textbox, promtly
• Close form and update another?
• showing a form that lies on bakground
• How to register a user control with an event in its container?
• updations to the table binded to the list view using ado.net in c#
• Buttons on UserControl show no sign of having focus
• window forms DataGridView in MFC Dilaog
• Rich text box margin problem
• save, new, open facilities for data entered in datagridview!
• Can I create gridview subtotals through data derived from stored proc?