Memory Leaks?

D

David Schwartz

Has anyone noticed memory leaks in their VB.NET Windows Forms apps? My app
definitely has some memory leaks, so I did a simple test to see if it was my
app or something about VB.NET.

I created a test app with 2 forms. Form1 has a command button with the
following code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim x As New Form2
x.Show()
End Sub

Watching the memory usage, I see that the used memory goes up between 10-20K
when I click the button to show Form2. When I close Form2, the memory is
not reclaimed.

Is there something I need to do to reclaim memory when closing a form?

Thanks!
 
F

Fredrik Melin

Form.Dispose

Then it takes some time before GC cleans up

Regards
Fredrik Melin
 
C

CJ Taylor

On top of that, Microsoft Task Manager doesn't really deal well with a
managed environment, in fact, it kinda sucks in general. When .NET releases
memory Task manager doesn't really catch it sometimes, or whats reported as
being released and really is sometimes varies.

With those other links given to you you will also see tht GC invokes about
once every 47 seconds... How this number came to be, I don't know, but
almost every GC test I see always results to about that number.

cooky..

You can always get your memory usage by using

GC.GetTotalMemory(False)

I use .NET Memory Profiler by scitech, great tool...
 
H

Herfried K. Wagner [MVP]

* "David Schwartz said:
Yes, I read the article, and basically it says that .NET handles all memory
management for me. The problem is that my little test program proves
otherwise - memory is not reclaimed when I close a form.

That's by design. The GC will release the memory if the system needs
memory. Don't worry about that!
 
D

David Schwartz

Yes, I read the article, and basically it says that .NET handles all memory
management for me. The problem is that my little test program proves
otherwise - memory is not reclaimed when I close a form.
 
D

David Schwartz

I see the same results by using System.GC.GetTotalMemory... Memory is not
completely reclaimed.
 
D

David Schwartz

Ah, now that makes sense! I just tested this out and confirmed that the
memory is indeed released as the system needs it (when I open up many more
applications).

Thanks!
 
A

Arcer P

Someone posted a solution in another group:

System.GC.WaitForPendingFinalizers()
System.GC.Collect()

This problem becames really bad when you deal with graphics.
 
C

Cor Ligthert

Hi CJ,

Some people/organizations like it when a program does a lot of processes
that do nothing.

Than they can say that the process they have are very heavy.
I think we can give this answer to someone who needs that the next time.

Function multiply(byval a as integer, byval b as integer) byval as integer
dim c = 0
if b > a then ' to be sure the longest way is taken
For i as integer = 1 to b
c = c + a
System.GC.WaitForPendingFinalizers()
System.GC.Collect()
next
else
For i as integer = 1 to a
c = c + b
System.GC.WaitForPendingFinalizers()
System.GC.Collect()
next
(I know that there should be a -1 to b also in)

However this is very good programming in a special class, I see it before me
never more use
c = a*b however only
c = multiply(a,b)

And no memory leaks of course for this calculation

Before someone thinks it, this is not a serious solution.
Cor.
 

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