Leak in BatchRendererGroup.InvalidateStaleBatches

In our project, units fight in a small arena. Units are often destroyed and instantiated again.

We noticed that the running time of

linearly increases from 0.05 ms to almost 7 ms over 30 minutes of play, this causes a significant drop of FPS. Reloading the scene along with the destruction of all entities, including prefabs from which units are instantiated, does not reduce the running time of InvalidateStaleBatches. Only restarting the game helps (play mode exit / play mode enter).

Using Unity 2022.3.15f, Entities/Entities graphics 1.2.0-pre.4, URP 14.0.9.
Also tested: Unity 2022.3.19f, Entities/Entities graphics 1.2.0-pre.6, URP 14.0.10. The problem persists.

This is what the profiler looks like after reloading the scene after 30 minutes of play:

During the analysis, it was discovered that there was no call to m_ThreadedBatchContext.RemoveBatch in EntitiesGraphicsSystem, which, logically, should be a pair to m_ThreadedBatchContext.AddBatch.

We added this line into Unity.Entities.Graphics/EntitiesGraphicsSystem.cs at the end of the method RemoveBatch:

private void RemoveBatch(int batchIndex)
{
    /// existing code

    m_ThreadedBatchContext.RemoveBatch(new BatchID { value = (uint) batchIndex });
}

This solves the problem: the running time of InvalidateStaleBatches no longer goes above 0.02 ms. However, no new problems were discovered.

3 Likes

Thank you for the analysis and the suggested fix! This certainly looks like a bug slipped in.