Search Unity

Where should i dispose NativeArray in a JobComponentSystem?

Discussion in 'Entity Component System' started by Vladimir-Borodin, Jun 27, 2018.

  1. Vladimir-Borodin

    Vladimir-Borodin

    Joined:
    Jun 1, 2017
    Posts:
    15
    Hi! Here is my example of code:

    Code (CSharp):
    1. public class MySystem : JobComponentSystem {
    2.         public struct Group {
    3.             public ComponentDataArray<MyComponent> components;
    4.             public int Length;
    5.         }
    6.         [Inject] private Group group;
    7.  
    8.         private struct SomeJob : IJobParallelFor {
    9.             [ReadOnly]
    10.             public NativeArray<double> values1;
    11.             [ReadOnly]
    12.             public NativeArray<double> values2;
    13.             public NativeArray<MyComponent> data;
    14.             public void Execute(int index) {
    15.                 MyComponent c = data[index];
    16.                 StaticFunctionForDoingStuff(ref c, values1[index], values2[index]);
    17.                 data[index] = c;
    18.             }
    19.         }
    20.  
    21.         protected override JobHandle OnUpdate(JobHandle inputDeps) {
    22.             NativeArray<double> values1 = new NativeArray<double>(group.Length, Allocator.TempJob);
    23.             NativeArray<double> values2 = new NativeArray<double>(group.Length, Allocator.TempJob);
    24.             for (int i = 0; i < group.Length; i++) {
    25.                 //fill values1 and values2 with numbers
    26.             }
    27.             SomeJob j = new SomeJob() {
    28.                 data = group.components.GetChunkArray(0, group.Length),
    29.                 values1 = values1,
    30.                 values2 = values2
    31.             };
    32.             return j.Schedule(data.Length, 5);
    33.         }
    34.     }
    In console I have "A Native Collection has not been disposed, resulting in a memory leak. It was allocated at..." message.

    I know that I must call j.Complete() and values1.Dispose(); values2.Dispose(), but where?
     
  2. dartriminis

    dartriminis

    Joined:
    Feb 3, 2017
    Posts:
    157
    There's an attribute you can add to the native array field in the job. I think it's called [DeallocateNativeContainerOnJobCompletion]. As far as I know, it only works for NativeArray types right now.
     
    Vladimir-Borodin likes this.
  3. Vladimir-Borodin

    Vladimir-Borodin

    Joined:
    Jun 1, 2017
    Posts:
    15
    Thanks, it works!
    One another question then. Where can I read about this attributes? I checked documentation, but there is nothing similar there.
     
  4. dartriminis

    dartriminis

    Joined:
    Feb 3, 2017
    Posts:
    157
    Yeah, documentation is a little sparse, but remember, this stuff is only in preview ;). You can also try exploring some of the systems that Unity created. Unity package soures are cached on your machine (appdata/local/unity/cache I think). You can use them for inspiration.
     
    Vladimir-Borodin likes this.
  5. recursive

    recursive

    Joined:
    Jul 12, 2012
    Posts:
    669
    @dartriminis is right, you'll learn more about the way everything fits together (and also some options the docs don't even cover yet) than from the docs right now.
     
  6. sebas77

    sebas77

    Joined:
    Nov 4, 2011
    Posts:
    1,642
    resuming thread. Does the compiler attribute work with custom NativeContainers?
     
  7. M_R

    M_R

    Joined:
    Apr 15, 2015
    Posts:
    559
    you need to enable it with
    [NativeContainerSupportsDeallocateOnJobCompletionAttribute]
    .
    (only doc about that is here)
     
    sebas77 likes this.
  8. ina

    ina

    Joined:
    Nov 15, 2010
    Posts:
    1,085
    is [NativeContainerSupportsDeallocateOnJobCompletionAttribute] antiquated now in 2020? I've seen NativeArrays used in ARFoundation examples but have not seen that [] before
     
  9. Nyanpas

    Nyanpas

    Joined:
    Dec 29, 2016
    Posts:
    406
    Isn't SystemBase going to replace JobComponentSystem or am I misunderstanding?
     
  10. ina

    ina

    Joined:
    Nov 15, 2010
    Posts:
    1,085
    how exactly do you specify this attribute?
    and where?
     
  11. ina

    ina

    Joined:
    Nov 15, 2010
    Posts:
    1,085
    error CS0592: Attribute 'NativeContainerSupportsDeallocateOnJobCompletion' is not valid on this declaration type. It is only valid on 'struct' declarations.