Persistent Memory Leak with A* Pathfinding Pro Using Recast/Graph System in Unity VR Game

  • A* version: [5.3.3]
  • Unity version: [2022.3.55f1]

Hi everyone,

I’m working on a VR project in Unity (using A* Pathfinding Project Pro) and have been running into persistent memory leaks related to pathfinding, particularly with Recast Graphs. The leaks seem to occur during scene transitions and gameplay, especially when enemies are actively chasing the player.

My Setup:

  • Unity Version: 2022.3.55f1
  • A* Pathfinding Version: Pro version (latest update)
  • VR Framework: Unity XR Toolkit (using Valve Index for testing)
  • Pathfinding Graph: Recast Graph (planning to use procedural generation in the future)
  • Enemy Movement: Custom EnemyAI script using Seeker and Path components for navigation.

Issue Details:

  • I’m getting persistent memory leaks from allocations related to Pathfinding.BinaryHeap, PathHandler, and PathProcessor.
  • The leaks often occur when the enemy requests paths frequently or when an enemy object is destroyed.
  • Using Unity’s Profiler, I’ve tracked consistent memory leaks with the following message:

A Native Collection has not been disposed, resulting in a memory leak.
Leak Detected: Persistent allocates multiple allocations from Pathfinding.BinaryHeap.cs

  • I also tried switching to a Grid Graph temporarily, but similar issues persisted.

What I’ve Tried:

  1. Ensured that paths are correctly claimed and released:
    if (path != null)
    {
    path.Release(this);
    }

  2. Cancelled ongoing path requests when stopping enemy movement or destroying the object.

  3. Disabled unnecessary threads via AstarPath settings.

  4. Verified that the Seeker component isn’t issuing multiple simultaneous requests.

Error Logs: Here’s a typical leak stack trace:

Found 1 leak(s) from callstack:
Pathfinding.BinaryHeap:.ctor (int) (at ./Packages/com.arongranberg.astar/Core/Collections/BinaryHeap.cs:94)
Pathfinding.PathHandler:.ctor (Pathfinding.GlobalNodeStorage,int,int) (at ./Packages/com.arongranberg.astar/Core/Pathfinding/PathHandler.cs:192)
Pathfinding.PathProcessor:SetThreadCount (int,bool) (at ./Packages/com.arongranberg.astar/Core/Pathfinding/PathProcessor.cs:99)
AstarPath:InitializePathProcessor () (at ./Packages/com.arongranberg.astar/Core/AstarPath.cs:1202)

Questions:

  1. Is there a best practice for managing path memory when using the Recast Graph with frequent enemy pathfinding requests?
  2. Would switching to a Grid Graph for static scenes help with memory management?
  3. Is there an internal method within A* Pathfinding Pro for clearing or disposing of old path data effectively?
  4. Are there specific Astar settings I should tweak for better memory management in a VR environment?

Any help or guidance on this would be greatly appreciated. If anyone has experienced similar issues with A* Pathfinding in Unity VR, I’d love to hear how you solved it.

Thanks so much for your time!

1 Like

Thanks for the detailed information! Can you also provide your Entities/Collections versions as well? I am not sure if those are “responsible” but they come up a lot so it could be useful information. Going to tag @aron_granberg on this leak. Thanks :smiley:

Thanks for the quick response and for tagging Aron! Here’s the information you requested:

I’m not currently using the Entities Package but my Collections version is below.

  • Collections Package Version: [1.5.1]

Let me know if there’s anything else you need from my end to help diagnose the issue. I appreciate all the help so far!

Thanks again!

This is documented on Pooling - A* Pathfinding Project. But you seem to handle that already.

Not really. Grid graphs typically use a lot more memory than recast graphs.

There is internal pooling, as mentioned above. But for disposing of them, that’s just left up to the GC.

This is definitely a bug, but I haven’t been able to replicate this. Can you consistently replicate this, even in the unity editor?