Situation - server having memory growth issues
cut out all the code except this bit, where a client is constantly
sending message and this just receives them, does nothing, and then
receives again.
I had stuck in a GC.Collect() and GC.WaitForFinalizers() call in the
code so I could more easily watch the gen2 heap size and see if it was
steady or not. But if I put it in one place, it would be steady, in
another it would grow!!?? (see comments in code below)
Whats up with that, and am I doing anything wrong with how I am using
async sockets here?
public void OnDataReceived(IAsyncResult asyn)
{
MsgState msgState=null;
try
{
msgState = (MsgState)asyn.AsyncState ;
int bytesReceived = 0 ;
bytesReceived = msgState.socket.EndReceive (asyn);
msgState.pos += bytesReceived;
//A result of 0 implies that the socket has died
if (bytesReceived == 0)
{
SocketClosed(msgState.socket);
return;
}
//comment this out to narrow down the problem
//msgState = processMessage(msgState);
msgState.reset(); //reset
position so the buffer won't grow
//if I put a GC.Collect() here, gen2 heap grows constantly
msgState.socket.BeginReceive
(msgState.dataBuffer,msgState.pos,msgState.expecte dSize-msgState.pos,SocketFlags.None,msgState.callBack,ms gState);
// but if I put the GC.Collect() here, the gen2 heap remains constant
as expected...
}