473,287 Members | 1,904 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Handle leak in threading?

I was wondering if someone could help me understand why it seems that
creating threads using the code below causes handles to seemingly leak.

System.Threading.Thread threadRunAsync = new
System.Threading.Thread(RunFunc);
threadRunAsync.Start(listParams);

I have to use ThreadPools in order to avoid the handle leaks.

System.Threading.ThreadPool.QueueUserWorkItem(new
System.Threading.WaitCallback(RunFunc), listParams);

All I can figure is that it was done to discourage people from spawning
short lived threads and to encourage the use of thread pools. Any thoughts
that might help me understand this would be helpful. Sometimes I have items
that I'd like to run in a thread because they run for a long time and I
don't want the thread pool to be used up but I also don't want to have to
reboot my application every month either just to clear up the handles from
these threads. I thought about reverting to a whole new process for such
things but that's more painful.

Thanks

Jayme
Dec 29 '05 #1
3 7899

"Jayme Pechan" <ja**********@whitefeld.com> wrote in message
news:u%***************@TK2MSFTNGP09.phx.gbl...
I was wondering if someone could help me understand why it seems that
creating threads using the code below causes handles to seemingly leak.

System.Threading.Thread threadRunAsync = new
System.Threading.Thread(RunFunc);
threadRunAsync.Start(listParams);

I have to use ThreadPools in order to avoid the handle leaks.

System.Threading.ThreadPool.QueueUserWorkItem(new
System.Threading.WaitCallback(RunFunc), listParams);

All I can figure is that it was done to discourage people from spawning
short lived threads and to encourage the use of thread pools. Any
thoughts that might help me understand this would be helpful. Sometimes I
have items that I'd like to run in a thread because they run for a long
time and I don't want the thread pool to be used up but I also don't want
to have to reboot my application every month either just to clear up the
handles from these threads. I thought about reverting to a whole new
process for such things but that's more painful.


You are right not to use the ThreadPool for long-lived background tasks.

Spawing threads should not leak handles. Can you post a repro?

David
Dec 29 '05 #2
My bad. It looks like it eventually clears the handles. I had it get up to
16k handles with a very simple program before it cleared them. I guess I
just have to be more patient with this garbage collector thing. It sure is
difficult to tell when you have memory problems in C# programs. I use a lot
of controls that are written in C++ so I have to pay attention to this. I
guess my unit testing will have to be the final word on the handles and
memory.

Thanks for the replay.

Jayme

Dec 30 '05 #3


"Jayme Pechan" <ja**********@whitefeld.com> wrote in message
news:u%***************@TK2MSFTNGP09.phx.gbl...
I was wondering if someone could help me understand why it seems that
creating threads using the code below causes handles to seemingly leak.

System.Threading.Thread threadRunAsync = new
System.Threading.Thread(RunFunc);
threadRunAsync.Start(listParams);

I have to use ThreadPools in order to avoid the handle leaks.

System.Threading.ThreadPool.QueueUserWorkItem(new
System.Threading.WaitCallback(RunFunc), listParams);

All I can figure is that it was done to discourage people from spawning
short lived threads and to encourage the use of thread pools. Any
thoughts that might help me understand this would be helpful. Sometimes I
have items that I'd like to run in a thread because they run for a long
time and I don't want the thread pool to be used up but I also don't want
to have to reboot my application every month either just to clear up the
handles from these threads. I thought about reverting to a whole new
process for such things but that's more painful.

Thanks

Jayme


Handles are unmanaged resources, it's your responsability to "dispose" them
of when you are done with them. If you don't, you may exhaust the handle
object pool before the GC comes along.

Willy.
Dec 30 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Craig Ringer | last post by:
Hi folks I'm a bit of a newbie here, though I've tried to appropriately research this issue before posting. I've found a lot of questions, a few answers that don't really answer quite what I'm...
4
by: Bob Smith | last post by:
Does the Win32 port of Python have a memory leak? I have some code that runs flawlessly on Linux, but bombs after a few hours on Windows. It's threaded and uses a lot of memory. Thanks!
0
by: Kong Li | last post by:
Follow up to this thread, the latest Oracle 9i release 2 patchset (9.2.0.5) fixed the handle count leak problem. The problem is in Oracle client component. Thanks. Kong ----- From: Kong...
0
by: dudi | last post by:
I am encountering the following strange problem which causes the application to use more memory then it should. I have 5 database tables. lets pretend I want to load each one of them into a...
12
by: Ken Brubaker | last post by:
I am using DB2 8.5 FP5 on Windows 2000 Server and have noticed what appears to be a handle leak. When I execute a DB2 connect statement in a command window, followed by a DB2 connect reset...
10
by: Manfred Braun | last post by:
Hi All, I am writing a proggi, which should monitor some processes. While doing this, I needed a test-program and wrote one, which does nothing else than to consume some cpu, sometimes more,...
1
by: =?Utf-8?B?QU1lcmNlcg==?= | last post by:
I have a vb.net program that leaks handles as reported by task manager. After running for a couple of hours, task manager reports over 1000 handles, and it continues to grow. I think I am failing...
3
by: Godzilla | last post by:
Hello, I have a program that create and pop an object off a queue, but it is experiencing some memory leakage. I have been unable to detect where the memory leakage occur. The strange thing is...
2
by: PimpDaddy | last post by:
Here is the source code of my C++/ME app. When I run it I can use "Windows Task Manager" to see that thread HANDLEs are leaking. #include "stdafx.h" #include <omp.h> public __gc class...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...

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.