[Solved] Trying to find source of memory leak.

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.

6844169--796754--upload_2021-2-16_19-43-24.png

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.

Any thoughts, directions and tips?

Do you have a non default editor layout ?

Not default, if you mean like per factory settings, but pretty much nothing special.

6844304--796787--upload_2021-2-16_20-26-8.jpg

In build that makes no difference however.
So I am sure that is not it.

Use the memory profiler package and compare 2 snapshots and see how they differ

1 Like

Maybe native containers that you are creating but never calling Dispose? Hard to tell without seeing actual code.

1 Like

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.

I did further diff of editor runtime.

So it looks like is indeed NativeArrays.

6844577--796859--upload_2021-2-16_21-46-32.png

6844577--796862--upload_2021-2-16_21-46-45.png

After few min of runtime, I got nearly 3 GB diff.

6844577--796871--upload_2021-2-16_21-50-3.png

In snapshot I detected 1.27 GB.

Oh well, I already looked for potentially suspicious NativeArrays. I got then for hunt again :slight_smile:

But I expected to see some warring at least. But nothing in console, even I got leak detection enabled. :confused:

1 Like

Ok not what I ran into,my issues ended up an editors only issues. I docked the build configuration in the editors and it slowly killed unity.


I see now, why you asked about editor config now.

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.

6844916--796934--upload_2021-2-16_23-15-20.png

That lead, to executing physics trigger event related systems.

6844916--796937--upload_2021-2-16_23-18-59.png

To mitigate the problem, when I add IsFinishedTag component, I also add Unity.Physics.PhysicsExclude component. That stops infinite generation of NativeArrays.

6844916--796940--upload_2021-2-16_23-22-33.png

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.

6844916--796931--upload_2021-2-16_23-13-14.jpg

2 Likes