christkv/node-mongodb-native — Issue #367: memory leak when inserting
Author: joachimkainz
Created: 2011-10-09 21:21:55+00:00
State: open

There is a memory leak when inserting. It is small, but it becomes a problem quickly when inserting a large number of bigger documents. Here is how this can be reproduced easily:

```
var mongo = require("mongodb");
var Server = mongo.Server;
var Db = mongo.Db;

function insert(collection) {
var doc = {};
for(var i = 0; i < 100; i++) {
doc['timestamp' + i] = Date.now();
}
var docs = [];
for(var i = 0; i < 100; i++) {
docs.push(doc);
}
collection.insert(docs);
}

function insert1000(callback) {
var db = new Db('leak', new Server("localhost", 27017, {}), {native_parser:true});

db.open(function(err, db) {
for(var i=0; i<1000; i++) {
db.collection('test', function(err, collection) {
insert(collection, function(err, doc) {
if (err) {
throw err;
}
});
});
}

db.close(function(){
callback();
});
});
}

function endlessInsert() {
insert1000(function(){
console.log("inserted 1000");
endlessInsert();
});
}

endlessInsert();
```