• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Klusimo · Nov 06, 2020 at 11:28 AM · dots

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)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by andrew-lukasik · Nov 06, 2020 at 05:44 PM


Do temporary allocators erase data stored in the list ?

No, they do not. It's your responsibility to do that.

correction: Allocator.TempJob requires you to call Dispose() (or equivalent) but Allocator.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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Klusimo · Nov 06, 2020 at 08:49 PM 1
Share

$$anonymous$$any thanks! You must be the only person on this forum :D

avatar image andrew-lukasik Klusimo · Nov 07, 2020 at 09:41 AM 0
Share

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 :)

avatar image andrew-lukasik · Nov 07, 2020 at 11:45 AM 1
Share

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 } );
 }
avatar image Klusimo andrew-lukasik · Nov 07, 2020 at 12:01 PM 0
Share

Good to know, that explains why several of my past tests with native containers were so slow. Thanks!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

212 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

EntityArchetype and ScriptableObject 0 Answers

Unity DOTS - How is it that they've implemented Entities.ForEach? 0 Answers

Adding constraints to PhysicsBody of the Unity.Physics DOTS package 1 Answer

Can not add PhysicsCollider at runtime Unity in ECS 1 Answer

Should I use preview packages when developing Asset Store assets? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges