Optimizing Malloc(Temp) Pool memory usage - 12.46 GB!

Hi everyone,

After auditing my game’s dedicated server binary, I found that the most impactful part of the memory composition of the game is “Malloc(Temp) Pool”:

The game loads a shocking amount of objects on server startup. This is a huge save file with hundreds of thousands of entities. Memory pressure is to be expected of course. But we really just can’t figure out why “Malloc(Temp) Pool” specifically is the issue, or how to resolve it.

There’s not a lot of resources online about this specific type of memory composition, so I’ve come here to ask on any community expertise.

I did some low-level debugging, and found these results:

ThreadsafeLinearAllocator<false>	this: 0x7e57f86583f0	Allocated: 0	Reserved: 603979776
ThreadsafeLinearAllocator<false>	this: 0x7e57f8658d70	Allocated: 79	Reserved: 201326592
DualThreadAllocator	this: 0x7e57f8659a70	Allocated: 602523024	Reserved: 805314560
DualThreadAllocator	this: 0x7e57f8659f30	Allocated: 500385916	Reserved: 919343104
DualThreadAllocator	this: 0x7e57f865a3f0	Allocated: 46060360	Reserved: 52428800
GetReservedHeapSize	Reserved: 24744636416
MallocTrackingManager	this: 0x7e57f8653c10	Allocated: 0	Reserved: 0
TLSAllocator<Mode0>	this: 0x7e57f8653ed0	Allocated: 0	Reserved: 15761408
ThreadsafeLinearAllocator<false>	this: 0x7e57f86570f0	Allocated: 0	Reserved: 0
ThreadsafeLinearAllocator<false>	this: 0x7e57f8657a70	Allocated: 0	Reserved: 0
BucketAllocator	this: 0x7e57f8654270	Reserved: 4194304
BucketAllocator	this: 0x7e57f865a530	Reserved: 4194304
DualThreadAllocator	this: 0x7e57f8656fb0	Allocated: 19102462839	Reserved: 21374193664

Loading the giant save file happens in a single frame. But I found that trying to optimize this and introducing frame delays via coroutine in between big steps didn’t make much difference.

This memory seems largely unaccounted for. I audited each place we use UnsafeUtility/Allocator allocations such as NativeArray and didn’t find anything interesting. We also properly dispose of any of this allocated memory correctly.

Does anyone have any experience with this kind of memory profile?

Many thanks

how do you load the save file? is that happening on a main thread or on other thread? (perhaps try on main thread if not)
I was assuming that Allocator.Temp should free memory next frame on on the next reuse occasion, but perhaps not

Hi Alexey,

We’re doing it on the main thread, yeah.

I read that Allocator.Temp doesn’t shrink its buffer though, but I’m not entirely sure as the information I read whilst tackling this often contradicted other experiences/docs.

But, unfortunately, we didn’t find much improvement when we tried adding thinks in between. Also, it was unfortunately also actually buggy at the high-level: some of the entities loaded from the save’s state depends on other entities existing/being loaded, so running a frame unfortunately is unstable in that regard.