开发者

Why does my simple lidgren client use so much memory?

开发者 https://www.devze.com 2023-04-02 09:23 出处:网络
I have a simple client and server that uses lidgren. The client continually sends requests to the server (messages consisting of one byte with value 1), and the client responds with \"data\" (2000 ush

I have a simple client and server that uses lidgren. The client continually sends requests to the server (messages consisting of one byte with value 1), and the client responds with "data" (2000 ushorts). It's doing this approximately 20 times a second. I store the incoming data in a List, which I'm clearing each time data is received. The problem is, I'm watching the memory usage for client.exe in task manager and it's just going up and up. It went up to about 100mb in ten minutes. I can occasionally entice the garbage collector to run but it seems to only shave a little off of the memory usage.

Here is the code for my server:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Lidgren.Network;
using System.Threading;

namesp开发者_如何转开发ace Client
{
    class Program
    {
        static void Main(string[] args)
        {
            NetPeerConfiguration config = new NetPeerConfiguration("testapp");
            config.UseMessageRecycling = true;
            config.EnableMessageType(NetIncomingMessageType.DiscoveryRequest);
            NetClient netClient = new NetClient(config);
            netClient.Start();
            netClient.DiscoverLocalPeers(12313);

            List<ushort> mylist = new List<ushort>();
            NetIncomingMessage msg;

            while (true)
            {
                if (Console.KeyAvailable && Console.ReadKey().Key == ConsoleKey.G)
                {
                    GC.Collect();
                }

                while ((msg = netClient.ReadMessage()) != null)
                {
                    switch (msg.MessageType)
                    {
                        case NetIncomingMessageType.DiscoveryResponse:
                            netClient.Connect(msg.SenderEndpoint);
                            Thread.Sleep(1000);

                            //request data from server
                            NetOutgoingMessage nom = netClient.CreateMessage();
                            nom.Write((byte)1);
                            netClient.SendMessage(nom, NetDeliveryMethod.ReliableUnordered);
                            break;
                        case NetIncomingMessageType.Data:
                            mylist.Clear();
                            for (ushort n = 0; n < 2000; n++) mylist.Add(msg.ReadUInt16());

                            //request more data
                            NetOutgoingMessage nom2 = netClient.CreateMessage();
                            nom2.Write((byte)1);
                            netClient.SendMessage(nom2, NetDeliveryMethod.ReliableUnordered);

                            break;
                        default:
                            break;
                    }
                    netClient.Recycle(msg);
                }

            }

        }

    }
}

and here is the code for my client:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Lidgren.Network;
using System.Threading;

namespace Server
{
    class Program
    {
        static void Main(string[] args)
        {
            NetServer server = null;

            NetPeerConfiguration config = new NetPeerConfiguration("testapp");
            config.EnableMessageType(NetIncomingMessageType.DiscoveryRequest);
            config.Port = 12313;
            server = new NetServer(config);
            server.Start();

            NetIncomingMessage msg;
            while (true)
            {
                while ((msg = server.ReadMessage()) != null)
                {
                    switch (msg.MessageType)
                    {
                        case NetIncomingMessageType.DiscoveryRequest:
                            Console.WriteLine("Discovery");
                            server.SendDiscoveryResponse(null, msg.SenderEndpoint);
                            break;
                        case NetIncomingMessageType.Data:
                            byte mtype = msg.ReadByte();
                            if (mtype == 1)
                            {
                                //user is requesting an update.
                                NetOutgoingMessage nom = server.CreateMessage();
                                for (ushort n = 0; n < 2000; n++) nom.Write(n);

                                server.SendMessage(nom, msg.SenderConnection, NetDeliveryMethod.ReliableUnordered);
                            }

                            Thread.Sleep(50);

                            break;
                        default:
                            break;
                    }

                    server.Recycle(msg);
                }
            }
        }
    }
}

I've tried using .NET Memory Profiler and as best I can ascertain libgren is holding on to my incoming data somewhere, although I don't really understand the internals. My best guess is that too much data is being sent too quickly and is being queued up somewhere on the client. Does that happen? The server memory usage is stable.


I ended up getting a response from Lidgren on the project's site (I should have just posted the problem there initially but I got confused by the various lidgren pages on Google code and thought the project was dead). This was acknowledged as a problem in lidgren and a fix was made available today http://code.google.com/p/lidgren-network-gen3/issues/detail?id=65#makechanges .

0

精彩评论

暂无评论...
验证码 换一张
取 消