Optimize Memory Allocation in Critical Code Paths

XMLWordPrintableJSON

    • Type: Improvement
    • Resolution: Duplicate
    • Priority: Unknown
    • None
    • Affects Version/s: None
    • Component/s: None
    • None
    • Dotnet Drivers
    • None
    • None
    • None
    • None
    • None
    • None

      We encountered significant memory allocation and GC pressure while using the mongo-csharp-driver (v2.25.0). BSON binary serialization in the driver has become a memory allocation hotspot on our server. Below are the memory allocation metrics captured via dotTrace over 5 minutes:
       

      Type/Class Memory Allocation Percentage
      MongoDB.Bson.IO.BsonBinaryWriterContext 29,909 MB 10.6%
      Systernint32 20,838 MB 7.4%
      Aki.Define.EntityDbData 15,622 MB 5.5%
      System.String 11,642 MB 4.1%
      Enumerator[MongoDB.Bson.Serialization.BsonMemberMap] 10,250 MB 3.6%
      System.Byte[] 9,650 MB 3.4%
      Systernint32n 8,805 MB 3.1%
      System.Boolean 6,816 MB 2.4%
      Entry[System.String,Serilog.Events.LogEventPropertyValue[] 5,561 MB 2.0%
      System.Char[] 5,306 MB 1.9%

      BsonBinaryWriterContext

      BsonBinaryWriter.WriteStartDocument
      https://github.com/mongodb/mongo-csharp-driver/blob/46eafc9d64a7f2147b49046f51e7fdd28c7e391f/src/MongoDB.Bson/IO/BsonBinaryWriter.cs#L638
      BsonBinaryWriter.WriteStartArray
      https://github.com/mongodb/mongo-csharp-driver/blob/46eafc9d64a7f2147b49046f51e7fdd28c7e391f/src/MongoDB.Bson/IO/BsonBinaryWriter.cs#L614

      System.Int32

      boxing allocations
      https://github.com/mongodb/mongo-csharp-driver/blob/46eafc9d64a7f2147b49046f51e7fdd28c7e391f/src/MongoDB.Bson/Serialization/Serializers/BsonClassMapSerializer.cs#L629

      Enumerator

      Iterator boxing
      https://github.com/mongodb/mongo-csharp-driver/blob/46eafc9d64a7f2147b49046f51e7fdd28c7e391f/src/MongoDB.Bson/Serialization/Serializers/BsonClassMapSerializer.cs#L573
      https://github.com/mongodb/mongo-csharp-driver/blob/46eafc9d64a7f2147b49046f51e7fdd28c7e391f/src/MongoDB.Bson/Serialization/Serializers/BsonClassMapSerializer.cs#L339

      System.Byte[]

      86% of the System.Byte[] allocations are generated by the MongoDB C# driver
      https://github.com/mongodb/mongo-csharp-driver/blob/46eafc9d64a7f2147b49046f51e7fdd28c7e391f/src/MongoDB.Bson/IO/ArrayElementNameAccelerator.cs#L71

      System.Boolean

      https://github.com/mongodb/mongo-csharp-driver/blob/46eafc9d64a7f2147b49046f51e7fdd28c7e391f/src/MongoDB.Bson/IO/BsonStreamAdapter.cs#L518

      System.Boolean

      Boxing allocations.

      color: Color value is invalid

      Same root cause as System.Int32.

      Does the driver development team have an optimisation plan for memory allocation?

        1. BsonAlloc.zip
          2 kB
        2. Snipaste_2025-03-21_15-02-16.png
          Snipaste_2025-03-21_15-02-16.png
          113 kB

            Assignee:
            Boris Dogadov
            Reporter:
            gary chan
            Votes:
            1 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: