Click here to Skip to main content
15,890,845 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi there. In my company, we have a windows service app that relies on multiple threads functionality to process many socket requests at a time, using the Socket events OnDataArrival to proccess the arriving data.

Although the system is working properly, the server administrator is complaining that the app process is using a very high number of handles. That is, when you open the Windows Task Manager and check for the Handles column, the number is around 30000 handles for the process. I've checked some sources and a reasonable number of Handles for a single process is around 3000, and a 8000 is already a very high number. I'm way too over this.

Do anyone has some good sources relating .net threads and events with the use of OS handles? I need some hints on how to low the number of hanldes for the process. I should probably re-do the app with good threading pratices (the implementation is very old), but I need a faster solution for the actual service.

Posted

You're probably not really using that many handles, but have a leak in your code somewhere where handles are not being released properly.

 
Share this answer
 
Hi,

I haven't done any mass socket stuff, however the way I understand .NET sockets you don't need explicit threads at all; a BeginConnect, BeginAccept, or BeginReceive is handled asynchronously, and the events get handled on some other thread, typically taken from the ThreadPool. So I expect you only need one (or a few?) handles per socket.

:)

 
Share this answer
 
Leonardo Muzzi wrote:
around 30000 handles for the process


Thats too much. AFAIK, the handles count includes the file handles, threads, mutex, semaphore etc. Look at the application source and ensure the resources are properly released. No resource should live longer than expected. Get some memory profilers and analyze the object allocation and GC activities.

Other way is to log the application activities and analyze the log. This will help you to understand which area it is spending more time and what resources are not getting released properly.

Leonardo Muzzi wrote:
windows service app that relies on multiple threads functionality to process many socket requests at a time,


How are you doing this? Each thread per request? If yes, consider using asynchronous methods provided in the socket class. Asynchronous methods makes it possible to write highly thread efficient applications.

Here[^] is a decent MSDN article which takes the subject in detail.

:)

 
Share this answer
 
It's most likely a leakage, I doubt you are really using all those handles. You may check for missing calls to Dispose() and similar.
Not only for sockets, since handles are used also for open files etc.

 
Share this answer
 


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900