如何管理HoloLens 2上的内存泄漏?
我有一个客户服务器应用程序。 Hololens 2是我的客户端,并使用UDP与服务器进行通信。我的MRTK Profiler向我展示了该应用程序会增加内存,并在2-3分钟后应用程序崩溃。我将一些数据包从服务器发送到Hololens。我试图致电GC,但也行不通。我尝试使用块和线程删除,但结果没有改变。我只使用一个端口。我应该连接,获取数据包并为每个数据包处理插座吗? (这是如此糟糕的解决方案)无论如何,我知道如何修复泄漏?
private void Socket_MessageReceived(Windows.Networking.Sockets.DatagramSocket sender,
Windows.Networking.Sockets.DatagramSocketMessageReceivedEventArgs args)
{
//Read the message that was received from the UDP client.
Stream streamIn = args.GetDataStream().AsStreamForRead();
MemoryStream ms = Task.Run(() => ToMemoryStream(streamIn)).Result;
byte[] msgData = ms.ToArray();
ThreadManager.ExecuteOnMainThread(() =>
{
Task.Run(() => HandleData(msgData));
});
}
private async Task<MemoryStream> ToMemoryStream(Stream input)
{
try
{
using (MemoryStream ms = new MemoryStream())
{
// Read and write in blocks of 4K.
byte[] block = new byte[0x1000];
while (true)
{
int bytesRead = input.Read(block, 0, block.Length);
if (bytesRead == 0)
{
return ms;
}
ms.Write(block, 0, bytesRead);
}
}
}
finally { }
}
private async System.Threading.Tasks.Task HandleData(byte[] data)
{
using (Packet packet = new Packet(data))
{
int packetLength = packet.ReadInt();
data = packet.ReadBytes(packetLength);
}
ThreadManager.ExecuteOnMainThread(() =>
{
using (Packet packet = new Packet(data))
{
int packetId = packet.ReadInt();
// Call appropriate method to handle the packet
_packetHandlers[packetId](packet);
}
});
}
I have a client-server app. HoloLens 2 is my client and communicates with the server using UDP. My MRTK profiler shows me the app consumes memory incrementally and after 2-3 mins app crashes. I send some packets from the server to HoloLens. I tried to call GC but it doesn't work either. I tried to remove using blocks and threads but the result didn't change. I use only one port. Should I connect, get packets and dispose of the socket for each packet? (this is so terrible solution) Anyway, any idea how can I fix the leak?
private void Socket_MessageReceived(Windows.Networking.Sockets.DatagramSocket sender,
Windows.Networking.Sockets.DatagramSocketMessageReceivedEventArgs args)
{
//Read the message that was received from the UDP client.
Stream streamIn = args.GetDataStream().AsStreamForRead();
MemoryStream ms = Task.Run(() => ToMemoryStream(streamIn)).Result;
byte[] msgData = ms.ToArray();
ThreadManager.ExecuteOnMainThread(() =>
{
Task.Run(() => HandleData(msgData));
});
}
private async Task<MemoryStream> ToMemoryStream(Stream input)
{
try
{
using (MemoryStream ms = new MemoryStream())
{
// Read and write in blocks of 4K.
byte[] block = new byte[0x1000];
while (true)
{
int bytesRead = input.Read(block, 0, block.Length);
if (bytesRead == 0)
{
return ms;
}
ms.Write(block, 0, bytesRead);
}
}
}
finally { }
}
private async System.Threading.Tasks.Task HandleData(byte[] data)
{
using (Packet packet = new Packet(data))
{
int packetLength = packet.ReadInt();
data = packet.ReadBytes(packetLength);
}
ThreadManager.ExecuteOnMainThread(() =>
{
using (Packet packet = new Packet(data))
{
int packetId = packet.ReadInt();
// Call appropriate method to handle the packet
_packetHandlers[packetId](packet);
}
});
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为此,据报道是Unity更新版本和流类。这是显示这样一个建议的帖子:
接收代码的消息不使用流类,如下示例中消除了内存泄漏,在Unity 2018.2.18f1中测试“
代码(CSHARP):
For this one, reported to be with Unity newer versions and stream classes. Here is post that shows the suggestion:
https://forum.unity.com/threads/hololens-udp-client-with-datagramsocket-and-memory-leak.583816/
"Stream streamIn = args.GetDataStream().AsStreamForRead();
Massive memory leak in IL2CPP build. Rewriting the message receiving code to not use the Stream class as in the example below eliminates the memory leak, tested in Unity 2018.2.18f1"
Code (CSharp):