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

MongoServer instance leaking memory

    • Type: Icon: Bug Bug
    • Resolution: Won't Fix
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 2.4.3
    • Component/s: Configuration
    • None

      While updating an older application to the latest driver, I needed to implement a method which took a connection string and returned a MongoDatabase instance.
      I ended up with this implementation:

      var url = MongoUrl.Create( connectionString );
      var serverSettings = MongoServerSettings.FromUrl( url );
      var s = new MongoServer( serverSettings );
      var db = s.GetDatabase( url.DatabaseName );
      return db;

      However, after deploying the application to production, we discovered that it was slowly leaking memory. After some investigation, I found that after calling this method 500 times, a number of objects had been created, which would not be garbage collected:

      MongoServer is not disposable and I cannot find any references in the documentation about how to "clean up".
      I am now using this implementation, which does not seem to leak:

      var url = MongoUrl.Create( connectionString );
      var client = new MongoClient(url);
      var s = client.GetServer();
      var db = s.GetDatabase( url.DatabaseName );
      return db;

      However, GetServer() is deprecated, so I expect it will go away at some point.
      I realize that I "should" be using IMongoDatabase instead of MongoDatabase so I could just use client.GetDatabase(...) but this is a large code base, and the API of IMongoDatabase is radically different (and worse IMHO).

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            tmh@fundconnect.com Thomas Hjorslev
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: