[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 276
  • Last Modified:

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.
0
mastiSoft
Asked:
mastiSoft
1 Solution
 
Pramod BugudaiCommented:
Hi. Instead of using Me.Dispose, why don't you use Me.Close(). This is a best way to do over Dispose().
0
 
CodeCruiserCommented:
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

0
 
mastiSoftAuthor Commented:
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


               
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
CodeCruiserCommented:
>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.
0
 
mastiSoftAuthor Commented:
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.
0
 
CodeCruiserCommented:
>Why I need this

To remove the reference of the form.

Show us the relevant complete code that you have now.
0
 
MDUnicornCommented:
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 (http://memprofiler.com) 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.
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now