I
ianrae88
We are getting a memory leak with code that's using async sockets.
Private Bytes perf counter keeps increasing as well as Bytes In All
Heaps. CLRProfiler shows a lot of old asyncresult objects (180 seconds
old) in the Gen 2 heap.
It looks very similar to the thread (link is below) about how
BeginReceive causes a buffer to be pinned, creating "holes" in the .Net
memory heap. The pinned objects prevent heap compaction. And since
allocation always occurs at the top of the heap, the CLR must
continually allocate more Win32 memory.
http://groups.google.ca/group/micro...ocket+gc+pinned&rnum=1&hl=en#06e053ecb340f2f6
Strangely, the addition of a Thread.Sleep(1) seems to make the memory
leak go away.
Basically, here is the BeginReceive callback function:
void AsyncReceiveCallback(IAsyncResult result)
{
VeoAsyncIOInfo thisConnection = (VeoAsyncIOInfo)result.AsyncState;
int num_read = thisConnection.m_socket.EndReceive(result);
if (0 != num_read) {
ProcessNewData(thisConnection.m_buffer, num_read,
thisConnection.m_socket);
}
Thread.Sleep(1); //hack hack hack
thisConnection.m_socket.BeginReceive(thisConnection.m_buffer, 0,
MAXMSGSIZE, SocketFlags.None, new AsyncCallback(AsyncReceiveCallback),
thisConnection);
}
It appears as though the sleep delays BeginReceive until after a reply
is sent on the same socket. ProcessNewData() queues the data from
which another thread consumes it and sends a reply.
Are there rules about sending on a socket while a BeginReceive is
active? It works, but it leaks.
Private Bytes perf counter keeps increasing as well as Bytes In All
Heaps. CLRProfiler shows a lot of old asyncresult objects (180 seconds
old) in the Gen 2 heap.
It looks very similar to the thread (link is below) about how
BeginReceive causes a buffer to be pinned, creating "holes" in the .Net
memory heap. The pinned objects prevent heap compaction. And since
allocation always occurs at the top of the heap, the CLR must
continually allocate more Win32 memory.
http://groups.google.ca/group/micro...ocket+gc+pinned&rnum=1&hl=en#06e053ecb340f2f6
Strangely, the addition of a Thread.Sleep(1) seems to make the memory
leak go away.
Basically, here is the BeginReceive callback function:
void AsyncReceiveCallback(IAsyncResult result)
{
VeoAsyncIOInfo thisConnection = (VeoAsyncIOInfo)result.AsyncState;
int num_read = thisConnection.m_socket.EndReceive(result);
if (0 != num_read) {
ProcessNewData(thisConnection.m_buffer, num_read,
thisConnection.m_socket);
}
Thread.Sleep(1); //hack hack hack
thisConnection.m_socket.BeginReceive(thisConnection.m_buffer, 0,
MAXMSGSIZE, SocketFlags.None, new AsyncCallback(AsyncReceiveCallback),
thisConnection);
}
It appears as though the sleep delays BeginReceive until after a reply
is sent on the same socket. ProcessNewData() queues the data from
which another thread consumes it and sends a reply.
Are there rules about sending on a socket while a BeginReceive is
active? It works, but it leaks.