Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

C# Job-System Error: Internal: JobTempAlloc has allocations that are more than 4 frames old

Discussion in 'Scripting' started by ZoCProffel, Aug 3, 2019.

  1. ZoCProffel

    ZoCProffel

    Joined:
    Oct 20, 2017
    Posts:
    3
    Hello,
    For the past month, I have been working on a piece of code in order to predict crowd movements. For that I use the C# Job System. Sadly, I'm currently getting an error:
    "Internal: JobTempAlloc has allocations that are more than 4 frames old - this is not allowed and likely a leak"
    "To Debug, enable the define: TLA_DEBUG_STACK_LEAK in ThreadsafeLinearAllocator.cpp. This will output the callstacks of the leaked allocations"

    I found this post on the forum which talks about the same problem (https://forum.unity.com/threads/job...tions-that-are-more-than-4-frames-old.513124/) and I have tried to find the problem in my code using the solution that is stated in this post but it hasn't worked so far. I have put my Code down below:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.AI;
    5. using Unity.Mathematics;
    6. using Unity.Jobs;
    7. using Unity.Collections;
    8.  
    9. public class Pedestrian_Job : MonoBehaviour
    10. {
    11.     //This is the static array for all pedestrians
    12.     public static GameObject[] pedestriansGB;
    13.  
    14.     //Here are the variables for the simulation calculations
    15.     public Vector2 A_Min_Max = new Vector2(1900f, 2100f);
    16.     public Vector2 B_Min_Max = new Vector2(0.06f, 1.0f);
    17.     public static float k = 120000f;
    18.     public static float kappa = 240000f;
    19.     public Vector2 mass_Min_Max = new Vector2(50f, 90f);
    20.     public Vector2 radius_Min_Max = new Vector2(0.4f, 0.6f);
    21.     public float desired_velocity = 1f;
    22.     public Transform target;
    23.     public float panic = 0.2f;
    24.  
    25.     // Start is called before the first frame update
    26.     void Start()
    27.     {
    28.  
    29.  
    30.         //At the start of every Simulation paths are caluclated and created for every Pedestrian
    31.         pedestriansGB = GameObject.FindGameObjectsWithTag("pedestrian");
    32.         for (int i = 0; i < pedestriansGB.Length; i++)
    33.         {
    34.  
    35.             pedestriansGB[i].GetComponent<Job_Data>().Target = target.position;
    36.             NavMeshAgent agent;
    37.             if (pedestriansGB[i].GetComponent<NavMeshAgent>() == null)
    38.             {
    39.                 agent = pedestriansGB[i].AddComponent<NavMeshAgent>();
    40.             }
    41.             else
    42.             {
    43.                 agent = pedestriansGB[i].GetComponent<NavMeshAgent>();
    44.             }
    45.             NavMeshPath path = new NavMeshPath();
    46.             agent.CalculatePath(pedestriansGB[i].GetComponent<Job_Data>().Target, path);
    47.             pedestriansGB[i].GetComponent<Job_Data>().Local_target = path.corners[1];
    48.             pedestriansGB[i].GetComponent<Job_Data>().Path_corners = path.corners;
    49.  
    50.             //This sets all the constants for the pedestrians
    51.             pedestriansGB[i].GetComponent<Job_Data>().A = UnityEngine.Random.Range(A_Min_Max.x, A_Min_Max.y);
    52.             pedestriansGB[i].GetComponent<Job_Data>().B = UnityEngine.Random.Range(B_Min_Max.x, B_Min_Max.y);
    53.             pedestriansGB[i].GetComponent<Job_Data>().K = k;
    54.             pedestriansGB[i].GetComponent<Job_Data>().Kappa = kappa;
    55.             pedestriansGB[i].GetComponent<Job_Data>().Mass = UnityEngine.Random.Range(mass_Min_Max.x, mass_Min_Max.y);
    56.             pedestriansGB[i].GetComponent<Job_Data>().Radius = UnityEngine.Random.Range(radius_Min_Max.x, radius_Min_Max.y);
    57.             pedestriansGB[i].GetComponent<Job_Data>().Desired_velocity = desired_velocity;
    58.         }
    59.         //All this data is stored in the Job_Data Component of the Pedestrian
    60.     }
    61.  
    62.     // Update is called once per frame
    63.     void Update()
    64.     {
    65.         //---------------------------------------------------------------------------------------------------------------------------------------------------------//
    66.         //Here the simulation Data is prepared
    67.         pedestriansGB = GameObject.FindGameObjectsWithTag("pedestrian");
    68.  
    69.         //This is a NativeContainer for most of the pedestrians data
    70.         NativeArray<PedestrianForJob> pedestrians = new NativeArray<PedestrianForJob>(pedestriansGB.Length, Allocator.TempJob);
    71.  
    72.  
    73.         //Now the Native Containers are populated with data
    74.         for (int i = 0; i < pedestriansGB.Length; i++)
    75.         {
    76.             // This fills the first Native Container
    77.             pedestrians[i] = new PedestrianForJob(
    78.                 pedestriansGB[i].transform.position,
    79.                 pedestriansGB[i].GetComponent<Job_Data>().Last_position,
    80.                 pedestriansGB[i].GetComponent<Job_Data>().Target,
    81.                 pedestriansGB[i].GetComponent<Job_Data>().A,
    82.                 pedestriansGB[i].GetComponent<Job_Data>().B,
    83.                 pedestriansGB[i].GetComponent<Job_Data>().K,
    84.                 pedestriansGB[i].GetComponent<Job_Data>().Kappa,
    85.                 pedestriansGB[i].GetComponent<Job_Data>().Desired_velocity,
    86.                 panic,
    87.                 pedestriansGB[i].GetComponent<Job_Data>().Local_target,
    88.                 pedestriansGB[i].GetComponent<Job_Data>().Mass
    89.                 );
    90.  
    91.             //This checks wheather the local target needs to be updated since this cant be done inside the job
    92.             if (Vector3.Distance(pedestriansGB[i].GetComponent<Job_Data>().Local_target, pedestriansGB[i].transform.position) < 5f)
    93.             {
    94.                 for (int k = 0; k < pedestriansGB[i].GetComponent<Job_Data>().Path_corners.Length; k++)
    95.                 {
    96.                     if ((Vector3.Distance(pedestriansGB[i].GetComponent<Job_Data>().Path_corners[k], pedestriansGB[i].GetComponent<Job_Data>().Local_target) < 5f) && ((k+1) < pedestriansGB[i].GetComponent<Job_Data>().Path_corners.Length))
    97.                     {
    98.                         pedestriansGB[i].GetComponent<Job_Data>().Local_target = pedestriansGB[i].GetComponent<Job_Data>().Path_corners[k + 1];
    99.                         break;
    100.                     }
    101.                 }
    102.             }
    103.             Debug.DrawRay(pedestrians[i].current_position, pedestrians[i].local_target - pedestrians[i].current_position, Color.blue);
    104.         }
    105.  
    106.         //---------------------------------------------------------------------------------------------------------------------------------------------------------------------------//
    107.         //Creates the job
    108.         NativeArray<float3> FirstForces = new NativeArray<float3>(pedestrians.Length, Allocator.TempJob);
    109.         NativeArray<float> deltaTime = new NativeArray<float>(1, Allocator.TempJob);
    110.         deltaTime[0] = Time.deltaTime;
    111.  
    112.         FirstForceJob firstjob = new FirstForceJob();
    113.         firstjob.pedestrians = pedestrians;
    114.         firstjob.First_forces = FirstForces;
    115.         firstjob.deltaTime = deltaTime;
    116.  
    117.         JobHandle firsthandle = firstjob.Schedule<FirstForceJob>(pedestrians.Length, 3);
    118.  
    119.         firsthandle.Complete();
    120.         pedestrians.Dispose();
    121.         deltaTime.Dispose();
    122.  
    123.  
    124.         //Saves Last Position for every pedestrian
    125.         foreach (GameObject p in pedestriansGB)
    126.         {
    127.             p.GetComponent<Job_Data>().Last_position = p.transform.position;
    128.         }
    129.  
    130.  
    131.  
    132.         //Applies Forces to pedestrians
    133.         for (int i = 0; i < pedestriansGB.Length; i++)
    134.         {
    135.             if (!math.Equals(new float3(-Mathf.Infinity, -Mathf.Infinity, -Mathf.Infinity), FirstForces[i]))
    136.             {
    137.                 pedestriansGB[i].GetComponent<Rigidbody>().AddForce(FirstForces[i]);
    138.             }
    139.         }
    140.         FirstForces.Dispose();
    141.     }
    142.  
    143. }
    This is the main code which first collects the data from pedestrians and the stores that data in NativeArrays and after that creates and schedules the jobs. Each pedestrian has a small script which stores data for the individual pedestrian. The main script is accessings these small scripts.

    This is the code for the job and the struct used in the NativeArray above:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.AI;
    4. using Unity.Jobs;
    5. using Unity.Collections;
    6. using Unity.Mathematics;
    7. using Unity.Burst;
    8.  
    9. public class Pedestrian_Job_Archive
    10. {
    11.  
    12. }
    13.  
    14.  
    15. public struct PedestrianForJob
    16. {
    17.     public float3 current_position;
    18.     public float3 last_position;
    19.     public float3 target;
    20.     public float A;
    21.     public float B;
    22.     public float k;
    23.     public float kappa;
    24.     public float desired_velocity;
    25.     public float panic;
    26.     public float3 local_target;
    27.     public float mass;
    28.  
    29.     public PedestrianForJob(float3 _current_position, float3 _last_position, float3 _target, float _A, float _B, float _k, float _kappa, float _desired_velocity, float _panic, float3 _local_target, float _mass)
    30.     {
    31.         current_position = _current_position;
    32.         last_position = _last_position;
    33.         target = _target;
    34.         A = _A;
    35.         B = _B;
    36.         k = _k;
    37.         kappa = _kappa;
    38.         desired_velocity = _desired_velocity;
    39.         local_target = _local_target;
    40.         panic = _panic;
    41.         mass = _mass;
    42.     }
    43. }
    44. [BurstCompile]
    45. public struct FirstForceJob : IJobParallelFor
    46. {
    47.  
    48.     [ReadOnly] public NativeArray<PedestrianForJob> pedestrians;
    49.     [ReadOnly] public NativeArray<float> deltaTime;
    50.  
    51.     [WriteOnly] public NativeArray<float3> First_forces;
    52.     public void Execute(int index)
    53.     {
    54.         float3 avarage_direction = new float3(0f, 0f, 0f);
    55.         int m = 0;
    56.         for (int i = 0; i < pedestrians.Length; i++)
    57.         {
    58.             //Makes sure the avarage direction does not include the currently selected pedestrian
    59.             if (index != i)
    60.             {
    61.                 avarage_direction += pedestrians[i].current_position - pedestrians[i].last_position;
    62.                 m++;
    63.             }
    64.         }
    65.         avarage_direction = avarage_direction / m;
    66.         float3 direction = math.normalize((1f - pedestrians[index].panic) * (pedestrians[index].local_target - pedestrians[index].current_position) + pedestrians[index].panic * avarage_direction);
    67.         float3 current_velocity = (pedestrians[index].current_position - pedestrians[index].last_position) / deltaTime[0];
    68.  
    69.         float3 first_force = (pedestrians[index].mass * pedestrians[index].desired_velocity * direction - current_velocity) / 0.5f;
    70.         First_forces[index] = first_force;
    71.        
    72.     }
    73. }
    I hope someone can help me.