Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Compact Framework / May 2005

Tip: Looking for answers? Try searching our database.

Forms (opening) and memory leaks

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Sidebp - 26 May 2005 10:04 GMT
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.
Ginny Caughey [MVP] - 26 May 2005 15:44 GMT
Which version of the Compact Framework are you using?

Ginny Caughey
.NET Compact Framework MVP

>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
[quoted text clipped - 32 lines]
>
> Many thanks.
Sidebp - 26 May 2005 15:55 GMT
Hi,

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

Thanks,

Paul
Sidebp - 26 May 2005 15:56 GMT
Hi,

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

Thanks,

Paul
Peter Foot [MVP] - 26 May 2005 16:10 GMT
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

Signature

Peter Foot
Windows Embedded MVP
http://www.inthehand.com | http://www.peterfoot.net |
http://www.opennetcf.org

> Hi,
>
[quoted text clipped - 4 lines]
>
> Paul
Sidebp - 27 May 2005 11:15 GMT
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!!
Sidebp - 27 May 2005 11:32 GMT
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
Daniel Moth - 27 May 2005 21:14 GMT
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
--
http://www.danielmoth.com/Blog/

> 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
[quoted text clipped - 16 lines]
>        Me.Dispose()
>    End Sub
Sidebp - 31 May 2005 09:00 GMT
Correct - Form2 only has the standard Windows Form generated code -
this is the only routine in the app. Will try the recommended change.

thanks

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.