possible memory leak with #connection.useDb
See original GitHub issueHello 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:
- Created 9 years ago
- Comments:18 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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:
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.
Thanks for the detailed explanation @bmpc and I’m glad you found the issue.