question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

possible memory leak with #connection.useDb

See original GitHub issue

Hello guys,

In my work, I need to switch through various different databases. I faced a memory leak warning when using .useDb several times with the same dabatabase name.

The following code demostrates this issue.

var mongoose = require('mongoose');

var db1 = mongoose.createConnection('mongodb://localhost/my_database');

for (var i = 0; i <= 11; i++){
  var db2 = db1.useDb('test')
}
$ node -v 
v0.10.28

mongoose version - latest (3.8.9)

Possible fix

I could fix it locally, by looping through db1.otherDbs object, and returning the connection if it already exists, otherwise it triggers the memory leak. Maybe , useDb function should return an existing db instance, if it already exists in the caller caller_db.otherDbs ? If so, I can pull request that.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:18 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
bmpccommented, May 5, 2020

Hi @vkarpov15,

Meanwhile, we have identified the “real” cause of our leaks. It was related to the mongoose connections but they were not the root cause of the issue.

It all comes down to Domains in nodejs and the following:

If domains are in use, then all new EventEmitter objects (including Stream objects, requests, responses, etc.) will be implicitly bound to the active domain at the time of their creation. from https://nodejs.org/api/domain.html#domain_implicit_binding

We were using “Raven” to send logs to Sentry. As it happens, this module was creating a domain for each request in which all the request information was being tied to the domain object.

Because Mongoose connections inherit from EventEmitter, they were also using the same Domain object. Therefore, because all Mongoose connections are cached, the domain and consequent request information was never collected.

We have removed the “Raven” module for now, and memory is now controlled.

Either way, thank you for considering this. I had also noticed that there was some “connection state” that was being propagated across all Mongoose Connections and I was not sure if we could simply remove BDs from memory.

Best Regards, Bruno.

0reactions
vkarpov15commented, May 13, 2020

Thanks for the detailed explanation @bmpc and I’m glad you found the issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

possible memory leak due to DB close - node.js - Stack Overflow
Problem - you're creating a new connection every time. Solution - connect once and cache the connection. Option 1
Read more >
How to fix a memory leak when switching between databases ...
I've identified a memory leak in an application I'm working on, which causes it to crash after a while due to being out...
Read more >
Possible memory leak if the Oracle JDBC-OCI driver is ... - IBM
There is a possible memory leak if the Oracle JDBC-OCI driver is used when enabling WebSphere Application Server connection pooling.
Read more >
Hunting a Ghost - Finding a Memory Leak in Node.js
Finding a Node.js memory leak can be quite challenging - we compiled a bunch of methods and tools that could help.
Read more >
Troubleshooting Node.js Memory Use - Heroku Dev Center
When your Node application uses more memory than is available on the ... If a memory leak is small and only happens on...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found