Unity Issue Tracker - UnsafeUtility.Malloc uses 4MB when creating an empty NativeQueue
Closed
UUM-103862
UnsafeUtility.Malloc uses 4MB when creating an empty NativeQueue
Unity: Kernel (DOTS)
As Designed
6.2.X
6.1.X
6.0.X
2022.3.X
NativeQueue internally implements block caching mechanism that's used by all queues. The reason for memory bump is because on first use static method creates this pool of empty blocks, that are then used at later point.
How to reproduce:
1. Open the attached "IN-98674" project
2. Open the "test" scene
3. Navigate to the project directory and run the "test1.exe" (found in "IN-98674/Builds/test1")
4. In the "test1" window, press the Spacebar
5. Go to “Open Memory Profiler > Capture New Snapshot“
6. Select the captured snapshot
7. Navigate to “All of Memory > Native > Unity Subsystems > UnsafeUtility > Malloc (Persistent)”
8. Observe the “Allocated Size” value
Expected result: Malloc (Persistent) uses around 1.3-1.5KB of memory (similar to other native collections)
Actual result: Malloc (Persistent) uses 4.0MB of memory
Reproducible with: 2022.1.0a10, 2022.3.61f1, 6000.0.47f1, 6000.1.0f1, 6000.2.0a9
Could not test with: 2021.3.51f1 (different Memory Profiler UI made it difficult to locate the Malloc (Persistent) value)
Reproducible on: macOS 15.3 (M4), Windows 11 (by user)
Not reproducible on: No other environments tested
Notes:
The “Test.cs“ script, attached to the “Main Camera” GameObject in the Hierarchy window, creates and deletes a NativeQueue when the Spacebar is pressed
Reproducible with IL2CPP and Mono Scripting Backend
The “GC Allocated In Frame” value in the Profiler window shows different results when creating a NativeQueue. With IL2CPP Scripting Backend, Windows shows 11.4 KB while MacOS shows 8.0 KB. With Mono Scripting Backend, Windows shows 2.2 KB while MacOS shows 2.5 KB
a NativeQueue uses 4.0 MB of memory, while NativeList uses 1.3 KB, and NativeArray uses 1.5 KB
Prioritise and Follow this issue
Have a workaround or additional info about this issue?