[CSHARP-3054] C# driver memory leak Created: 10/Apr/20  Updated: 27/Oct/23  Resolved: 13/Apr/20

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: 2.10.3
Fix Version/s: None

Type: Task Priority: Major - P3
Reporter: Christopher Eastwood Assignee: Robert Stam
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Windows 10 x64


Attachments: PNG File bsonChunkPool_example.PNG    

 Description   

Memory leak in the MongoDB .NET driver.

Our team has noticed some memory issues in our application. We have been performing some memory profiling tests and have noticed that after a large query the BsonChunkPool object will stay around without being GC. 

I've created a sample app to demonstrate the issue. Once you run the app, use the seed data button, and then fetch the data. If you have a memory profiler running you should see that the BsonChunkPool is not deleted. https://github.com/Beastw00d/MongoWebApp

We have been using dotMemory as our memory profiler. I've attached an image of the profiler from the demo app. The BsonChunkPool takes up 7.88 MB (top right of image). In our real app it takes 35 MB and up.

 



 Comments   
Comment by 대협 신조 [ 03/May/21 ]

Thankyou very much

Comment by Christopher Eastwood [ 14/Apr/20 ]

Thanks for the info @robert!

Comment by Robert Stam [ 13/Apr/20 ]

The `BsonChunkPool` exists so that large memory buffers (chunks) can be reused, thus easing the amount of work the garbage collector has to do.

Initially the pool is empty, but as buffers are returned to the pool the pool is no longer empty. Whatever memory is held in the pool will not be garbage collected. This is by design. That memory is intended to be reused. This is not a memory leak.

The default configuration of the `BsonChunkPool` is such that it can hold a maximum of 8192 chunks of 64KB each, so if the pool were to grow to its maximum size it would use 512MB of memory (even more than the 7 or 35 MB you are observing).

If for some reason you don't want the `BsonChunkPool` to use  that much memory, you can configure it differently by putting the following statement at the beginning of your application:

BsonChunkPool.Default = new BsonChunkPool(16, 64 * 1024); // e.g. max 16 chunks of 64KB each, for a total of 1MB

We haven't experimented with different values for chunk counts and sizes so if you do decide to change the default `BsonChunkPool` configuration you should do some benchmarking and verify that it doesn't have an adverse impact on your performance.

 

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