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