When should I use which value (None
, Invalid
, Temp
, TempJob
, Persistent
, FirstUserIndex
, AudioKernal
)
How does each Allocator
affect allocation and the lifespan of the NativeArray
in implementation?
I can't figure out how the lifespans/implementations differ for each Allocator
value when instantiating a NativeArray
. I've checked the docs and IntelliSense to no avail, besides being shown which Enum
values are available.
Allocator.Temp
Offers fastest allocation time. Is valid for a single frame. No need to call
Dispose()
. Can't be assigned to field of a job struct (because job's lifetime can be over 1 frame) but can be allocated by a job execution code.Allocator.TempJob
Fast, temporary allocations for job struct fields.
Allocator.Persistent
Slow allocation. No lifetime restrictions. Must call
Dispose()
to free (memory leak otherwise). Ideal for data that needs to always exist for a system to function.Allocator.None
Allocator.None
is for these rare cases where you need to say "no allocation happened here" to create a validNativeArray
of which lifetime is decided elsewhere. For example, to access managed memory as if it is just anotherNativeArray
:source: https://gist.github.com/andrew-raphael-lukasik/812e152f95e38cc13c44e5040c5739bc
Invalid
Signals allocation failure. Don't use.
FirstUserIndex
No idea. Don't use.
AudioKernel
No idea. Don't use.