i have a multi-threaded TCP server that is written in C#. Clients are accepted by server and don’t leave the server when they are connected. After approximately 1300 active connections, my software gives System.OutOfMemmoryException error. Does this problem related to 32 bit system architecture and ram? I have 32 bit Windows 7 Professional and 4 GBs RAM. When approximately 1300 active connections exist on my server, my memmory usage is about 2.1GB and CPU usage is 30%.
Thank you.
System.OutOfMemmoryExceptionis thrown when ~1300 threads in your process is created (at 32 bit OS). And the problem is that you are creating brand new thread for each connection. That is the bad practice.You should instead use only one thread for managing the sending data, the same thread (or may be second) for sending it and only one for accepting connections.
Code example for the thread pool accepting all the clients:
Code example for one thread accepting all the clients: