Hi there,
I’m currently using jobs, burst and native containers quite extensively. I recently discovered that the code I’ve been writting causes Unity to consume 16 Gb of memory in about 15-20 minutes.
Upon further inspection, I isolated the cause to be NativeQueue. Searched the forums, and found this:
Turns out that when you call Enqueue(), some memory is allocated to never be released. Over time this leak is a real problem. You can reproduce it using this code, it will leak at a rate of 1 Gb every ten seconds, approximately:
using Unity.Collections;
using Unity.Mathematics;
using UnityEngine;
public class QueueLeak : MonoBehaviour
{
private struct Data
{
public float4x4 data0;
public float4x4 data1;
public float4x4 data2;
public float4x4 data3;
public float4x4 data4;
public float4x4 data5;
public float4x4 data6;
public float4x4 data7;
public float4x4 data8;
public float4x4 data9;
public float4x4 data10;
public float4x4 data11;
public float4x4 data12;
public float4x4 data13;
public float4x4 data14;
public float4x4 data15;
public float4x4 data16;
public float4x4 data17;
public float4x4 data18;
public float4x4 data19;
}
private NativeQueue<Data> queue;
private void OnEnable()
{
//Allocate
queue = new NativeQueue<Data>(Allocator.Persistent);
}
private void OnDisable()
{
//Deallocate
queue.Dispose();
}
private void Update()
{
for (int i = 0; i < 10000; i++)
{
queue.Enqueue(default);
queue.Dequeue();
}
}
}
Can anyone confirm it’s not an obvious misuse of the queue, so that I can file a bug report?