- Home /
Will Allocator.Temp erase contents of my native containers?
Hello there, I am working with native containers and I am confused with allocators. I understand what they do, Temp lasts only 1 frame, TempJob up to 3 and Persistent indefinitely, but what confuses me is if the temporary allocators erase data stored in the list.
What I mean by that is that I have one list that I want to "re-do" every frame and I dont know if I can rely on just the temp allocator to erase the contents, or if I must also execute .Clear to erase the data. I also heard I must use .Dispose at the end of my usage for the container, can you confirm it/explain it as well?
Many thanks for answers. (Edit: Jesus Christ I use "I" very, very often lmao)
Answer by andrew-lukasik · Nov 06, 2020 at 05:44 PM
No, they do not. It's your responsibility to do that.Do temporary allocators erase data stored in the list ?
correction:
Allocator.TempJob
requires you to callDispose()
(or equivalent) butAllocator.Temp
doesn't. Note that this "disposal" it no "erasure" necessarily i.e. given memory block won't be actively cleared but merely flagged as free for reuse by future allocations.
I also heard I must use .Dispose at the end of my usage for the container, can you confirm it/explain it as well?
Every NativeContainer
requires you to call Dispose()
directly or indirectly (jobs can schedule deallocation when completed).
One of the ideas behind NativeContainer
is to give us power to manually allocate memory C-like style. But this comes with responsibility to release that memory explicitly so there is no memory leaks.
I have one list that I want to "re-do" every frame and I dont know if I can rely on just the temp allocator to erase the contents, or if I must also execute .Clear to erase the data.
I suggest going with Allocator.Persistent
here i.e. allocate once then reuse the same memory block while system is running:
public class MySystem : SystemBase
{
NativeList<int2> myPersistentList;
protected override void OnCreate ()
{
myPersistentList = new NativeList<int2>(
initialCapacity: 100 ,
allocator: Allocator.Persistent
);
}
protected override void OnDestroy ()
{
if( myPersistentList.IsCreated )
myPersistentList.Dispose( Dependency );
}
protected override void OnUpdate ()
{
myPersistentList.Clear();
var myListAlias = myPersistentList;// just an alias, nothing more
Job
.WithName("fill_list_job")
.WithCode( () =>
{
for( int i=0 ; i<100 ; i++ )
myListAlias.Add( new int2{ x=i , y=i } );
} )
.Schedule();
}
}
PS: I/O access to NativeContainers
is optimized for job systems. In practice this means that accessing those containers outside some kind of IJob will be very slow.
$$anonymous$$any thanks! You must be the only person on this forum :D
There is definitely more OOP than DOD programmers hanging here at the moment. If nobody responds here in the future then ask on data oriented technology stack forum - there is plenty of informed people there :)
I just remembered something and fixed the code sample in the answer.
I didn't mention that but I/O access to NativeContainers
is optimized for job systems. In practice this means that accessing those containers outside any job will be very slow. Avoid doing this:
protected override void OnUpdate ()
{
myPersistentList.Clear();
// code below will be very slow because it's not part of any job system:
for( int i=0 ; i<100 ; i++ )
myPersistentList.Add( new int2{ x=i , y=i } );
}
Good to know, that explains why several of my past tests with native containers were so slow. Thanks!
Your answer
Follow this Question
Related Questions
[ECS] Create Entities from Job ( IJobForEachWithEntity) using an EntityCommandBuffer 1 Answer
Best way to get data from a Monobheaviour in a JobComponentSystem? 2 Answers
[ECS] DefaultWorldInitialization.Initialize take 50MB 1 Answer
How to serialize/deserialize entities at runtime -1 Answers
How to create realistic wire 0 Answers