DOTS random memory leaks and null reference exception error

Hello there,
I am not sure if this is a place to write this, but I had these weird issues when I tried using DOTS. I was following the CodeMonkeys tutorial video about the DOTS “EXTREME PERFORMANCE with Unity DOTS! (ECS, Job System, Burst, Hybrid Game Objects)” and not soon after starting to create a few things I started getting issues.

To be honest, I have no idea what is relevant, so I am just adding all the details. So I use:
Unity version: 2022.3.10f1
Entities: 1.0.16
Entities Graphics: 1.0.16
URP: 14.0.8
Code Editor: Rider 2023.2.1

Also, as shown in the video I have enabled in ‘Project Settings>Editor’ the ‘Enter Play Mode Options’ checkbox and left ‘Reload Domain’ and ‘Reload Scene’ as unchecked. I am not sure if this is necessary or not or if this has anything to do with the issues I am having.

These are all the scripts I have made:

using Unity.Burst;
using Unity.Entities;
using Unity.Transforms;

public partial struct RotateSystem : ISystem
{
    public void OnCreate(ref SystemState state)
    {
        state.RequireForUpdate<RotateSpeedComponent>();
    }
    public void OnUpdate(ref SystemState state)
    {
        var job = new RotateJob()
        {
            deltaTime = SystemAPI.Time.DeltaTime
        };
        job.ScheduleParallel();
    }
  
    [BurstCompile]
    public partial struct RotateJob : IJobEntity
    {
        public float deltaTime;
      
        public void Execute(ref LocalTransform transform, in RotateSpeedComponent speed)
        {
            transform = transform.RotateY(speed.value * deltaTime);
        }
    }
}
using Unity.Entities;
using UnityEngine;

public struct RotateSpeedComponent : IComponentData
{
    public float value;
}
public class RotateSpeedAuthoring : MonoBehaviour
{
    public float value;

    private class Baker : Baker<RotateSpeedAuthoring>
    {
        public override void Bake(RotateSpeedAuthoring authoring)
        {
            AddComponent(GetEntity(TransformUsageFlags.Dynamic), new RotateSpeedComponent
            {
                value = authoring.value
            });
        }
    }
}

I have created a cube added the authoring component, created a prefab, and duplicated the cube prefab multiple times in the scene and that’s it.

I want to use DOTS as it would benefit me greatly, however, these random leaks and errors happening already when I have nothing pretty much make me nervous…

Why am I getting all this? Am I missing something?

Everything works and I get no issues when entering and exiting the play mode, however, when I edit any script and the editor compiles the changes I get this error:

NullReferenceException: Object reference not set to an instance of an object
Unity.Entities.Editor.HierarchyWindow.OnCleanup () (at ./Library/PackageCache/com.unity.entities@1.0.16/Unity.Entities.Editor/Hierarchy/HierarchyWindow.cs:113)
Unity.Entities.Editor.DOTSEditorWindow.OnDisable () (at ./Library/PackageCache/com.unity.entities@1.0.16/Unity.Entities.Editor/Common/DOTSEditorWindow.cs:201)

And I get random Persistent allocations memory leaks:

Leak Detected : Persistent allocates 309 individual allocations.
Found 105 leak(s) from callstack:
0x000001394105a383 (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)
0x000001394105a243 (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)
0x000001394105a03b (Mono JIT Code) Unity.Collections.AllocatorManager:TryLegacy (Unity.Collections.AllocatorManager/Block&) (at ./Library/PackageCache/com.unity.collections@2.1.4/Unity.Collections/AllocatorManager.cs:1023)
0x0000013941059e2b (Mono JIT Code) Unity.Collections.AllocatorManager:Try (Unity.Collections.AllocatorManager/Block&) (at ./Library/PackageCache/com.unity.collections@2.1.4/Unity.Collections/AllocatorManager.cs:1055)
0x0000013941059d9b (Mono JIT Code) Unity.Collections.AllocatorManager/AllocatorHandle:Try (Unity.Collections.AllocatorManager/Block&) (at ./Library/PackageCache/com.unity.collections@2.1.4/Unity.Collections/AllocatorManager.cs:540)
0x0000013941059af3 (Mono JIT Code) Unity.Collections.AllocatorManager:AllocateBlock<Unity.Collections.AllocatorManager/AllocatorHandle> (Unity.Collections.AllocatorManager/AllocatorHandle&,int,int,int) (at ./Library/PackageCache/com.unity.collections@2.1.4/Unity.Collections/AllocatorManager.cs:32)
0x00000139410599bb (Mono JIT Code) Unity.Collections.AllocatorManager:Allocate<Unity.Collections.AllocatorManager/AllocatorHandle> (Unity.Collections.AllocatorManager/AllocatorHandle&,int,int,int) (at ./Library/PackageCache/com.unity.collections@2.1.4/Unity.Collections/AllocatorManager.cs:53)
0x0000013a1ac60173 (Mono JIT Code) Unity.Collections.LowLevel.Unsafe.UnsafeList`1<Unity.Entities.BakerEntityUsage/ReferencedEntityUsage>:ResizeExact<Unity.Collections.AllocatorManager/AllocatorHandle> (Unity.Collections.AllocatorManager/AllocatorHandle&,int) (at ./Library/PackageCache/com.unity.collections@2.1.4/Unity.Collections/UnsafeList.cs:354)
0x0000013a1ac60093 (Mono JIT Code) Unity.Collections.LowLevel.Unsafe.UnsafeList`1<Unity.Entities.BakerEntityUsage/ReferencedEntityUsage>:SetCapacity<Unity.Collections.AllocatorManager/AllocatorHandle> (Unity.Collections.AllocatorManager/AllocatorHandle&,int) (at ./Library/PackageCache/com.unity.collections@2.1.4/Unity.Collections/UnsafeList.cs:390)
0x0000013a1abfff9b (Mono JIT Code) Unity.Collections.LowLevel.Unsafe.UnsafeList`1<Unity.Entities.BakerEntityUsage/ReferencedEntityUsage>:SetCapacity (int) (at ./Library/PackageCache/com.unity.collections@2.1.4/Unity.Collections/UnsafeList.cs:399)
0x0000013a1ab1ff5b (Mono JIT Code) Unity.Collections.LowLevel.Unsafe.UnsafeList`1<Unity.Entities.BakerEntityUsage/ReferencedEntityUsage>:.ctor (int,Unity.Collections.AllocatorManager/AllocatorHandle,Unity.Collections.NativeArrayOptions) (at ./Library/PackageCache/com.unity.collections@2.1.4/Unity.Collections/UnsafeList.cs:181)
0x0000013a1ab1fe13 (Mono JIT Code) Unity.Entities.BakerEntityUsage:.ctor (Unity.Entities.Entity,int,Unity.Collections.Allocator) (at ./Library/PackageCache/com.unity.entities@1.0.16/Unity.Entities.Hybrid/Baking/BakerEntityUsage.cs:36)
0x0000013a1ac623b3 (Mono JIT Code) Unity.Entities.BakerState:.ctor (Unity.Entities.Entity,Unity.Collections.Allocator) (at ./Library/PackageCache/com.unity.entities@1.0.16/Unity.Entities.Hybrid/Baking/BakerState.cs:44)
0x0000013a1ac533b3 (Mono JIT Code) Unity.Entities.BakedEntityData:ApplyBakeInstructions (Unity.Entities.Baking.BakeDependencies&,Unity.Entities.Baking.IncrementalBakingContext/IncrementalBakeInstructions,Unity.Entities.BlobAssetStore,Unity.Entities.BakingSettings,Unity.Entities.Conversion.IncrementalHierarchy&,Unity.Entities.Baking.GameObjectComponents&) (at ./Library/PackageCache/com.unity.entities@1.0.16/Unity.Entities.Hybrid/Baking/BakedEntityData.cs:579)
0x0000013a1ac3094b (Mono JIT Code) Unity.Entities.BakingSystem:Bake (Unity.Entities.IncrementalBakingChangeTracker,UnityEngine.GameObject[]) (at ./Library/Pack
Found 204 leak(s) from callstack:
0x000001394105a383 (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)
0x000001394105a243 (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)
0x000001394105a03b (Mono JIT Code) Unity.Collections.AllocatorManager:TryLegacy (Unity.Collections.AllocatorManager/Block&) (at ./Library/PackageCache/com.unity.collections@2.1.4/Unity.Collections/AllocatorManager.cs:1023)
0x0000013941059e2b (Mono JIT Code) Unity.Collections.AllocatorManager:Try (Unity.Collections.AllocatorManager/Block&) (at ./Library/PackageCache/com.unity.collections@2.1.4/Unity.Collections/AllocatorManager.cs:1055)
0x0000013941059d9b (Mono JIT Code) Unity.Collections.AllocatorManager/AllocatorHandle:Try (Unity.Collections.AllocatorManager/Block&) (at ./Library/PackageCache/com.unity.collections@2.1.4/Unity.Collections/AllocatorManager.cs:540)
0x0000013941059af3 (Mono JIT Code) Unity.Collections.AllocatorManager:AllocateBlock<Unity.Collections.AllocatorManager/AllocatorHandle> (Unity.Collections.AllocatorManager/AllocatorHandle&,int,int,int) (at ./Library/PackageCache/com.unity.collections@2.1.4/Unity.Collections/AllocatorManager.cs:32)
0x00000139410599bb (Mono JIT Code) Unity.Collections.AllocatorManager:Allocate<Unity.Collections.AllocatorManager/AllocatorHandle> (Unity.Collections.AllocatorManager/AllocatorHandle&,int,int,int) (at ./Library/PackageCache/com.unity.collections@2.1.4/Unity.Collections/AllocatorManager.cs:53)
0x0000013a1ac60173 (Mono JIT Code) Unity.Collections.LowLevel.Unsafe.UnsafeList`1<Unity.Entities.BakerEntityUsage/ReferencedEntityUsage>:ResizeExact<Unity.Collections.AllocatorManager/AllocatorHandle> (Unity.Collections.AllocatorManager/AllocatorHandle&,int) (at ./Library/PackageCache/com.unity.collections@2.1.4/Unity.Collections/UnsafeList.cs:354)
0x0000013a1ac60093 (Mono JIT Code) Unity.Collections.LowLevel.Unsafe.UnsafeList`1<Unity.Entities.BakerEntityUsage/ReferencedEntityUsage>:SetCapacity<Unity.Collections.AllocatorManager/AllocatorHandle> (Unity.Collections.AllocatorManager/AllocatorHandle&,int) (at ./Library/PackageCache/com.unity.collections@2.1.4/Unity.Collections/UnsafeList.cs:390)
0x0000013a1abfff9b (Mono JIT Code) Unity.Collections.LowLevel.Unsafe.UnsafeList`1<Unity.Entities.BakerEntityUsage/ReferencedEntityUsage>:SetCapacity (int) (at ./Library/PackageCache/com.unity.collections@2.1.4/Unity.Collections/UnsafeList.cs:399)
0x0000013a1ab1ff5b (Mono JIT Code) Unity.Collections.LowLevel.Unsafe.UnsafeList`1<Unity.Entities.BakerEntityUsage/ReferencedEntityUsage>:.ctor (int,Unity.Collections.AllocatorManager/AllocatorHandle,Unity.Collections.NativeArrayOptions) (at ./Library/PackageCache/com.unity.collections@2.1.4/Unity.Collections/UnsafeList.cs:181)
0x0000013a1ab1fe13 (Mono JIT Code) Unity.Entities.BakerEntityUsage:.ctor (Unity.Entities.Entity,int,Unity.Collections.Allocator) (at ./Library/PackageCache/com.unity.entities@1.0.16/Unity.Entities.Hybrid/Baking/BakerEntityUsage.cs:36)
0x0000013a1ac623b3 (Mono JIT Code) Unity.Entities.BakerState:.ctor (Unity.Entities.Entity,Unity.Collections.Allocator) (at ./Library/PackageCache/com.unity.entities@1.0.16/Unity.Entities.Hybrid/Baking/BakerState.cs:44)
0x0000013a1ac54adb (Mono JIT Code) Unity.Entities.BakedEntityData:ApplyBakeInstructions (Unity.Entities.Baking.BakeDependencies&,Unity.Entities.Baking.IncrementalBakingContext/IncrementalBakeInstructions,Unity.Entities.BlobAssetStore,Unity.Entities.BakingSettings,Unity.Entities.Conversion.IncrementalHierarchy&,Unity.Entities.Baking.GameObjectComponents&) (at ./Library/PackageCache/com.unity.entities@1.0.16/Unity.Entities.Hybrid/Baking/BakedEntityData.cs:720)
0x0000013a1ac3094b (Mono JIT Code) Unity.Entities.BakingSystem:Bake (Unity.Entities.IncrementalBakingChangeTracker,UnityEngine.GameObject[]) (at ./Library/Pack

Don’t worry too much about the first error (I have it sometimes too). As you can see from the stack trace, it comes from the Unity Editor Hierarchy Window. You can’t do anything about it (or just close this window when you don’t use it).

Concerning the leak, I really doubt you are responsible of it. If you don’t have it when playing in a build, it’s just an in editor issue.

The final recommendation I can make is to be up-to-date with the unity editor version (actual is 2022.3.27f1) and DOTS packages version (actual is 1.2.0), as they include fixes for this type of errors. Just be aware that updating the DOTS package might lead to some little differences (UI, code or new warnings) comparing to what happens in the tutorial.

1 Like

I just decided to follow this tutorial myself today. No issues so far. But check your computer memory usage, I found this caused issues in the past ie. too many things open at the same time (also 4k 6 split Unity uses alot). Im trying it out on Unity 6 so I may encounter bugs at some point (unable to have dual inspectors is one). If Unity loads with errors at the start, reopening usually fixes this too.

Also to note, unless requiring the job handle (var job) you can just do new Job{•••}.ScheduleParallel();

Also this memory issue can come from always Sleeping the computer without restarting. So good to do a restart ( and update) now and again.