Archived
This forum has been archived. Please start a new discussion on GitHub.
C# TCP memory leak occur
lauria
in Bug Reports
hi,
i tried massive connecting to C# ice server socket examples.
i found memory increasing.
i think some change need.
Ice/TcpTransceiver.cs
public void close()
{
try
{
_fd.Close(0); // TIME_WAIT
#if ICE_SOCKET_ASYNC_API
_readEventArgs.Dispose();
_writeEventArgs.Dispose();
#endif
}
catch(SocketException ex)
{
throw new Ice.SocketException(ex);
}
finally
{
_fd = null;
#if ICE_SOCKET_ASYNC_API
_readEventArgs = null;
_writeEventArgs = null;
#endif
_readCallback = null;
_writeCallback = null;
}
}
#if ICE_SOCKET_ASYNC_API
internal void ioCompleted(object sender, SocketAsyncEventArgs e)
{
try
{
switch (e.LastOperation)
{
case SocketAsyncOperation.Receive:
_readCallback(e.UserToken); // if _readCallback is null
break;
case SocketAsyncOperation.Send:
case SocketAsyncOperation.Connect:
_writeCallback(e.UserToken); // if _writeCallback is null
break;
default:
throw new ArgumentException("The last operation completed on the socket was not a receive or send");
}
}catch(Exception ex)
{
_logger.trace(_traceLevels.networkCat, ex.ToString());
}
}
#else
i tried massive connecting to C# ice server socket examples.
i found memory increasing.
i think some change need.
Ice/TcpTransceiver.cs
public void close()
{
try
{
_fd.Close(0); // TIME_WAIT
#if ICE_SOCKET_ASYNC_API
_readEventArgs.Dispose();
_writeEventArgs.Dispose();
#endif
}
catch(SocketException ex)
{
throw new Ice.SocketException(ex);
}
finally
{
_fd = null;
#if ICE_SOCKET_ASYNC_API
_readEventArgs = null;
_writeEventArgs = null;
#endif
_readCallback = null;
_writeCallback = null;
}
}
#if ICE_SOCKET_ASYNC_API
internal void ioCompleted(object sender, SocketAsyncEventArgs e)
{
try
{
switch (e.LastOperation)
{
case SocketAsyncOperation.Receive:
_readCallback(e.UserToken); // if _readCallback is null
break;
case SocketAsyncOperation.Send:
case SocketAsyncOperation.Connect:
_writeCallback(e.UserToken); // if _writeCallback is null
break;
default:
throw new ArgumentException("The last operation completed on the socket was not a receive or send");
}
}catch(Exception ex)
{
_logger.trace(_traceLevels.networkCat, ex.ToString());
}
}
#else
0
Comments
-
Ping. I think this bug is more serious than it appears at first glance.
Connection is leaked every time some connection attempt throws ConnectionRefusedException as is common with explicitly registered well-known objects on developer machines. Any application that tries to reconnect periodically to such dead endpoints will keep leaking memory continuously.
Our servers ping every failed proxy every 10 seconds. I just checked memory dump from one of our servers. In just 3 days, it accumulated 120,000 leaked connections equaling 1GB of leaked RAM. Our servers currently consume 1-2GB RAM each despite periodic restarts.0 -
Robert,
We will address this issue in the next Ice release. In the meantime, a work-around could be to avoid pinging these failed proxies so often. If this is not practical and you can't wait for the next Ice release, you could subscribe to our Priority Support and a request a patch.
Best regards,
Bernard0 -
A month ago I faced with leak problem. In my case it was many OVERLAPPEDDATA pinned objects.
Downloaded ZeroC's Ice sources and analizied them.I've found
that asyncsocket event arg is never been disposed
File:
TCPtransceiver.cs
members:
private SocketAsyncEventArgs _writeEventArgs;
private SocketAsyncEventArgs _readEventArgs;
They are properly initialized and used but On close() they are NOT disposed
Full description is here
SocketAsyncEventArgs leaks its memory unless manually disposed | Microsoft Connect
I've already sent a letter to ZeroC with proposed fix in the source code.0
