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

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 nb = new NativeArray(bn, Allocator.Temp);
senddata.WriteInt(bn.Length);
senddata.WriteBytes(nb);
m_serverDrive.EndSend(senddata);
nb.Dispose();
}
bn = null;
}

  1. Use code blocks for posting code.
  2. What object is using memory? Did you check the memory profiler?

Yes I checked in profiler ,The execution time of this function fills the memory

But did you check the memory profiler?

Yes but I just saw the RAM fill graph going up when using the function

I mean the actual memory profiler. Not the memory graph in the regular profiler.
https://docs.unity3d.com/Packages/com.unity.memoryprofiler@1.0/manual/index.html

Make a snap shot when ram usage is high

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?

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:

using Unity.Collections.LowLevel.Unsafe;

...

unsafe
{
    fixed (byte* ptr = bn)
    {
        var nb = NativeArrayUnsafeUtility.ConvertExistingDataToNativeArray<byte>(ptr, bn.Length, Allocator.None);
        senddata.WriteBytes(nb);
    }
}

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

I would presume the same as for the other one?

This is my memory capture, can anyone check it and figure out the problem?

https://drive.google.com/file/d/1UkNvJM50SUf_vIAlVxPQhEFzsiAFB1nP/view?usp=drivesdk

As pointed out over here , the leak might have something to do with the repeated adding of Server components