Link to home
Start Free TrialLog in
Avatar of mastiSoft
mastiSoft

asked on

memory problems

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
     GC.Collect()
      Me.Dispose()
end sub

But this is not works.
Please can anyone give me some idea how to resolve this.
Avatar of Pramod Bugudai
Pramod Bugudai
Flag of India image

Hi. Instead of using Me.Dispose, why don't you use Me.Close(). This is a best way to do over Dispose().
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
     GC.Collect()
End Sub

Then wherever I declare a form,

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

Avatar of mastiSoft
mastiSoft

ASKER

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)

                    AktivForm.Show()
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


               
>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.
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.
>Why I need this

To remove the reference of the form.

Show us the relevant complete code that you have now.
ASKER CERTIFIED SOLUTION
Avatar of MDUnicorn
MDUnicorn

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial