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

Memory leak investigation help

    • Type: Icon: Task Task
    • Resolution: Done
    • Priority: Icon: Critical - P2 Critical - P2
    • None
    • Affects Version/s: 2.2.4
    • Component/s: API, Serialization
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      We are running mongo version 3.0.7 on Amazon Linux AMI. We have a background windows service running on Windows server 2012 that is connecting to mongodb using c# driver+legacy driver v 2.2.4.

      We have several cron jobs that the windows background service runs. Some jobs run at every 5 minutes, some every 30 sec. The background windows service is a long running service. What we are noticing is gradually the background windows service memory keeps increasing and we have to restart the service at some point before it hits the max memory on machine.

      We used dotMemory to profile the server and this is what we found was

      and when we navigate to the first object

      IConvention[]

      which has size of 2.06GB in Largest Size pie chart
      we are seeing this:

      Look at that it looks related to the Conventions. We are still investigating on our end but wanted to post it and see if any one is finding similar issue.

      Each of the job runs by opening a connection to MongoDB via

      Job1.cs
      // Some comments here
      public class Job1
      {
          public MongoContext Mongo = new MongoContext();
          public void Run()
          {
              var data=Mongo.SomeCollectionName.Find(somequery); 
              //Some business logic
          }
      }
      
      MongoContext.cs
      public class MongoContext
      {
          private readonly MongoServer _server;
          private readonly MongoDatabase _db;
          private readonly MongoClient _client;
      
          static MongoContext()
          {
              BsonSerializer.RegisterIdGenerator(
                  typeof(Guid),
                  CombGuidGenerator.Instance
                  );
      
              var serializer = new DateTimeSerializer(DateTimeKind.Local);
              BsonSerializer.RegisterSerializer(typeof(DateTime), serializer);
          }
      
          public MongoContext(string dbKey = "MongoDB")
          {
              ConventionRegistry.Register("Ignore null values",
                  new ConventionPack
                  {
                  new IgnoreIfNullConvention(true),
                  new IgnoreExtraElementsConvention(true)
                  },
                  t => true);
      
              var connectionString = GetConnectionString(dbKey);
              if (_client == null)
              {
                  _client = new MongoClient(connectionString);
              }
              _server = _client.GetServer();
              _db = _server.GetDatabase(db);
          }
      
          public MongoCollection<SomeCollectionName> SomeCollectionName => _db.GetCollection<SomeCollectionName>("SomeCollectionName");
      
          //Rest of 78 collections
      }
      

      Any help is really appreciated. Thanks

        1. memory.PNG
          memory.PNG
          41 kB
        2. LargestSize.PNG
          LargestSize.PNG
          39 kB

            Assignee:
            Unassigned Unassigned
            Reporter:
            hari.pulugurta Hari Pulugurta
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: