0

We have a socket server developed in C#.Net 3.5. I see server memory usage increasing whenever a client disconnectes and connects. The server disconnects the client if valid credentials were not provided.

When the client attempts to connect with invalid credentials more memory is reserved and seemingly never relinquished. The Client is trying to reconnect once every second. Below is the code handling the disconnect. This code is being called whenever a client disconnects and on encountering an error on read and write operations.

Could somebody guide me on how to debug / fix this issue?

dabididabidi
  • 41
  • 1
  • 6
  • Are you using one thread per client? If you call displose/close on the socket and stream, you should be fine? Otherwise I would advise you to look at using a memory profiler. – weismat Apr 30 '12 at 09:27

2 Answers2

0

It could be that you are setting state.workSocket and state to null, without them getting a chance to dispose.

Shiraz Bhaiji
  • 64,065
  • 34
  • 143
  • 252
  • Yes, by setting it to null it never gets disposed – Shiraz Bhaiji Apr 27 '12 at 19:02
  • Our socket server,which is windows service, has custom performance counters , which are intialized on the socket service startup. Once service is started ,it accepts client connections and use custom performance counters to store total num of clients connected/disconnected, total byts in ,total bytes out etc.My guess is when sockets are closed , somehow still the closed socket is refernced in performance counters, which is causing to keep on increasing memory. I tried running sockets services with out performance counters and i see memory increase and also it comes down after sometime. – dabididabidi May 01 '12 at 00:58
0

How are you measuring memory usage?

The GC will not perform a collection as often if there is no memory pressure on the machine. So the memory usage shown in Task Manager can rise even when memory has been disposed.

You could try adding some GC.Collect calls to force the GC to run - this will show if you are actually leaking memory. ( Don't leave the Collect calls in your production build ).

MSDN article: Memory Usage Auditing For .NET Applications

Nick Butler
  • 24,045
  • 4
  • 49
  • 70