Unity 2022.3.0, Entities 1.0.10: Leaks Detected - even in a new, mostly empty project?

Hello guys,

I upgraded my project to the new Unity LTS version and the new entities package and all of the sudden I constantly get a console message that tells me that Unity detects a varying amount of leaks/persistent indiviual allocations.

So I went to Edit->Preferences->Jobs->Leak Detection Level, set it to “Enabled With Stack Trace” and tried to figure out at which point I made a mistake by going through the callstack in the “found x leak(s) from callstack” messages. Unfortunately, I never got to any of my scripts, just a series of Unity scripts calling each other.

Next I tried to remove as many objects and subscenes as possible to narrow down what causes the problem but the leaks wouldnt disapper. Thus, I decided to create a new project instead and add stuff until the leaks show up again, which, unfortunately, happened almost immediately. Here’s what I did:

  1. Create a new project (using the 3D URP template)
  2. Import the Entities Graphics package (which auto imports a some other DOTS packages)
  3. Add a subscene to sample scene.
  4. Add a 3d Object (Cube) to the subscene

And - upon entering and leaving play mode - the leaks are back! This isnt supposed to happen, right?

A typical “found leak” message looks like this:

Found 2 leak(s) from callstack:
0x0000021ab9f131c3 (Mono JIT Code) Unity.Collections.Memory/Unmanaged/Array:Resize (void*,long,long,Unity.Collections.AllocatorManager/AllocatorHandle,long,int) (at ./Library/PackageCache/com.unity.collections@2.1.4/Unity.Collections/Memory.cs:79)
0x0000021ab9f13063 (Mono JIT Code) Unity.Collections.Memory/Unmanaged:Allocate (long,int,Unity.Collections.AllocatorManager/AllocatorHandle) (at ./Library/PackageCache/com.unity.collections@2.1.4/Unity.Collections/Memory.cs:20)
0x0000021bdd91595b (Mono JIT Code) Unity.Entities.EntityQueryImpl:Allocate () (at ./Library/PackageCache/com.unity.entities@1.0.10/Unity.Entities/Iterators/EntityQuery.cs:1968)
0x0000021bdd9157fb (Mono JIT Code) Unity.Entities.EntityQuery:Construct (Unity.Entities.EntityQueryData*,Unity.Entities.EntityDataAccess*) (at ./Library/PackageCache/com.unity.entities@1.0.10/Unity.Entities/Iterators/EntityQuery.cs:2148)
0x0000021bdd911823 (Mono JIT Code) Unity.Entities.EntityQueryManager:CreateEntityQuery (Unity.Entities.EntityDataAccess*,Unity.Entities.ArchetypeQuery*,int,Unity.Entities.ComponentType*,int) (at ./Library/PackageCache/com.unity.entities@1.0.10/Unity.Entities/Iterators/EntityQueryManager.cs:720)
0x0000021bdd90ab53 (Mono JIT Code) Unity.Entities.EntityQueryManager:CreateEntityQuery (Unity.Entities.EntityDataAccess*,Unity.Entities.EntityQueryBuilder) (at ./Library/PackageCache/com.unity.entities@1.0.10/Unity.Entities/Iterators/EntityQueryManager.cs:536)
0x0000021bdd93896b (Mono JIT Code) Unity.Entities.EntityManager:CreateEntityQueryUnowned (Unity.Entities.EntityQueryBuilder&) (at ./Library/PackageCache/com.unity.entities@1.0.10/Unity.Entities/EntityManager.cs:4243)
0x0000021bdd95e8b3 (Mono JIT Code) Unity.Entities.EntityManager:CreateEntityQuery (Unity.Entities.EntityQueryBuilder&) (at ./Library/PackageCache/com.unity.entities@1.0.10/Unity.Entities/EntityManager.cs:4237)
0x0000021be16f3c0b (Mono JIT Code) Unity.Entities.EntityManager:CreateEntityQuery (Unity.Entities.EntityQueryDesc[ ]) (at ./Library/PackageCache/com.unity.entities@1.0.10/Unity.Entities/EntityManager.cs:4225)
0x0000021be1755103 (Mono JIT Code) Unity.Entities.BakingCompanionComponentSystem:UpdateCompanionQuery () (at ./Library/PackageCache/com.unity.entities@1.0.10/Unity.Entities.Hybrid/Baking/BakingCompanionComponentSystem.cs:248)
0x0000021be1752b83 (Mono JIT Code) Unity.Entities.BakingCompanionComponentSystem:CreateCompanionGameObjects () (at ./Library/PackageCache/com.unity.entities@1.0.10/Unity.Entities.Hybrid/Baking/BakingCompanionComponentSystem.cs:81)
0x0000021be17526bb (Mono JIT Code) Unity.Entities.BakingCompanionComponentSystem:OnUpdate () (at ./Library/PackageCache/com.unity.entities@1.0.10/Unity.Entities.Hybrid/Baking/BakingCompanionComponentSystem.cs:222)
0x0000021be16d8243 (Mono JIT Code) Unity.Entities.SystemBase:Update () (at ./Library/PackageCache/com.unity.entities@1.0.10/Unity.Entities/SystemBase.cs:420)
0x0000021be174f2fc (Mono JIT Code) Unity.Entities.BakingUtility:postprocessBake (Unity.Entities.World,Unity.Entities.BakingSettings,Unity.Entities.BakingSystem) (at ./Library/PackageCache/com.unity.entities@1.0.10/Unity.Entities.Hybrid/Baking/BakingUtility.cs:170)
0x0000021be17042ab (Mono JIT Code) Unity.Entities.BakingUtility:BakeScene (Unity.Entities.World,UnityEngine.SceneManagement.Scene,Unity.Entities.BakingSettings,bool,Unity.Entities.IncrementalBakingChangeTracker) (at ./Library/PackageCache/com.unity.entities@1.0.10/Unity.Entities.Hybrid/Baking/BakingUtility.cs:87)
0x0000021be1702deb (Mono JIT Code) Unity.Scenes.Editor.LiveConversionDiffGenerator:Bake (Unity.Entities.BakingUtility/BakingFlags) (at ./Library/PackageCache/com.unity.entities@1.0.10/Unity.Scenes.Editor/LiveConversion/LiveConversionDiffGenerator.cs:214)
0x0000021be16f2313 (Mono JIT Code) Unity.Scenes.Editor.LiveConversionDiffGenerator:UpdateLiveConversion (UnityEngine.SceneManagement.Scene,Unity.Entities.Hash128,Unity.Scenes.Editor.LiveConversionDiffGenerator&,Unity.Scenes.LiveConversionMode,ulong,UnityEditor.GUID,Unity.Entities.Build.IEntitiesPlayerSettings,Unity.Scenes.LiveConversionChangeSet&

A developer of Netcode for Entities confirmed that there a still memory leaks within the ECS packages, so at least they know about and may take care about it in the future.

There have been more reports of leaks, I think the leak detection has become better. It did find a leak in my own code that was not found in previous versions. But I also get leak notifications from unity code.

When looking at the memory consumption of my application I don’t see the memory exploding, so as long as people have enough RAM it does not look like a real problem. Garbage collection hits performance harder than a memory leak, right :roll_eyes:. I am hopeful they will fix it.

From URP14.0.7 ForwardLights.cs :

general ‘persistent allocates X individual allocations’ :

looked to me like this could be in user code:

The first argument is the initial capacity. When you use NativeList.Add it will increase the size of the list when there is no empty slot left.
https://docs.unity3d.com/Packages/com.unity.collections@2.2/api/Unity.Collections.NativeList-1.html#Unity_Collections_NativeList_1_Add__0__

That’s interesting. Maybe I should spend some time read the NativeList source code.

After importing the package,u can try to disable Unity’s Domain Reload setting. To do this, go to Edit > Project Settings > Editor menu, and enable the Enter Play Mode Options setting, but leave the Reload Domain and Reload Scene boxes disabled.