Should we just ignore memory leak warnings?

I’m trying to generate a mesh with the jobs system and I keep seeing memory leak detected warnings.

I have a hashmap of nodes which will build the mesh data.

nodes = new NativeParallelHashMap<int4, Node>(size * size * height * scale * scale * scale, Allocator.Persistent);

And my job to bulid the mesh looks like this.

[BurstCompile]

public struct Refresh : IJobParallelFor
{
    [ReadOnly] public NativeParallelHashMap<int4, Node> nodes;
    [ReadOnly] public int scale;

    NativeStream.Writer verts;
    NativeStream.Writer tris;
    NativeStream.Writer uvs;

    public void Execute(int y)
    {
        verts.BeginForEachIndex(y);
        tris.BeginForEachIndex(y);
        uvs.BeginForEachIndex(y);

        for(int x = 0; x < scale; x++) {
            for (int z = 0; z < scale; z++) {
                nodes.TryGetValue(new(x,y,z,0), out Node node);
                Node.Build(ref node, ref verts, ref tris, ref uvs);
            }
        }

        verts.EndForEachIndex();
        tris.EndForEachIndex();
        uvs.EndForEachIndex();
    }

    public static void Schedule(_Chunk chunk)
    {
        // declare our data
        var job = new Refresh(){ };
        job.scale = _.map.scale;
        job.nodes = _.map.nodes;
        MeshBuilder mb = new MeshBuilder(_.map.scale);
        job.verts = mb.verts.AsWriter();
        job.tris = mb.tris.AsWriter();
        job.uvs = mb.uvs.AsWriter();

        // schedule job
        var handle = job.Schedule(_.map.scale, 1);
        handle.Complete();

        // cleanup
        mb.verts.Dispose();
        mb.tris.Dispose();
        mb.uvs.Dispose();
    }
}

public struct MeshBuilder
{
    public NativeStream verts;
    public NativeStream tris;
    public NativeStream uvs;

    public MeshBuilder(int buffers)
    {
        verts = new NativeStream(buffers, Allocator.TempJob);
        tris = new NativeStream(buffers, Allocator.TempJob);
        uvs = new NativeStream(buffers, Allocator.TempJob);
    }
}

If I allocate the node hashmap as persistent and I dispose of the nativestreams after the job then there shouldn’t be any leaks right?

No. Don’t ignore leaks. There’s a leak somewhere in your project.

Some Unity packages leak sometimes when things go wrong, so make sure the leak pertains to your code first. Based on the code you shared, my suspicion would be that you are not disposing your hashmap in an OnDestroy() method.

1 Like

Hey Dreaming,

I narrowed it down to the hashmap allocation. I created a new project and added a new script.

public class test : MonoBehaviour
{
    public NativeParallelHashMap<int4, Node> nodes;

    void Awake() {
        nodes = new NativeParallelHashMap<int4, Node>(1, Allocator.Persistent);
    }
}

public struct Node
{
    public int x,y,z;
}

But I still get the errors.

Leak Detected : Persistent allocates 2 individual allocations.

I feel like that should be valid? I am going to use the hashmap throughout the life of the program so it should be persistent right?

Btw man you’ve been super helpful. Do you have a paypal or something I can tip you at?

Your test MonoBehaviour needs: void OnDestroy() => nodes.Dispose();

Short answer: no.
Long answer: See Latios-Framework-Documentation/Contributing.md at main · Dreaming381/Latios-Framework-Documentation · GitHub

Ahh, so the compiler checks everywhere. I didn’t think I would have to dispose of persistent allocations. I get it now, thx dreaming.

Unity 6 has Allocator.Domain which can be used this way.

1 Like