为什么我的简单的 litgren 客户端使用这么多内存?
我有一个使用 litgren 的简单客户端和服务器。客户端不断向服务器发送请求(由值为 1 的一个字节组成的消息),并且客户端以“数据”(2000 个 ushort)进行响应。它每秒执行此操作大约 20 次。我将传入的数据存储在列表中,每次收到数据时都会清除该列表。问题是,我在任务管理器中观察 client.exe 的内存使用情况,它一直在上升。十分钟内就增加到了大约 100mb。我偶尔可以诱使垃圾收集器运行,但它似乎只能减少一点内存使用量。
这是我的服务器的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Lidgren.Network;
using System.Threading;
namespace 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);
}
}
}
}
}
这是我的客户端的代码:
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);
}
}
}
}
}
我尝试使用 .NET Memory Profiler,并且尽我所能确定 libgren 是否在某处保存了我的传入数据,尽管我不太明白内部结构。我最好的猜测是,太多数据发送得太快,并且在客户端的某个地方排队。会发生这种情况吗?服务器内存使用稳定。
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;
namespace 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我最终在该项目的网站上得到了 Lidgren 的回复(我最初应该在那里发布问题,但我对 Google 代码上的各种 Lidgren 页面感到困惑,并认为该项目已经死了)。这在lidgren中被认为是一个问题,并且今天提供了修复http://code.google.com/p/lidgren-network-gen3/issues/detail?id=65#makechanges。
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 .