Unmanaged C# memory
Control how memory is allocated to native containers.
Read time 4 minutesLast updated 12 days ago
The C# unmanaged memory layer allows you to access the native memory layer to fine-tune memory allocations, with the convenience of writing C# code.
You can use the namespace (including ) in the Unity core API, and the data structures in the Unity Collections package to access C# unmanaged memory. If you use Unity's job system, or Burst, you must use C# unmanaged memory.
Unity.CollectionsNativeArrayNative container memory allocators overview
Unity's native code libraries track native memory usage through a system of memory labels, areas, and roots that are managed by Unity's native memory manager. The Profiler uses the native memory manager to monitor native memory usage. The asset garbage collector also tracks types that inherit from so that on calls to it can clean up memory assigned to assets that are no longer referenced.
UnityEngine.ObjectResources.UnloadUnusedAssetsUnity's native memory manager uses memory labels to categorize memory usage and to choose which allocates the memory. Which allocator the memory manager uses for each memory label varies between platforms. You can also create your own named memory labels with the API. Important: The API isn't compatible with the Collections package.
AllocatorMemoryLabelMemoryLabelThe memory manager uses areas to categorize memory usage for profiling purposes. It uses roots to track related memory allocations under one root. For example, the native memory for each type that inherits from has a root in the area called . When the object is deleted, for example via , all allocations associated with that object's root are also freed.
UnityEngine.ObjectObjectResources.UnloadUnusedAssetsYou can use the Memory Profiler package to check memory usage ordered by memory labels, areas, and roots. This includes any custom labels you've defined using MemoryLabel. You can also discover the underlying allocator that allocated the memory through region names.
Allocator types
You can use certain native Allocator types with native container types in the namespace. The allocator types available in this way are:
Unity.Collections- : A temporary allocator type for short-lived allocations.
Temp - : A temporary allocator type for longer-lived allocations passed from job to job.
TempJob - : An allocator without any temporal restrictions.
Persistent - : An allocator for DSP audio kernel allocations.
AudioKernel - : An allocator that lasts the lifetime of the domain.
Domain
To use these types, pass an enum value to select the native allocator type's memory label to use. Alternatively, for low-level allocations where you need custom categorization, you can create and pass a instance directly to allocation functions. If a chosen allocator is full, Unity chooses a fallback allocator instead (usually , or Temp Overflow).
AllocatorMemoryLabelTempTempJobTemp
TempTempThere are multiple allocators, and each thread that needs an allocator has one. Unity recycles allocators each frame ensuring a clean memory usage pattern. However, if memory spills over into the fallback allocators, their memory might get fragmented.
TempTempYou can use and to get and set the size for allocators.
Profiler.GetTempAllocatorSizeProfiler.SetTempAllocatorRequestedSizeTempThe sizes for the allocator default to:
Temp- Editor: 16 MB main thread, 256 KB worker threads.
- Players: 4 MB main thread, 256 KB worker threads.
If these sizes are exceeded, allocations on the main thread fall back to the allocator, and on other threads they fall back to the Temp Overflow allocator. For more information on temporary native allocations, refer to the Thread storage local (TLS) allocator documentation.
TempJobTempJob
This type uses a linear style allocator which Unity stores in a area that it allocates from main memory. It's designed for larger temporary allocations that are passed from job to job, and those that might carry data between frames. If the allocations aren't disposed of within 4 frames of their creation, a warning is displayed in the Unity Editor.
TempJobIf this allocator is full, allocations fall back to the Temp Job Overflow Allocator. For more information on temporary job allocations, refer to the Thread safe linear allocator documentation.
Persistent
This allocator allocates memory with no time restrictions, is only limited by the amount of free memory on a device, and therefore has no fallback. It can exist for as long as you need it, but this also means that only the class warns you if you don't free the allocated memory again, and then only if your handle to the memory was garbage collected. Allocating persistent memory is slower than allocating temporary memory.
DisposeSentinel