Search Unity

Question Jobs: Using WithDisposeOnCompletion(). Still Getting Warning: jobtempalloc has ... 4 frames old.

Discussion in 'Entity Component System' started by VladDerGauner, Jul 18, 2021.

  1. VladDerGauner

    VladDerGauner

    Joined:
    Dec 3, 2015
    Posts:
    21
    Hey everyone,

    I am trying to get the following parallelized code running.

    Code (CSharp):
    1. public class GravitationalPullSystem : SystemBase
    2. {
    3.  
    4.     protected override void OnUpdate()
    5.     {
    6.         float deltaTime = Time.DeltaTime;
    7.         float gravitationalConstant = 0.0000000000667430f;
    8.  
    9.         var query = GetEntityQuery(typeof(AsteroidTag), typeof(Translation), typeof(AsteroidMassData));
    10.         NativeArray<Translation> asteroidTranslations = query.ToComponentDataArray<Translation>(Allocator.TempJob);
    11.         NativeArray<AsteroidMassData> asteroidMasses = query.ToComponentDataArray<AsteroidMassData>(Allocator.TempJob);
    12.  
    13.  
    14.         Entities.ForEach((ref Translation translation, ref AsteroidVelocityData asteroidVelocity, in AsteroidMassData asteroidMass) =>
    15.         {
    16.             var force = float3.zero;
    17.          
    18.             for (int i = 0; i < asteroidTranslations.Length; i++)
    19.             {
    20.                 float3 distDiff = asteroidTranslations[i].Value - translation.Value;
    21.                 if (math.length(distDiff)!=0)
    22.                 {
    23.                     force += math.normalize(distDiff) * (gravitationalConstant * asteroidMass.Value *
    24.                         asteroidMasses[i].Value / (math.length(distDiff) * math.length(distDiff)));
    25.                 }
    26.  
    27.             }
    28.             asteroidVelocity.Value = asteroidVelocity.Value + force * deltaTime / asteroidMass.Value;
    29.             translation.Value = translation.Value + asteroidVelocity.Value * deltaTime;
    30.         }).WithReadOnly(asteroidMasses).WithReadOnly(asteroidTranslations).WithDisposeOnCompletion(asteroidMasses).WithDisposeOnCompletion(asteroidTranslations).ScheduleParallel();
    31.     }
    32. }
    As you can see at the end of the lambda function I am disposing of the NativeContainers, I need for the calculations in the Lambda.

    However, the Editor still gives me the warning; "unity internal jobtempalloc has allocations that are more than 4 frames old"

    The scene I am currently running is small and I get 1000FPS. (If this is relevant.)

    Any ideas?

    Cheers

    Vlad
     
  2. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,264
    If it still gives you that warning after an editor restart, turn on Jobs->Leak Detection->Full Stack Traces.
     
  3. DreamersINC

    DreamersINC

    Joined:
    Mar 4, 2015
    Posts:
    131
    Personal I would write this as an IJobChunk
     
  4. VladDerGauner

    VladDerGauner

    Joined:
    Dec 3, 2015
    Posts:
    21
    Thanks! It actually looks like restarting the editor solved the problem. Then later on it appeared again. Will try to figure out what the problem is even if it just occurs sporadically. Do I need to close anything or somethning like this maybe?
     
  5. VladDerGauner

    VladDerGauner

    Joined:
    Dec 3, 2015
    Posts:
    21
    Probably will look into this. If you want to elaborate however, that would be greatly appreciated.
     
  6. DreamersINC

    DreamersINC

    Joined:
    Mar 4, 2015
    Posts:
    131
  7. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,759
    Do you have netcode installed? This looks like a fixed update issue.

    Side note personally would never use .WithDisposeOnCompletion
    always stick with container.Dispose(Dependency) for consistency (and less magic)
     
  8. 8bitgoose

    8bitgoose

    Joined:
    Dec 28, 2014
    Posts:
    448
    Your frame rate is too fast. Unity runs a bunch of things in the background which rely on a frame rate of maybe 144fps. If it goes too fast, it trips this error. Enable v-sync and it'll go away.

    If it doesn't, you have a leak somewhere in your code.

    This has been an issue with Unity for a couple years and I don't know if they can solve it. I wish they'd allow us to set how many frames TempJob is allowed to live for. It spams the debug and can cause major slowdowns due to writing to your log.