Preallocate cause excessive GC Alloc

Hey,

It seems both the Burst example and my attempts to use CommandBuilder.Preallocate cause excessive GC alloc, per frame, on Unity 2020 LTS + macOS.

Unsure why this happens, unsure if Preallocate() is working as intended, without this line no GC alloc are observed and the scene render just fine.

Hi

This allocation is part of some error checking code. It will be removed in your final build, or if you disable burst safety checks.

Thx, I will check it in build and report back if there are more GC.

1 Like

Just to follow up, indeed in the build I do not see such GC alloc per frame (only a 112B GC alloc during Sorting Meshes stage)

I am curious why these 2 methods result in different GC in Editor though:

IF I call it like this:

void Update()
{
    var builder = DrawingManager.GetBuilder(true);
    builder.WireBox(postion, scale, color):
    builder.Dispose();
}

Then there is that 2.4KB GC alloc.

But IF I do it in a job:

void Update()
{
    var builder = DrawingManager.GetBuilder(true);
    var job = new DrawJob()
    {
        Builder = builder;
    }
    builder.Dispose();
}

There is no extra GC.

Was it because the job version simply doesn’t do the same error checks?

Yes, the job cannot do that error check due to technical limitations, that’s why you didn’t see the alloc.