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

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.

using UnityEngine;
using UnityEngine.AI;
public class WanderingAI : MonoBehaviour
{

    public float wanderRadius;
    public float wanderTimer;

    protected Vector3 basePosition;
    protected NavMeshAgent agent;
    protected float timer;

    void Start()
    {
        basePosition = gameObject.transform.position;
        agent = GetComponent<NavMeshAgent>();
        timer = wanderTimer;
    }

    // Update is called once per frame
    void Update()
    {
        timer += Time.deltaTime;

        if (timer >= wanderTimer)
        {
            Vector3 newPos = RandomNavSphere(basePosition, wanderRadius, -1);
            agent.SetDestination(newPos); // If this line is disabled. Other native memory (in use / reserved) is not slowly increasing
            timer = 0;
        }
    }

    private Vector3 RandomNavSphere(Vector3 origin, float dist, int layermask)
    {
        Vector3 randDirection = Random.insideUnitSphere * dist;

        randDirection += origin;

        NavMeshHit navHit;

        NavMesh.SamplePosition(randDirection, out navHit, dist, layermask);

        return navHit.position;
    }
}

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


7911538–1008970–memory-leak-unity-master.zip (64.7 KB)

1 Like

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 :slight_smile:

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 :slight_smile:

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:7911835--1009042--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

2 Likes

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 :slight_smile:

I did get a case id. Its 1404634. If I receive an issue ID I will post it here @MartinTilo

1 Like

Hi @MartinTilo ,
I did not get an answer yet. Do you know how much time this usually takes?

Also I tried your approach

if (timer >= wanderTimer)
{
    Vector3 newPos = RandomNavSphere(basePosition, wanderRadius, -1);
    if (!agent.pathPending) {  
      agent.SetDestination(newPos); // If this line is disabled. Other native memory (in use / reserved) is not slowly increasing
    }
    timer = 0;
}

However in the example project it looks like the memory still increases

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.

1 Like

Hi @MartinTilo ,

Is there any progress on the ticket? I did not get a reply yet

2 Likes

There wasn’t yet no, but I’ve asked our QA to take a look at it. Hopefully that’ll get it going soon :slight_smile:

1 Like

@MartinTilo Thanks for moving this forward. I got a message that this issue is now added to the issue tracker and the QA team successfully reproduced this issue. Its added here Unity Issue Tracker - Memory leak in NavmeshAgent.SetDestination method when multiple agents are in the same position

2 Likes