Using TCPClient's NetworkStream and protobuf-net I send and receive protobuf messages via TCP. Receiving is done with the following method that runs in an own thread:
private void HandleClientComm()
{
using (NetworkStream stream = m_Stream)
{
object o;
while (true)
{
if (stream.CanRead && stream.DataAvailable)
{
o = null;
if (Serializer.NonGeneric.TryDeserializeWithLengthPrefix(stream, PrefixStyle.Base128, Utilities.CommunicationHelper.resolver, out o))
{
if (o != null)
{
//Do something with the incoming protobuf object
}
}
Thread.Sleep(1);
}
}
}
}
This works fine, but I have a problem with the garbage collection. It seems like old protobuf objects are still kept in the memory. Large message lead to System.OutOfMemoryExceptions after a while.
Explicitly calling GC.Collect()
before sleeping fixes this problem. But it obviously slows down everything. How do I handle this properly?
protobuf-net itself isn't going to keep hold of the old messages - indeed, if it was doing that, the
GC.Collect
wouldn't have helped.The first thing I can see is that the hot loop waiting on
DataAvailable
is really expensive; that could perhaps be interfering withGC
. The second thing I can see is that you can probably release the object ato
before sleeping; as a random thing to try, perhaps: