Search Unity

Permanent NativeArrays

Discussion in 'Data Oriented Technology Stack' started by georgeq, Apr 12, 2018.

  1. georgeq

    georgeq

    Joined:
    Mar 5, 2014
    Posts:
    484
    I have a set of data that needs to be filled and iterated a couple of times on every FixedUpdate, in order to benefit form the linear memory layout I'm using native arrays. Since the number of elements is always the same, I think there's a waste of time disposing the array just to have to create it over again next time with exactly the same parameters, so instead I create the array OnEnable and dispose it OnDisable. The program works just fine and theoretically there's no room for leaking, however I'm getting the following message:

    Internal: JobTempAlloc has allocations that are for more than 4 frames old - this is not allowed and likely a leak.

    Maybe someone at Unity forgot to include Allocator.Permanent?
     
  2. recursive

    recursive

    Joined:
    Jul 12, 2012
    Posts:
    598
    Are you using Allocator.Persistent?
     
  3. georgeq

    georgeq

    Joined:
    Mar 5, 2014
    Posts:
    484
    Yes, I do, but since there's no real documentation about it, I don't know if this behavior is correct or not. This page is of no help at all.
     
  4. recursive

    recursive

    Joined:
    Jul 12, 2012
    Posts:
    598
    From my own experience, and I think some posts elsewhere in this sub-forum, using the persistent allocator avoids that warning message, since you're telling the system that'll you'll be keeping the memory around long-term. I believe this not only sets up certain monitoring, but also determines which allocation strategy and memory arena your block is allocated with.

    From what I've seen:
    Allocator.Invalid - used for library testing, maybe for handling errors. Normal users shouldn't use it.
    Allocator.None - doesn't actually allocate anything, used for testing.
    Allocator.Temp - intended to be used for the scope of the current function (ultra-fast allocation/deallocation, can be allocated within a job kernel I think but shouldn't be returned or passed on). Will start throwing warnings if it lives more than 1 frame.
    Allocator.TempJob (fast allocation/deallocation) - intended to be used for the scope of 1 frame, effectively it should be declared and disposed within 1 frame, but can live up to 3 frames before that warning kicks in.
    Allocator.Persistent - stays alive as long as the user wishes it, up to and including the life of the program, but user must Manully track it and deallocate. Leak warnings will only happen on playmode exit / shutdown.
     
    Last edited: Apr 14, 2018
    Creepgin and nzhangaudio like this.
  5. GabrieleUnity

    GabrieleUnity

    Unity Technologies

    Joined:
    Sep 4, 2012
    Posts:
    116
    Yes,
    Allocator.Persistent
    should not trigger the warning unless there is a bug somewhere. Maybe try to make sure the warning is not caused by another
    NativeArray
    allocated with a different label.
     
  6. AlkisFortuneFish

    AlkisFortuneFish

    Joined:
    Apr 26, 2013
    Posts:
    661
    Managed arrays of value types are also linear, are you sure you need NativeArray for this?
     
  7. georgeq

    georgeq

    Joined:
    Mar 5, 2014
    Posts:
    484
    No, I just have 1 allocation point, and it uses Allocator.Persistent.
     
  8. slime73

    slime73

    Joined:
    May 14, 2017
    Posts:
    31