Uploaded image for project: 'Node.js Driver'
  1. Node.js Driver
  2. NODE-5182

Memory leak in .findOne()

    • Type: Icon: Bug Bug
    • Resolution: Gone away
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: 4.14.0
    • Component/s: None
    • 3
    • Not Needed
    • Not Needed
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?

      What problem are you facing?

      When .findOne({ field: "value" }) is called repeatedly, there is a constant increase in memory usage.

      What driver and relevant dependency versions are you using

      Using mongodb from mongoose.

      • mongodb: (4.14.0)
      • mongoose: (6.7.0)
      • bullmq: (3.10.3)

      Steps to reproduce?

       

      import { Worker } from 'bullmq'
      import mongoose from 'mongoose'
      
      const client = await mongoose.connect(url)
      const collection = client.connection.db.collection('collectionName')
      new Worker('some-queue', async function() {
        const document = await collection.findOne({ field: "someValue" })
      })

       

       

      My research:

      Looking at the source code for .findOne() I see it is calling 

       

      .find().limit(-1).batchSize(1).next()

       

      However, in the mongodb documentation for .batchSize() it mentions to not to specify size of 1:
      https://www.mongodb.com/docs/manual/reference/method/cursor.batchSize/

      My temporary solution is to .find() without .batchSize(1):

       

      .find().limit(-1).next() // ".findOne()" without memory leak

      Memory usage with .findOne()

       

      With .find().limit(-1).next()

        1. image-2023-04-07-09-57-09-545.png
          image-2023-04-07-09-57-09-545.png
          101 kB
        2. image-2023-04-07-09-56-37-748.png
          image-2023-04-07-09-56-37-748.png
          126 kB
        3. image-2023-04-07-09-38-21-901.png
          image-2023-04-07-09-38-21-901.png
          29 kB
        4. image-2023-04-07-09-38-13-073.png
          image-2023-04-07-09-38-13-073.png
          35 kB
        5. image-2023-04-07-09-36-35-249.png
          image-2023-04-07-09-36-35-249.png
          29 kB

            Assignee:
            bailey.pearson@mongodb.com Bailey Pearson
            Reporter:
            steven.liu@cslico.com Steven Liu
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: