如何管理HoloLens 2上的内存泄漏?

发布于 2025-01-18 11:46:17 字数 1997 浏览 4 评论 0原文

我有一个客户服务器应用程序。 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); 
        }
    });
}   

Update for Memory Tool:
Memory Tool

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

假装不在乎 2025-01-25 11:46:17

为此,据报道是Unity更新版本和流类。这是显示这样一个建议的帖子:

​接收代码的消息不使用流类,如下示例中消除了内存泄漏,在Unity 2018.2.18f1中测试“

代码(CSHARP):

using (var reader = args.GetDataReader())
{
            var buf = new byte[reader.UnconsumedBufferLength];
            reader.ReadBytes(buf);
            string message = Encoding.UTF8.GetString(buf);
}

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):

using (var reader = args.GetDataReader())
{
            var buf = new byte[reader.UnconsumedBufferLength];
            reader.ReadBytes(buf);
            string message = Encoding.UTF8.GetString(buf);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文