VB.NET Memory Leaks
|
On Friday, September 5th 2008 at 01:50 PM By Sonu |
|
Hello -
I have the following code in my vb.net app and when it runs, the memory
usage at the Windows Task Manager keeps increasing for the application and
stops after a while. I'm using some bitmaps and sound in the program.
How would I stop the size thing to increase! I went to these link already
but I cannot fix this issue.
http://msdn.microsoft.com/en-us/library/system.idisposable.aspx
http://www.java2s.com/Code/VB/Development/Forceagarbagecollect.htm
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
GC.SuppressFinalize(Me)
End If
MyBase.Dispose(disposing)
End Sub
Protected Overrides Sub Finalize()
MyBase.Finalize()
Dispose()
End Sub
Any help would be greatly apprecited with a sample code.
Thanks,
Sonu |
Re: VB.NET Memory Leaks by Jason on Sunday, September 14th 2008 at 07:55 AM
|
Hello Sonu,
It looks to me that the problem here is your use of the GC.SuppressFinalize
method. Take a look at this MSDN article for it:
http://msdn.microsoft.com/en-us/library/system.gc.suppressfinalize.aspx
Basically your telling the GC to NOT dispose your object from memory. Thus
each instance of the object is is sticking around and using up more memory
when a new instance is created.
Remove that line of code from your object and see what happens.
As a side note, I would recommend not forcing a garbage collection from your
source code; the GC does a good job of cleaning up memory in it's own time.
If you really, really want to force a GC then use this:
GC.Collect();
GC.WaitForPendingFinalizers();
But please read this log entry before adding the above code to your class:
http://blogs.msdn.com/abhinaba/archive/2008/05/02/forcing-a-garbage-collection-is-not-a-good-idea.aspx
Cheers.
Jas.
> Hello -
>
> I have the following code in my vb.net app and when it runs, the
> memory usage at the Windows Task Manager keeps increasing for the
> application and stops after a while. I'm using some bitmaps and sound
> in the program.
>
> How would I stop the size thing to increase! I went to these link
> already but I cannot fix this issue.
> http://msdn.microsoft.com/en-us/library/system.idisposable.aspx
> http://www.java2s.com/Code/VB/Development/Forceagarbagecollect.htm
>
> Protected Overrides Sub Dispose(ByVal disposing As Boolean)
> If disposing AndAlso components IsNot Nothing Then
> components.Dispose()
> GC.SuppressFinalize(Me)
> End If
> MyBase.Dispose(disposing)
> End Sub
>
> Protected Overrides Sub Finalize()
> MyBase.Finalize()
> Dispose()
> End Sub
> Any help would be greatly apprecited with a sample code.
>
> Thanks,
> Sonu |
Re: VB.NET Memory Leaks by Jon on Sunday, September 14th 2008 at 09:25 AM
|
Jason Evans <testmsg80@hotmail.com> wrote:
> It looks to me that the problem here is your use of the GC.SuppressFinalize
> method. Take a look at this MSDN article for it:
>
> http://msdn.microsoft.com/en-us/library/system.gc.suppressfinalize.aspx
>
> Basically your telling the GC to NOT dispose your object from memory.
No, it's saying not to call the finalizer. That's not in any way, shape
or form the same as saying not to collect the object.
> Thus each instance of the object is is sticking around and using up
> more memory when a new instance is created.
Actually when the finalizer is suppressed the object will be collected
earlier than if the finalizer is present.
--
Jon Skeet - <skeet@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com |