We noticed one interesting issue regarding memory management in VB that we do not understand. If anyone could help us with this one, please do.
We have a simple class with one single event. We create and destroy 5000 instances of this class and before we run a test we read the process memory usage. At the end we force GC and check the memory again.What we noticed is, that we have a constant memory growing. We did the same sample in C# and we did not run into this issue. Now here is the wired point. If we omit event declaration from class the memory will be cleaned as we expected. Does any one know why, and what is the right way to finalize this class.
Here is the sample code:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim 开发者_C百科memorySize As Long
memorySize = System.Diagnostics.Process.GetCurrentProcess().WorkingSet64
Console.Out.WriteLine(System.Diagnostics.Process.GetCurrentProcess().WorkingSet64 / (1024.0 * 1024.0))
For index As Integer = 1 To 5000
Dim x As New TestClass()
x = Nothing
Me.Text = index.ToString()
Next
GC.Collect()
Console.Out.WriteLine(System.Diagnostics.Process.GetCurrentProcess().WorkingSet64 / (1024.0 * 1024.0))
Console.Out.WriteLine("delta: " + ((System.Diagnostics.Process.GetCurrentProcess().WorkingSet64 - memorySize) / (1024.0)).ToString() + " kb")
End Sub
End Class
and test class:
Imports System.ComponentModel
Imports System.ComponentModel.Design.Serialization
Imports System.Xml
Imports System.Xml.XPath
Public Class TestClass
Friend Event ForeColorChanged()
Public Sub New()
End Sub
Protected Overrides Sub Finalize()
MyBase.Finalize()
End Sub
End Class
And the printout after several runs:
22,76953125
23,828125
delta: 2016 kb
24,08984375
24,0625
delta: 228 kb
24,4375
24,2109375
delta: 24 kb
24,58984375
24,83984375
delta: 384 kb
24,84375
24,890625
delta: 48 kb
Your code doesn't compile, TestClass doesn't implement the Dispose() method.
I can guess what you are seeing though. There's a known bug in the Edit+Continue support code that the VB.NET compiler generates. It uses a WeakReference to track assigned event handlers, that WR object is leaked when you run your code without a debugger. Check if you can fix your problem by either commenting out the Event or by running the Release build of your code.
You cannot ship the Debug build of your project, it will bomb with OOM when those leaked WeakReference objects consume all memory. Only ever ship the Release build.
Ok I found an answer on this post. The guy had the same problem.
http://bytes.com/topic/visual-basic-net/answers/619203-memory-leak
精彩评论