Temp allocations never disposed

Unity 2020.3.24f1

I hit this using NativeHashMap.GetValueArray.

Context was I was calling that inside OnDrawGizmosSelected() in editor mode with ExecuteAlways attribute on the MB. TempJob and Persistent allocations worked fine. But Temp just never released the memory crashing Unity once it hit around 16gb of process memory. I remember the error was native allocation specific. Makes sense there is an internal limit, the OS had plenty of free memory left.

1 Like

Outside of jobs I’ve always found I have to dispose Temp allocations. I normally use the using var foo = new NativeThing<T>(Allocator.Temp); approach to get scoped cleanup.

This concerned me so I setup a test project but I can’t repo.

Made a simple script to allocate 5MB/frame in temp memory. Editor stayed static ± 20MB and a build stayed static at 56MB of memory.

Bumped it to 500MB/frame in temp memory and again couldn’t repo. Editor total memory usage oscillated between 1800 and 3000MB/frame but did not grow. In a build memory usage oscillated between 246 and 1800MB.

I could not cause app or editor memory to grow.

Tests run on 2020.3.16f1

Do you have a repo?

Ah my concern may no longer be valid. As of 2020.2 the documentation said:

Allocator.Temp has the fastest allocation. It is for allocations with a lifespan of one frame or fewer. You should not pass NativeContainer allocations using Temp to jobs. You also need to call the Dispose method before you return from the method call (such as MonoBehaviour.Update, or any other callback from native to managed code).

But as of 2020.3 the documentation was changed.

Ya but it’s odd it took a while to trigger, it didn’t repro immediately and then several iterations on the code later trying to figure out WTF it started bugging out. So I think some internal state that who knows what the trigger is. Reloading, that and some other threshold, wild guesses is the best I have here. FYI the project is small no third party assets pretty stock setup, it’s a chunk of code I wanted to work on in complete isolation.

https://gist.github.com/gamemachine/f8f1781969ca2c63e5b43769797c88b1

I noticed your script has [ExecuteAlways]
Are you executing this outside play mode? Because i can totally see this not being cleaned properly in editor mode as it usually gets reset end of frame.

Yes I noted I was in editor mode in the original post. As you say I also assumed this was from being in editor mode. Probably they know about this and just punted on solving it. Unity’s update mechanism’s have so much legacy crap I’ve seen their own developers have a hard time reasoning about it.