Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-4971

C# driver memory leak in Unity

    • Type: Icon: Bug Bug
    • Resolution: Unresolved
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: 2.18.0, 2.22.0, 2.23.0
    • Component/s: None
    • None
    • None
    • Dotnet Drivers
    • None
    • None
    • None
    • None
    • None
    • None

      Created from this Community Forums post.

      We are trying to use Mongo c# driver (2.18, 2.22 and 2.23 were tested) in our Unity project (2022.3.10f1), but creating MongoClient instance causes memory leak

      By experimenting I found out that speed of leaking depends on heartbeat interval settings, and it reproduces for MultiServerCluster only.

      Also amount of read/write requests does not matter. Just creating connection causes constant leak.

      Here is memory usage graph:

      We are using C# driver for Unity server and build using “Dedicated Server” with target platform “Linux” (also windows was tested, both leaks).

      Empty project is enough to observe the leak. This is only class I had for the experiments:

      public class DriverTest : MonoBehaviour
          {
              const string ConnectionString = "mongodb://localhost:27017,localhost:27019/?replicaSet=poolReplSet";
      
              void Awake()
              {
                  Debug.Log("Initializing...");
      
                  var mongoClient = new MongoClient(MongoClientSettings.FromConnectionString(ConnectionString));
      
                  Debug.Log("Initialized!");
              }
          }
      

      GitHub: GitHub - Raildoc1/MongoDriverTest

      I tried using various connection strings:
      “mongodb://localhost:27017” - does not leak
      “mongodb://localhost:27017,localhost:27019” - leaks
      “mongodb://localhost:27017,localhost:27019/?replicaSet=poolReplSet” - leaks
      2.18, 2.22 and 2.23 - were the only versions I tested

      I compiled for JIT. Not sure if I can use AOT: when I try to build using AOT it says:

      Error NETSDK1207 : Ahead-of-time compilation is not supported for the target framework.
      

            Assignee:
            Unassigned Unassigned
            Reporter:
            james.kovacs@mongodb.com James Kovacs
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: