memory problems

Posted on 2011-10-18
Last Modified: 2012-05-12
I have a main window in my project, and numerous other child widows inside the main.
 When I open the one child  main window occupies some memory, i can see it in task manager.
 When I close the  child window the occupied memory is not released.
I used:
Private Sub MainForm_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
end sub

But this is not works.
Please can anyone give me some idea how to resolve this.
Question by:mastiSoft
    LVL 8

    Expert Comment

    by:Pramod Bugudai
    Hi. Instead of using Me.Dispose, why don't you use Me.Close(). This is a best way to do over Dispose().
    LVL 83

    Expert Comment

    I would declare an event in a common place such as a module

    Private Sub Form_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs)
         Sender = Nothing
    End Sub

    Then wherever I declare a form,

    Dim f as new someform
    AddHandler f.FormClosed, AddressOf Form_FormClosed

    LVL 1

    Author Comment

    if I use Me.Close then I get:An unhandled exception of type 'System.StackOverflowException' occurred in System.Windows.Forms.dll.
    this is MDI application and I need to clean memory when child form close.

    to load a new form I use
                        AktivForm = New MainForm
                        'New Form Creater & Show
                        AktivForm.MdiParent = Me

                        'AktivForm.Tag = m_ChildFormNumber
                        AktivForm.Text = My.Computer.FileSystem.GetName(OpenFileName)

    This is from MDIParent.
    Then when a child form close  then i use  Private Sub MainForm_FormClosed(...
    Where I have to use your suggestion mr.CodeCruiser

    LVL 83

    Expert Comment

    >Where I have to use your suggestion mr.CodeCruiser

    >Then when a child form close  then i use  Private Sub MainForm_FormClosed(...

    Why would you call Close on a form which is already closing???

    GC.Collect removes objects that have no more references left. When you call it from within the form, the form is still being referenced. Call GC.Collect from the calling location which is MDI form. As I have shown, use an event handler on the mdi form which handles the closing event of ALL child forms. Then call GC.Collect in that event handler.
    LVL 1

    Author Comment

    I add the module as you suggested.
    and line AddHandler f.FormClosed, AddressOf Form_FormClosed before show child form.
    Nothing change,
    Why I need this:
    Before opening the child form  Task Manager show that the application use let say 6000 k
    When child form opens this value increase to  9000 k. If I close this form then it still 9000k.
    Opening several files(forms) this amount just be bigger and at the some moment application just hangs.
    LVL 83

    Expert Comment

    >Why I need this

    To remove the reference of the form.

    Show us the relevant complete code that you have now.
    LVL 2

    Accepted Solution

    Microsoft .NET uses Garbage Collector to release unused memory. Usually there is no need for developers to explicitly release memory. .NET Framework calls GC.Collect() on specific times to release memory.
    When something like your situation happens, it shows that you have a memory leak.
    Perhaps you keep a reference to your forms after they close.
    For example if your child form subscribes to an event of your parent form, your parent form has a hidden reference to the child form. So when you close the child form, the occupied memory will no release.

    There are some tools that help you find memory leaks.
    Sci Tech .NET Memory Profiler ( is a very good one that I used before. You can use it to find your memory leak. Feel free to ask me your questions about that.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    This article will show, step by step, how to integrate R code into a R Sweave document
    If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
    This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now