Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Resolved Memory leak on HoloLens 2

Discussion in 'AR' started by yuemco, Mar 31, 2022.

  1. yuemco

    yuemco

    Joined:
    Jul 1, 2018
    Posts:
    36
    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?

    Code (CSharp):
    1. private void Socket_MessageReceived(Windows.Networking.Sockets.DatagramSocket sender,
    2. Windows.Networking.Sockets.DatagramSocketMessageReceivedEventArgs args)
    3. {
    4.     //Read the message that was received from the UDP  client.
    5.     Stream streamIn = args.GetDataStream().AsStreamForRead();
    6.     MemoryStream ms = Task.Run(() => ToMemoryStream(streamIn)).Result;
    7.     byte[] msgData = ms.ToArray();
    8.     ThreadManager.ExecuteOnMainThread(() =>
    9.     {
    10.         Task.Run(() => HandleData(msgData));
    11.      });
    12. }
    13.  
    14. private async Task<MemoryStream> ToMemoryStream(Stream input)
    15. {
    16.     try
    17.     {
    18.         using (MemoryStream ms = new MemoryStream())
    19.         {
    20.             // Read and write in blocks of 4K.
    21.             byte[] block = new byte[0x1000];
    22.             while (true)
    23.             {
    24.                 int bytesRead = input.Read(block, 0, block.Length);
    25.                 if (bytesRead == 0)
    26.                 {
    27.                     return ms;
    28.                 }
    29.                 ms.Write(block, 0, bytesRead);
    30.             }
    31.         }
    32.     }
    33.     finally { }
    34. }
    35.  
    36. private async System.Threading.Tasks.Task HandleData(byte[] data)
    37. {
    38.     using (Packet packet = new Packet(data))
    39.     {
    40.         int packetLength = packet.ReadInt();
    41.         data = packet.ReadBytes(packetLength);
    42.     }
    43.        
    44.     ThreadManager.ExecuteOnMainThread(() =>
    45.     {
    46.         using (Packet packet = new Packet(data))
    47.         {
    48.             int packetId = packet.ReadInt();
    49.             // Call appropriate method to handle the packet
    50.             _packetHandlers[packetId](packet);
    51.         }
    52.     });
    53. }  
     
  2. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    9,560
  3. yuemco

    yuemco

    Joined:
    Jul 1, 2018
    Posts:
    36
unityunity