new thread causes System.OutOfMemoryException

  • Thread starter Thread starter adnan.masood
  • Start date Start date
A

adnan.masood

I have a program which create threads in a loop, but I make sure that
it doesn't create more then 10 threads

Thread addSearchThread = new Thread
(StartSearch);
addSearchThread.Start(new object[]
{ data});
GC.Collect();

but it seems like system is hanging on to those threads...even after
they finish. When system throws this exception then I have about 1GB
left in memory, so it is not using anything close to where it should
throw that exception anyway.

If I look at System.Diagnostics.Process.GetCurrentProcess
().Threads.Count then I see that number of threads is in hundreds (my
program could create thousands of threads, but never more then 10 at a
time. It doesn't go too fast either, I wait 10 seconds before I
create anothe thread. What can I do so that system releases those
threads?
 
I have a program which create threads in a loop, but I make sure that
it doesn't create more then 10 threads

Thread addSearchThread = new Thread
(StartSearch);
addSearchThread.Start(new object[]
{ data});
GC.Collect();

but it seems like system is hanging on to those threads...even after
they finish. When system throws this exception then I have about 1GB
left in memory, so it is not using anything close to where it should
throw that exception anyway.

If I look at System.Diagnostics.Process.GetCurrentProcess
().Threads.Count then I see that number of threads is in hundreds (my
program could create thousands of threads, but never more then 10 at a
time. It doesn't go too fast either, I wait 10 seconds before I
create anothe thread. What can I do so that system releases those
threads?

The code you have shown would not itself cause the issue you are seeing.
You will need to show more code, like the loop and the routine StartSearch,
for anyone to make a useful suggestion.

By the way, GC.Collect() is almost never needed. There are many previous
posts regarding this which you can find.
 
If you don't post code that produces the problem then nobody can reproduce
it and help.

By the way, why not use the ThreadPool instead?
 
Back
Top