472,796 Members | 1,384 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,796 software developers and data experts.

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 2330
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Michael Foord | last post by:
Here's a little oddity with 'print' being a reserved word... >>> class thing: pass >>> something = thing() >>> something.print = 3 SyntaxError: invalid syntax >>> print something.__dict__...
22
by: xixi | last post by:
hi, we are using db2 udb v8.1 for windows, i have changed the buffer pool size to accommadate better performance, say size 200000, if i have multiple connection to the same database from...
5
by: SStory | last post by:
Hi all, I really needed to get the icons associated with each file that I want to show in a listview. I used the follow modified code sniplets found on the internet. I have left in...
2
by: Ray Cassick \(Home\) | last post by:
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....
5
by: jmdocherty | last post by:
All, I've been trying to set up a CSS layout and all seems well in Firefox but in Safari it just seems to be plain weird! I hope someone can help tell me whether this is a problem with my code...
43
by: michael.f.ellis | last post by:
The following script puzzles me. It creates two nested lists that compare identically. After identical element assignments, the lists are different. In one case, a single element is replaced. In...
6
by: Rex the Strange | last post by:
Hello all, Traditionally I'm a Delphi and VB6 programmer and have recently started working with VB.NET (which, I might add, I love, but that's beside the point). Anyway, I was trying to make a...
5
by: kumarmdb2 | last post by:
Hi guys, For last few days we are getting out of private memory error. We have a development environment. We tried to figure out the problem but we believe that it might be related to the OS...
1
by: Jean-Paul Calderone | last post by:
On Tue, 22 Apr 2008 14:54:37 -0700 (PDT), yzghan@gmail.com wrote: The test doesn't demonstrate any leaks. It does demonstrate that memory usage can remain at or near peak memory usage even after...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.