473,796 Members | 2,628 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Threadin g.Thread threadRunAsync = new
System.Threadin g.Thread(RunFun c);
threadRunAsync. Start(listParam s);

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

System.Threadin g.ThreadPool.Qu eueUserWorkItem (new
System.Threadin g.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 7955

"Jayme Pechan" <ja**********@w hitefeld.com> wrote in message
news:u%******** *******@TK2MSFT NGP09.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.Threadin g.Thread threadRunAsync = new
System.Threadin g.Thread(RunFun c);
threadRunAsync. Start(listParam s);

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

System.Threadin g.ThreadPool.Qu eueUserWorkItem (new
System.Threadin g.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**********@w hitefeld.com> wrote in message
news:u%******** *******@TK2MSFT NGP09.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.Threadin g.Thread threadRunAsync = new
System.Threadin g.Thread(RunFun c);
threadRunAsync. Start(listParam s);

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

System.Threadin g.ThreadPool.Qu eueUserWorkItem (new
System.Threadin g.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
3181
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 looking for, but nothing that really solves or explains all this. I'll admit to being stumped, hence my question here. I'm also trying to make this post as clear and detailed as possible. Unfortunately, that means it's come out like a book. I...
4
1738
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
1386
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 Li (likong@email.com)
0
2535
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 dataTable. then, run over all the rows of the dataTable and for each cell in the dataRow concatenate a string (the value in the cell is a string to begin with). I expect the memory to go up every time I load a dataTable, even go up when I modify the...
12
4014
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 statement, The number of handles displayed in Windows Task Manager for the db2syscs process does not return to its original value, but to 1 handle higher. While this is not a serious issue most of the time, we have applications connecting every 30...
10
2289
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, sometimes less. As I monitored this process for a time, I found the handles increasing without end. I assume this is a bug, but I do not understand, where this could be located in my simple app. The core is as follows: private void OnTimer(object...
1
6765
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 to close or dispose something. I've looked at the code and can't spot the problem. The program is fairly big (10k lines). I can ask it to do something that always increases the handle count, so the problem is reproducible. What I don't know...
3
2569
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 when i replace the object creation with a plain integer/string, the leak goes away... Here's the code I used as my test: import Queue
2
2549
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 TestClass { public: void ThreadProc()
0
9673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9524
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10217
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10003
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9047
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7546
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5568
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4114
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.