Forms (opening) and memory leaks

S

Sidebp

I have written an app for a pocketpc in vb.net. The problem I have
is that every time i open a new form, resources get used and never
released.

I have confirmed i have set everything on the form to nothing. I have
also tried opening the form using showDialog and then calling Dispose
on the form afterwards but i still seem to lose memory.

The following shows the memory usage each time I open a form:

Memory Load Mem Free

46% 10551296
48% 10256384
48% 10129408
51% 99854234

This wouldnt be a problem but it is expected that the app will be used
for a long time and the form will be opened and closed numerous times.
The same situation occurs for any other forms I open.

So my question is:

Could someone supply me with an example of the best way to:

a) open a form
b) close it and dispose of it properly

I know it sounds like a trivial task but I cant figure out where my
resources are going!

Over a course of time I eventually get an out of memory exception so
there must still be links to the form objects and hence they're not
getting picked up by the GC.

Many thanks.
 
G

Ginny Caughey [MVP]

Which version of the Compact Framework are you using?

Ginny Caughey
..NET Compact Framework MVP
 
S

Sidebp

Hi,

I'm using the .NET compact framework with SP2 and the OpenNETCF
libraries as well.

Thanks,

Paul
 
S

Sidebp

Hi,

I'm using the .NET compact framework with SP2 and the OpenNETCF
libraries as well.

Thanks,

Paul
 
P

Peter Foot [MVP]

First try installing the latest service-pack on your device - SP3 and see if
this helps. While it's quite normal for memory not the be released
immediately when objects are finished with as the garbage collector should
only kick in under certain curcumstances (detailed in this post
http://blogs.msdn.com/scottholden/archive/2004/12/28/339733.aspx), if you
are reaching a point where free memory is exhaustedd this would indicate a
problem though.
What sort of operations are carried out on the forms, do you do a lot of
custom drawing? load data? are you using P/Invokes to work with native
resources such as graphics or io? does your app hold references to the forms
anywhere which would prevent them from being garbage collected?

Peter
 
S

Sidebp

Hi Peter,

Thanks for yout input once again and thanks for your work on the
openNetCF!

I will try installing SP3 and see what happens - my only thought is
that its such a common operation that I'm probably doing something
wrong? Cant imagine there would be a memory leak when opening/closing
forms? One thing I have noticed is that memory usage increases when i
show a hidden textbox - this happens a lot on some of my pages and the
memory never seems to be recovered.

What sort of operations are carried out on the forms, do you do a lot
of
custom drawing?

Nope - predominantly I use a streamwriter which i close and dispose of.
The only other operation on one of the forms is barcode scanning -
which led me to be suspect of the objects provided for this purpose.
However the same problem appears on all of my forms.

are you using P/Invokes to work with native
resources such as graphics or io?

I use getPowerStatusEx on each form (I have seen a native exception
occur) which I believe may be as a result of this??

does your app hold references to the forms
anywhere which would prevent them from being garbage collected?

not that i know of. Predominantly I do the following:

dim x as new Formname
x.show

then when closing...I call a routine which sets all objects to nothing
and then call

me.close

from my understanding, me.close will also call dispose (this will also
dispose of any child objects on the form?)

Thanks again - anyones input is truely valued!!
 
S

Sidebp

Ok so it doesnt get simpler than this - I tried the following code and
profiled memory usage - every time i opened the form memory usage
dropped, even after a course of time/hundreds of forms opened - the
memory was not recovered. I even did a system.gc.collect()

On FORM1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim x As New Form2
x.ShowDialog()
x.Dispose()
End Sub

On FORM2

Private Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click
Me.Dispose()
End Sub
 
D

Daniel Moth

So you are running this on SP3, Form2 has no other code apart from the
button_click and nothing else happens in the application, right? Change the
Me.Dispose to Me.Close and if you get an OOM exception, post the sample
project for us to try (also tell us what device you are running on).

For more on memory problems, check this out:
http://www.danielmoth.com/Blog/2005/01/memory-problems-faq.html

Cheers
Daniel
 
S

Sidebp

Correct - Form2 only has the standard Windows Form generated code -
this is the only routine in the app. Will try the recommended change.

thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top