Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Internal: JobTempAlloc has allocations that are more than the maximum lifespan of 4 frames old - thi

Discussion in 'Entity Component System' started by keepupalltime, Jul 24, 2023.

  1. keepupalltime

    keepupalltime

    Joined:
    Apr 17, 2020
    Posts:
    6
    Code (CSharp):
    1. Internal: JobTempAlloc has allocations that are more than the maximum lifespan of 4 frames old - this is not allowed and likely a leak
    I receives this warning without specifying the location of the code, How do I locate the problem and fix it.
     
  2. davenirline

    davenirline

    Joined:
    Jul 7, 2010
    Posts:
    941
    You have a native collection that was created with Allocator.JobTempAlloc and was not disposed. Always ensure that these are disposed properly. I usually use this pattern:
    Code (CSharp):
    1. // Could be any collections
    2. NativeArray array = new(..., Allocator.JobTempAlloc);
    3.  
    4. // Job scheduling that uses the collection
    5. MyJob job = new() {
    6.     array = array,
    7.     ...
    8. };
    9. state.Dependency = job.ScheduleParallel(..., state.Dependency);
    10.  
    11. // Dispose the collection
    12. state.Dependency = array.Dispose(state.Dependency);
    You can also use the attribute DeallocateOnJobCompletion on your job but I prefer calling the Dispose() myself as it is more clear when is it disposed.
     
  3. keepupalltime

    keepupalltime

    Joined:
    Apr 17, 2020
    Posts:
    6
    Thank you very much. Perfect solution to my problem