Search Unity

Bug Memory leak in NavmeshAgent.SetDestination if multiple agents in the same position

Discussion in 'Navigation' started by InteractivePie, Feb 20, 2022.

  1. InteractivePie

    InteractivePie

    Joined:
    May 2, 2014
    Posts:
    18
    I found out that my game will eventually run out of memory on a dedicated Linux server with 1GB of ram and some SWAP after a few days (can also confirm endless increasing of memory usage on my local Windows machine. Included project that reproduces the issue. Few MBs per minute added).

    After using the memory profiler, it seems the problem is in the following wandering AI script: I can confirm that commenting out UserAgent.SetDestination will not make the memory increase.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.AI;
    3. public class WanderingAI : MonoBehaviour
    4. {
    5.  
    6.     public float wanderRadius;
    7.     public float wanderTimer;
    8.  
    9.     protected Vector3 basePosition;
    10.     protected NavMeshAgent agent;
    11.     protected float timer;
    12.  
    13.     void Start()
    14.     {
    15.         basePosition = gameObject.transform.position;
    16.         agent = GetComponent<NavMeshAgent>();
    17.         timer = wanderTimer;
    18.     }
    19.  
    20.     // Update is called once per frame
    21.     void Update()
    22.     {
    23.         timer += Time.deltaTime;
    24.  
    25.         if (timer >= wanderTimer)
    26.         {
    27.             Vector3 newPos = RandomNavSphere(basePosition, wanderRadius, -1);
    28.             agent.SetDestination(newPos); // If this line is disabled. Other native memory (in use / reserved) is not slowly increasing
    29.             timer = 0;
    30.         }
    31.     }
    32.  
    33.     private Vector3 RandomNavSphere(Vector3 origin, float dist, int layermask)
    34.     {
    35.         Vector3 randDirection = Random.insideUnitSphere * dist;
    36.  
    37.         randDirection += origin;
    38.  
    39.         NavMeshHit navHit;
    40.  
    41.         NavMesh.SamplePosition(randDirection, out navHit, dist, layermask);
    42.  
    43.         return navHit.position;
    44.     }
    45. }
    Running this code for a while (few days) will fill up the Other Native Memory. Please see the extreme example in the attachments. its using 8 GB of native memory, while only using around 100MB ram for the rest. This increasing of memory seems to happen if many agents are in one position.

    Is this a bug in Unity or in my script? This is tested on the latests LTS release. Included a zip file that reproduces the issue in a clean project
     

    Attached Files:

    DwinTeimlon likes this.
  2. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,451
    Hello,
    Thank you for the detailed analysis and description. Just from the description alone this very much sounds like a bug within the Engine.

    There are some possibly related accounts of path searches getting stuck with pathPending == true when multiple agents target the same spot or one occupied by an agent, so that sounds like the native memory for the path calculation might get leaked if a new destination is set before the old calculation finishes?
    I expect that if you compare two snapshots taken a bit apart during the same session, and check out the "Diff All Native Allocations" table on the Objects and Allocations page of the Memory Profiler, that some Navmesh related allocations should show up as "New".

    Either way, it'd be much appreciated if you could file a bug report with the information you provided here, and a link to this thread, via the Editor menu entry
    Help > Report a Bug
    :)

    And you might try a work around of it by not setting a new destination before the path is calculated. It might cause all agents to get stuck over time but maybe it won't and then you A) have a workaround of sorts until it's fixed, and B) we'd have confirmation for that theory that could help find an fix the bug itself :)
     
  3. InteractivePie

    InteractivePie

    Joined:
    May 2, 2014
    Posts:
    18
    Hi @MartinTilo,

    I reported a bug. Will try to use your workaround.

    using the example in the attachment I let it run for a few minutes and these are the results comparing snapshots: upload_2022-2-20_16-45-33.png

    Not 100% sure where to look at TBH. Its filtered on New and Diff all Native allocations but looks like this only shows the memory region and not if its from NavMesh

     
    DwinTeimlon and MartinTilo like this.
  4. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,451
    That also narrows it down though, thank you for checking that. And thank you for reporting the bug.

    Did you already get an email reply with an issue ID, I don't need the link, just the number so I can forward it to the right team to hopefully get things moving quicker :)
     
    Last edited: Feb 20, 2022
  5. InteractivePie

    InteractivePie

    Joined:
    May 2, 2014
    Posts:
    18
    I did get a case id. Its 1404634. If I receive an issue ID I will post it here @MartinTilo
     
    MartinTilo likes this.
  6. InteractivePie

    InteractivePie

    Joined:
    May 2, 2014
    Posts:
    18
    Hi @MartinTilo ,
    I did not get an answer yet. Do you know how much time this usually takes?

    Also I tried your approach
    Code (CSharp):
    1. if (timer >= wanderTimer)
    2. {
    3.     Vector3 newPos = RandomNavSphere(basePosition, wanderRadius, -1);
    4.     if (!agent.pathPending) {  
    5.       agent.SetDestination(newPos); // If this line is disabled. Other native memory (in use / reserved) is not slowly increasing
    6.     }
    7.     timer = 0;
    8. }
    However in the example project it looks like the memory still increases
     
  7. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,451
    It depends but I think somewhere between 1-2 weeks?
    It depends a bit on the current load, product area (how many people in our Customer QA Team might be able to do the repo based on e.g. platform or accuracy& details of the repro steps), how close to latest versions the report was send in from...

    The team is already informed, on the lookout and subscribed to the issue but waiting for the first confirmation and analysis from our Customer QA Team.
     
    InteractivePie likes this.
  8. InteractivePie

    InteractivePie

    Joined:
    May 2, 2014
    Posts:
    18
    Hi @MartinTilo,

    Is there any progress on the ticket? I did not get a reply yet
     
    DwinTeimlon and lclemens like this.
  9. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,451
    There wasn't yet no, but I've asked our QA to take a look at it. Hopefully that'll get it going soon :)
     
    DwinTeimlon likes this.
  10. InteractivePie

    InteractivePie

    Joined:
    May 2, 2014
    Posts:
    18
    MartinTilo and DwinTeimlon like this.