I am trying to find source of memory leak.
It seems memory usage grows stiltedly, as long editor, or built version is running.
Restarting Unity editor, clears the memory. But executing stop / play mode, will just keep adding.
Memory gain seems proportional, to number of entities.
It can start as low as 1 GB, and keeps growing over the time, until run out of memory.
In my case, that is about an hour, for given setup.
I was looking into profiler, and NativeArrays seems ever growing.
This may be significant factor to it.
Yet I have all native array disposed, no warnings.
And it seems that profiler NativeArray memory does not fully corresponds, to Unity editor memory usage in the Task Manger.
But most NativeArrays operate every few seconds. And growth is observed continuously. So unless there is some delay, or something, I don’t know what that causes it.
Also, memory seems keep growing, whenever I have Full Stack Trace enabled or not.
My another thought is, leading to Unity.Physics.Systems.StepPhysicsWorld.
I use triggers, for detecting collisions with entities and multiple raycasts.
But after disabling Leak detection and safety checks, GC goes mostly flat.
The spike of GC below is different source, which I am aware of, but It is not contributing to ever growing memory leak.
Yep I understand. Is just few systems inter-related. Besides one, they are rather simple one.
But because of entities relation, nothing trivial that I can isolate. I spent on this quite a bit now, trying figure out.
Which makes harder to narrow the problem down.
I do dispose NativeArrays and NativeHashmaps. Maybe there is something else, that I am not aware of. But wouldn’t editor throw some warring otherwise? Like in case of NativeArrays.
This is good call.
The only issue I got with it now, while I can profile memory in editor, linking the dev built version profiling is freezing. So I am unable to capture snapshot. In the Editor ot takes only few seconds to grab it. In built however, I was waiting few min, while application was running and never stopped. Application is rather simple.
Memory profiler never stopped capturing, until I forced to shut down. So I am not sure, what issue is with that.
Maybe I should use earlier version of previe package, of memory profiler? Shall try that.
However, I think found the issue. At least no more memory leak.
In my case, is a bit weir, but leads to Unity Physics.
You see here, red cars once hit the yellow walls, they stop.
By stopping, I add IsFinishedTag.
However, because they stop on the wall, they stay on the colliders.
That lead, to executing physics trigger event related systems.
To mitigate the problem, when I add IsFinishedTag component, I also add Unity.Physics.PhysicsExclude component. That stops infinite generation of NativeArrays.
Then when cycle is restarted, I remove Unity.Physics.PhysicsExclude.
I am not convinced this is right way to deal with a problem, but at least work for me.
No more memory leak so far.
Yet, I am not really happy at this point, that physics NativeArrays to be an issue.
Be in mind, I did not look into, nor tried to modified Unity BuildPhysicsWorldSystem.