1

TCPClient の NetworkStream と protobuf-net を使用して、TCP 経由で protobuf メッセージを送受信します。受信は、独自のスレッドで実行される次のメソッドで行われます。

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);
            }
        }
    }   
}

これは問題なく動作しますが、ガベージ コレクションに問題があります。古い protobuf オブジェクトがまだメモリに保持されているようです。大きなメッセージは、しばらくすると System.OutOfMemoryExceptions につながります。

スリープする前に明示的に呼び出すとGC.Collect()、この問題が修正されます。しかし、明らかにすべてが遅くなります。これを適切に処理するにはどうすればよいですか?

4

1 に答える 1