Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Why does this RAM fill up and not empty? This function inside the loop may be executed hundred times

Discussion in 'Unity Transport' started by Farshadfarzan368, Aug 12, 2023.

  1. Farshadfarzan368

    Farshadfarzan368

    Joined:
    Sep 10, 2022
    Posts:
    73
    Why does this RAM fill up and not empty? This function inside the loop may be executed hundred times in one frame

    public void SendStringDataToClient(byte[] bn, NetworkConnection nc)
    {
    if (m_serverDrive.BeginSend(_unreliblePipeline, nc, out var senddata) == 0)
    {
    NativeArray<byte> nb = new NativeArray<byte>(bn, Allocator.Temp);
    senddata.WriteInt(bn.Length);
    senddata.WriteBytes(nb);
    m_serverDrive.EndSend(senddata);
    nb.Dispose();
    }
    bn = null;
    }
     
  2. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,847
    1. Use code blocks for posting code.
    2. What object is using memory?
    Did you check the memory profiler?
     
  3. Farshadfarzan368

    Farshadfarzan368

    Joined:
    Sep 10, 2022
    Posts:
    73
    Yes I checked in profiler ,The execution time of this function fills the memory
     
  4. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,847
    But did you check the memory profiler?
     
  5. Farshadfarzan368

    Farshadfarzan368

    Joined:
    Sep 10, 2022
    Posts:
    73
    Yes but I just saw the RAM fill graph going up when using the function
     
  6. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,847
  7. Skiriki

    Skiriki

    Joined:
    Aug 30, 2013
    Posts:
    68
    Are you executing any of that logic on a thread other than the main or job threads? And if so, are you terminating those threads?
     
  8. simon-lemay-unity

    simon-lemay-unity

    Unity Technologies

    Joined:
    Jul 19, 2021
    Posts:
    416
    Is the memory usage increasing frame over frame? Or is the level of memory usage relatively constant frame-to-frame?

    Temporary native allocations are all freed at the end of a frame. So it would be expected to see the memory usage increase for each invocation of that function (since it's doing a temporary allocation), but that memory should be reclaimed at the end of the frame. (Note that there's a subtlety here: we don't necessarily de-allocate that memory when reclaiming it. It's made available for future allocations, but might still be allocated to the process to make future allocations faster.)

    To avoid all the temporary allocations, you could re-use the same native array for each execution of the function (or use a native list if there's no known maximum size to your payloads). Another alternative involves constructing a native array that's basically a view inside your buffer. This requires writing unsafe code however:
    Code (CSharp):
    1. using Unity.Collections.LowLevel.Unsafe;
    2.  
    3. ...
    4.  
    5. unsafe
    6. {
    7.     fixed (byte* ptr = bn)
    8.     {
    9.         var nb = NativeArrayUnsafeUtility.ConvertExistingDataToNativeArray<byte>(ptr, bn.Length, Allocator.None);
    10.         senddata.WriteBytes(nb);
    11.     }
    12. }
     
  9. Farshadfarzan368

    Farshadfarzan368

    Joined:
    Sep 10, 2022
    Posts:
    73
    public void SendFragmentedDatatoClient(byte[] bn, NetworkConnection nc)
    {
    if (m_serverDrive.BeginSend(_fragmentedPipeline, nc, out var senddata) == 0)
    {
    NativeArray<byte> nb = new NativeArray<byte>(bn, Allocator.Temp);
    senddata.WriteInt(bn.Length);
    senddata.WriteBytes(nb);
    m_serverDrive.EndSend(senddata);
    nb.Dispose();
    bn = null;
    }
    }
    what should i do for fragmentedPipeline ??
     
  10. Skiriki

    Skiriki

    Joined:
    Aug 30, 2013
    Posts:
    68
    I would presume the same as for the other one?
     
  11. Farshadfarzan368

    Farshadfarzan368

    Joined:
    Sep 10, 2022
    Posts:
    73
  12. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,406
    As pointed out over here, the leak might have something to do with the repeated adding of Server components