Out of control threads

  • Thread starter Thread starter ujjc001
  • Start date Start date
U

ujjc001

Hello group. I have a vb.net winform app that, in a sub, starts 10
new threads. The sub waits in a loop for these threads to finish
before continuing. The end of the sub thread's sub sets a boolean
which the main sub's loop checks for. So, the problem is, that after
each thread completes it's task, memory is not released and the thread
count does not decrease. The main sub loops for x number of items and
starts these 10 threads for each item. I've used gc.collect and also
Thread.CurrentThread.Abort() at the end of each sub thread's sub.
I've disposed of all objects and set them = nothing and also tried
GC.GetTotalMemory(True) but nothing seems to want to let me release
these threads.
Any help would be much appreciated.
Thanks,
Jeff
 
Instead of setting a boolean, create an array (or collection) of threads and
add each thread to it. When you are ready to wait for the threads, do the
following (VB.NET):

for each th as Thread in myThreads
th.Join
next th

This eliminates the boolean variable, which may be what is keeping the
thread in memory.

Mike Ober.
 
Hi,

I am wondering why you need multithreading in this particular situation.
Your main sub waits for threads to finish anyway! Multithreading would make
sense only if you started some background task(s) and continued on doing
something else.

Why not to just execute these thread functions (without creating threads)
one aftert another, like this:

Sub s()
Begin
ThreadFunction1()
ThreadFunction2()
........
ThreadFunctionN()
End

This way your sub doesn't have to monitor threads. It just exits after their
functions are all finished.
 
Hi,

I am wondering why you need multithreading in this particular situation.
Your main sub waits for threads to finish anyway! Multithreading would make
sense only if you started some background task(s) and continued on doing
something else.

Why not to just execute these thread functions (without creating threads)
one aftert another, like this:

Sub s()
Begin
ThreadFunction1()
ThreadFunction2()
........
ThreadFunctionN()
End

This way your sub doesn't have to monitor threads. It just exits after their
functions are all finished.






- Show quoted text -

Sometimes you may want to do 10 things in parralel (like visit 10
websites and scrape some info). This is much faster when you use 10
threads, then doing it sequentially, as the bottleneck is latency, not
CPU. I have personally used a method described above, and it works
great.

You may need to retrieve values from different databases, and then
perform some calculation using them. You can only start the
calculation once you have the 10 values.
 
Hmm, and if you needed to visit 1000 websites (or databases) would you have
created a 1000 threads?

I am sure the method works great, but I still have doubts whether or not it
can give you any gain in performance.

There's still a network cable which allows only this much information to
pass at any moment. Say, 100 kbits per second. What if you need 100 kbits
from each of the 10 websites? They'll not come all in one second because of
multithreading. They'll take exactly 10 seconds.

And during multithreading doesn't CPU pass a token between threads and run
just a portion of a thread funtion and then move on to the next thread? How
is it different from running thread functions sequentially? Besides all the
extra steps that CPU has to take because of multithreading?
 
In this case, I would use the threadpool and queue up the threads. You get
the benefits of the parallel operations but don't saturate your physical
resources.

Mike.
 
FYI, I do need 10 threads, I have 10 things to do each which take
about 20 seconds, if I do sequentially, it would take 200 seconds, if
I use 10 threads it takes about 60 seconds, it's not 1 to 1 since
there are other factors like DB writing and such...

I'll try the pool.. Thanks.
 
Forgot to mention, I had to dispose of everything in any call made by
the thread before I could (and had to seemingly) abort the thread at
the end of the procedure.
I'd crash after 99+ threads and >100 mb of ram usage. Once I properly
disposed of my objects (especially a memory stream. (oops)) it stayed
at 25 or so threads and 60 mb (which is reasonable for what I'm
doing).
Thanks again.
 
Back
Top