Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Closing and Showing forms while keeping memory low

Status
Not open for further replies.

Govnor

Technical User
Sep 3, 2002
55
GB
Hi,

I am trying to write an application where I have multiple forms. I am trying to keep the memory usage low as well.

So I thought I would open a form when requested, then when I close the form it will release the memory but it does not. (That’s the first problem)

Secondly, when I try to open the form that I just closed it errors on me.

The error message is:
An unhandled exception of type 'System.ObjectDisposedException' occurred in system.windows.forms.dll

Additional information: Cannot access a disposed object named "Form2".

I am using a module to open my forms. The code is below.
The sub showfrm2 opens my form fine the first time but when I close it then try to open it again it errors on me.

Module SubMain
Dim frm1 As New Form1
Dim frm2 As New Form2

Sub Main()
Frm1.ShowDialog()
End Sub

Sub showfrm2()
Frm2.Show() - error occurs here
End Sub

End Module

Any ideas???

Thanks
Manraj
 
It sounds like you're not closing your form, you're disposing it. If you want to open it again, you need to create a new instance of it.

Code:
Dim Frm2 As Form2

Sub showfrm()
  Frm2 = New Form2
  Frm2.Show()
End Sub

The reason that memory is not being cleared is because that will not happen until garbage collection occurs.
 
Great that works! Thanks for that. And now when I open and close a form I can see the memory go up and down respectively

Have you any idea on this part regarding memory usage…… As I have one Form that only has a NotifyIcon on it and all it does is have a contextmenu that opens new forms but uses 27,000k apx. Even word only uses 18,000k. Why does such a simply program require so much memory?

Thanks
Manraj
 
There's a lot of overhead associated with the .NET runtime. I believe that the number is somewhat deceiving, though. If you run multiple applications most of that memory will only be used once.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top