[CSHARP-1769] Memory leak investigation help Created: 15/Sep/16  Updated: 05/Apr/19  Resolved: 16/Sep/16

Status: Closed
Project: C# Driver
Component/s: API, Serialization
Affects Version/s: 2.2.4
Fix Version/s: None

Type: Task Priority: Critical - P2
Reporter: Hari Pulugurta Assignee: Unassigned
Resolution: Done Votes: 0
Labels: question
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: PNG File LargestSize.PNG     PNG File memory.PNG    

 Description   

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



 Comments   
Comment by Robert Stam [ 16/Sep/16 ]

Thanks for letting us know you found the problem.

Comment by Hari Pulugurta [ 15/Sep/16 ]

Issue Resolved: The issue turns out to be an coding issue in our code base

Comment by Hari Pulugurta [ 15/Sep/16 ]

Correction:
Mongo version we are running is 3.2.9

Generated at Wed Feb 07 21:40:38 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.