R
Ray Cassick \(Home\)
I have been beating myself up over trying to locate what I thought was a memory leak in MY code but based upon what I have seen I can't be sure that it IS my fault or something strange with DOTNET. Someone here please feel free to enlighten me on this one.
The code to reproduce is very simple. Done in VB.NET, One form with a button on it called cmdStart. Here is the code behind the form:
Public Class Form1
Inherits System.Windows.Forms.Form
Dim t1 As System.Threading.Thread
Dim t2 As System.Threading.Thread
Dim t3 As System.Threading.Thread
Dim t4 As System.Threading.Thread
Dim t5 As System.Threading.Thread
#Region " Windows Form Designer generated code " 'This has been clipped out for brevity here
Private Sub ThreadWorker()
Do
Debug.WriteLine("Thread Enter. [" & System.Threading.Thread.CurrentThread.Name & "]")
System.Threading.Thread.CurrentThread.Sleep(1000)
Debug.WriteLine("Thread exit [" & System.Threading.Thread.CurrentThread.Name & "]")
Loop
End Sub
Private Sub cmdStart_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdStart.Click
t1 = New System.Threading.Thread(AddressOf ThreadWorker)
t1.Name = "T1"
t1.IsBackground = True
t1.Start()
t2 = New System.Threading.Thread(AddressOf ThreadWorker)
t2.Name = "T2"
t2.IsBackground = True
t2.Start()
t3 = New System.Threading.Thread(AddressOf ThreadWorker)
t3.Name = "T3"
t3.IsBackground = True
t3.Start()
t4 = New System.Threading.Thread(AddressOf ThreadWorker)
t4.Name = "T4"
t4.IsBackground = True
t4.Start()
t5 = New System.Threading.Thread(AddressOf ThreadWorker)
t5.Name = "T5"
t5.IsBackground = True
t5.Start()
End Sub
End Class
Now, you ask what the problem is here? It appears to me that there is a memory leak afoot.At least according to perfmon.
When I monitor the privateBytes for the test app running these threads and sample every 60 seconds I get a very definite memory increase shown.
My question here is WHY?!? My thread is not creating any resources unless you count anything allocated internally by the framework to perform the debug prints. I am testing this running debug build of the app and to be honest I have not tested a release build yet so I cannot comment on what those results are.
Now, keep in mind that my app is MUCH more complicated than this, but I am seeing the same results here as I am in my app. I thought originally that I was seeing some memory issues because I was creating a clone of a few hashtables inside my threads and I thought perhaps that was causing some GC issues but now I am not so sure. In fact when I ran my app with all the internals of the threads commented out except for the debugs and sleeps as I have here I still see the apparent memory creep.
Can ANYONE explain to me what I am seeing here?
Am I going nuts?
I have to admit that I am by no means a threading master yet but this seems pretty basic to me.
The code to reproduce is very simple. Done in VB.NET, One form with a button on it called cmdStart. Here is the code behind the form:
Public Class Form1
Inherits System.Windows.Forms.Form
Dim t1 As System.Threading.Thread
Dim t2 As System.Threading.Thread
Dim t3 As System.Threading.Thread
Dim t4 As System.Threading.Thread
Dim t5 As System.Threading.Thread
#Region " Windows Form Designer generated code " 'This has been clipped out for brevity here
Private Sub ThreadWorker()
Do
Debug.WriteLine("Thread Enter. [" & System.Threading.Thread.CurrentThread.Name & "]")
System.Threading.Thread.CurrentThread.Sleep(1000)
Debug.WriteLine("Thread exit [" & System.Threading.Thread.CurrentThread.Name & "]")
Loop
End Sub
Private Sub cmdStart_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdStart.Click
t1 = New System.Threading.Thread(AddressOf ThreadWorker)
t1.Name = "T1"
t1.IsBackground = True
t1.Start()
t2 = New System.Threading.Thread(AddressOf ThreadWorker)
t2.Name = "T2"
t2.IsBackground = True
t2.Start()
t3 = New System.Threading.Thread(AddressOf ThreadWorker)
t3.Name = "T3"
t3.IsBackground = True
t3.Start()
t4 = New System.Threading.Thread(AddressOf ThreadWorker)
t4.Name = "T4"
t4.IsBackground = True
t4.Start()
t5 = New System.Threading.Thread(AddressOf ThreadWorker)
t5.Name = "T5"
t5.IsBackground = True
t5.Start()
End Sub
End Class
Now, you ask what the problem is here? It appears to me that there is a memory leak afoot.At least according to perfmon.
When I monitor the privateBytes for the test app running these threads and sample every 60 seconds I get a very definite memory increase shown.
My question here is WHY?!? My thread is not creating any resources unless you count anything allocated internally by the framework to perform the debug prints. I am testing this running debug build of the app and to be honest I have not tested a release build yet so I cannot comment on what those results are.
Now, keep in mind that my app is MUCH more complicated than this, but I am seeing the same results here as I am in my app. I thought originally that I was seeing some memory issues because I was creating a clone of a few hashtables inside my threads and I thought perhaps that was causing some GC issues but now I am not so sure. In fact when I ran my app with all the internals of the threads commented out except for the debugs and sleeps as I have here I still see the apparent memory creep.
Can ANYONE explain to me what I am seeing here?
Am I going nuts?
I have to admit that I am by no means a threading master yet but this seems pretty basic to me.