469,358 Members | 1,556 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,358 developers. It's quick & easy.

Memory oddity

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.

Jul 21 '05 #1
2 2200
The even stranger part is that, I just happened to look at perfmon and see that it appears to have eventually leveled off after about 14 minutes. The memory appears to have gone from 6654720 to approx 7772000. That is a growth of 117280 bytes. Not allot, but I still don't understand why the memory is growing. Can this all be explained away by GC? Seems a bit odd since I am not allocating anything in my app on a cyclic basis here.
"Ray Cassick (Home)" <rc************@enterprocity.com> wrote in message news:%2****************@TK2MSFTNGP12.phx.gbl...
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.

Jul 21 '05 #2
Actually, you *are* allocating strings in your threads. I don't know if that
is the cost but every loop iteration in each thread should result in two
string allocations. If you change to release mode or somply remove the
Debug.Write lines, I suspect you'll see your memory working properly again.

Also, as a suggestion, don't provide sample code using windows forms unless
the issue is *with* windows forms themselves. To test this one would have to
load visual studio and lay out a form or rewrite it to be a console
app(potentially inadvertainly fixing hte problem as well, ;))...had it been
a console app it'd be simple cut 'n paste.

Unfortunatly I cannot get this to repro with 2005 and I don't have 2003 on
hand.

Also, one point, Thread.Sleep is a static method, Your
System.Threading.Thread.CurrentThread.Sleep() call is misguided as sleep
always affects the current thread. Simply System.Threading.Thread.Sleep() is
sufficent.

"Ray Cassick (Home)" <rc************@enterprocity.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
The even stranger part is that, I just happened to look at perfmon and see
that it appears to have eventually leveled off after about 14 minutes. The
memory appears to have gone from 6654720 to approx 7772000. That is a growth
of 117280 bytes. Not allot, but I still don't understand why the memory is
growing. Can this all be explained away by GC? Seems a bit odd since I am
not allocating anything in my app on a cyclic basis here.

"Ray Cassick (Home)" <rc************@enterprocity.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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.
Jul 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by Michael Foord | last post: by
22 posts views Thread by xixi | last post: by
2 posts views Thread by Ray Cassick \(Home\) | last post: by
5 posts views Thread by jmdocherty | last post: by
43 posts views Thread by michael.f.ellis | last post: by
6 posts views Thread by Rex the Strange | last post: by
1 post views Thread by Jean-Paul Calderone | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.