I use mongodb for my Discord Bot, and just recently I had an issue where the the process running the driver would keep growing in memory size. This is a new problem as far as I can tell, or at least it hadn’t caused any isses before this. I looked at heap dumps, at it appears that there are many many objects and closures being created in a denque inside the ConnectionPool object. When looking at all of these objects inside the denque, it appears that every one only has one method, callback
. This has another function in its context (like a scope from what I understand) called callback, which is an instance of callbackWithRetry. This chain continues far down the line until eventually the only things left inside its context are a resolve() and reject() method. There are thousands of these callback chains, each taking up around 4 KB of memory.
Tracing down the line, here’s what each level of callback nesting has that might help identify what each is called for.
- Context has a this variable which is the
ConnectionPool
(seems to be the class scope) - Context has variables named
topology
,inTransaction
,operation
, andserverSelectionOptions
- Context has variables named
owner
,session
, andoperation
- Context has a
this
variable set to aCursor
, as well as acursor
variable also referencing the sameCursor
. This context also has acallback
as well as aqueryCallback
- Context has a variable
self
set to that sameCursor
, with no other variables exceptcallback
- Context has variable
cursor
set to that sameCursor
- Empty except for
callback
variable - Empty except for
callback
variable - Context has variables named
owner
,session
, andoperation
(reference different variables than level 3) - Context has
resolve
andreject
variables and nothing else (nocallback
variable, end of chain)
I do not know whether this is normal operation or not, but I found it very interesting that there would be such a long chain of callbacks, and so many of them all put together. The process just keeps taking memory until it’s killed. Does anyone have any insights as to what’s going on here, and how it can be mitigated or fixed?