Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / September 2003

Tip: Looking for answers? Try searching our database.

Garbage Collection and releasing memory

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Achim Dulog - 16 Sep 2003 11:05 GMT
Hi all,
we're having developed a .NET Windows application in C#
based on the .NET Framework 1.0. This MDI application
consists of several complex WinForms dialogs and therefor
it consumes an huge amount of memory resources. When
closing a WinForm dialog I imagined that memory occupied
by that dialog isn't completely released although
the "this.Dispose()" and "System.GC.Collect() methods are
used in the closing event method. Some MB memory are left
back. Why?
Best regards
Achim
Jay B. Harlow [MVP - Outlook] - 16 Sep 2003 15:33 GMT
Achim,
> the "this.Dispose()" and "System.GC.Collect() methods are
> used in the closing event method. Some MB memory are left
Where is the closing event? Based on "this.Dispose" it is a method of the
form.

Is the call to GC.Collect in the closing event?

What about events after the closing event, such as the Closed event?

Think carefully about what you just said: In the closing event for the form
(where I have a reference to the form, hence everything that the form
references) I call this.Dispose, then I call GC.Collect. Why do I still have
memory occupied by this form?

Remember Dispose only releases unmanaged resources, it does nothing for
managed resources. Also for modeless forms Dispose is automatically called
in the Closed event, for modal forms you are responsible for calling
Dispose.

To get this to even half work, you would need to handle the Closed event for
your form in a different object, then be certain to clear the sender object
& any event handlers to ensure that nothing (that you are keeping) has a
reference to that form. Self references & Child controls can have a
reference to the form as they are going away also...

I'm saying half work, as I am sure the Forms library is doing something that
I am not aware of.

Using the Closed event is assuming that Closed is the absolute last event
that the form raises. I really do not know if it is or not. There is a
utility on Code Project that may be able to tell you.

http://www.codeproject.com/csharp/controlinspector.asp

Also you may want to follow GC.Collect with a call to
GC.WaitForPendingFinalizers, then a second call to GC.Collect &
GC.WaitForPendingFinalizers. Just be certain that you do not call GC.Collect
too often as you can actually hurt performance rather then help.

The following two articles covers when you should & should not call
GC.Collect.

http://msdn.microsoft.com/msdnmag/issues/1100/gci/
http://msdn.microsoft.com/msdnmag/issues/1200/GCI2/

Hope this helps
Jay

> Hi all,
> we're having developed a .NET Windows application in C#
[quoted text clipped - 8 lines]
> Best regards
> Achim

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.