Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"complete" callback of Database.each() leaking memory? #297

Closed
thierer opened this issue May 25, 2014 · 3 comments · Fixed by #307
Closed

"complete" callback of Database.each() leaking memory? #297

thierer opened this issue May 25, 2014 · 3 comments · Fixed by #307

Comments

@thierer
Copy link
Contributor

thierer commented May 25, 2014

I've got a memory leak in the complete callback of Database.each() that I can't explain.

This is running node-sqlite3 2.2.3 and node 0.10.22 on Linux 3.12.13 x86_64.

The following stripped down example shows the behaviour:

var sqlite3 = require('sqlite3');

function doLeak(callback) {
  var leak = new Buffer(1000000);

  db.each('SELECT 1',
    function item(err, row) {
    },
    function complete(err, found) {
      callback(leak);
    }
  );
}

var db = new sqlite3.Database(':memory:', sqlite3.OPEN_READONLY);
db.once('open', function(err) {
  if (err) return console.error(err);

  var active = 0;

  (function callLeak() {
    active++;
    doLeak(function() {
      console.log(--active);
      callLeak();
    });
  })();
});

The Buffer allocation is just there to increase the effect. If I use heapdump to take snapshots, I can see the "complete" functions pile up in the "Containment" view of Devtools. (Note that it only seems to affect the completeand not the item callback).

I'm not sure if it's really a bug or just my lack of understanding javascript / node.js / v8 / whatever.

OTOH, I don't see any leaks if I replace the sqlite3 query and use the same call pattern with fs.readFile(), which I would consider a comparable situation?

var fs = require('fs');

function doLeak(callback) {
  var leak = new Buffer(1000000);

  fs.readFile('test.txt',
    function complete(err, data) {
      callback(leak);
    }
  );
}

var active = 0;

(function callLeak() {
  active++;
  doLeak(function() {
    console.log(--active);
    callLeak();
  });
})();

Any help would be appreciated.

@springmeyer
Copy link
Contributor

thanks for the report and for providing a patch. I'm busy with other projects but I will try to test when I have time. One think to try: run the test with node --expose-gc and then call gc() to force garbage collection during your script. Then watch memory - this is to make sure you are not just seeing a lag in garbage collection and thinking its a leak.

@thierer
Copy link
Contributor Author

thierer commented May 29, 2014

I did that. Same result: Virtual memory going through the roof (without the fix).

@springmeyer
Copy link
Contributor

thanks. another thing I'll check is if it is also present with node v0.11.x and #184. If you have time to test that too I'd be grateful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants