node icon indicating copy to clipboard operation
node copied to clipboard

Memory Leak on sample code async-hello-world

Open batobolg opened this issue 10 years ago • 16 comments

today I test this again on version node-v4.2.6 and node-v5.5.0 https://github.com/nodejs/node-v0.x-archive/issues/8255

it still leaking.

batobolg avatar Feb 01 '16 19:02 batobolg

Source code from the original report (based on node-v0.10.31/test/addons/async-hello-world/binding.cc and node-v0.10.31/test/addons/async-hello-world/test.js):

var assert = require('assert');
var binding = require('./build/Release/binding');
var called = false;

process.on('exit', function () {
  assert(called);
});

binding(5, function (err, val) {
  assert.equal(null, err);
  assert.equal(10, val);
  process.nextTick(function () {
    called = true;
  });
});

var http = require("http");
var server = http.createServer();
server.on("request", function(req, res) {
        if (req.url === '/favicon.ico') {
                return;
        }
        res.writeHead(200, {'Content-Type': 'text/plain'});
        res.write("hello world !!");

binding(5, function (err, val) {
  assert.equal(null, err);
  assert.equal(10, val);
  process.nextTick(function () {
    called = true;
  });
});
        res.end();
        console.log("call ----> 3?");
        //console.log(req);
});
server.listen(8090);
console.log("server start 8090");

ChALkeR avatar Feb 02 '16 11:02 ChALkeR

@batobolg What makes you think that this is a memory leak? (Note: I have not run it yet, just want to know what do you observe). With numbers for the current version of Node.js, if possible.

ChALkeR avatar Feb 02 '16 11:02 ChALkeR

after million times apache bench. ab command like this ab -c 100 -n 1000000 http://server:8090/ the memory increase we can confirm it by top or ps at RSS column.

batobolg avatar Feb 02 '16 11:02 batobolg

version is node-v4.2.6 and node-v5.5.0 download from nodejs site.

batobolg avatar Feb 02 '16 11:02 batobolg

I just added this part to original code

var http = require("http");
var server = http.createServer();
... ... ... ...
server.listen(8090);
console.log("server start 8090");

batobolg avatar Feb 02 '16 11:02 batobolg

Reproduced with node v4.2.4:

# Right after starting the server process
$ top -n 1 -b | grep 'COMMAND\|node'
  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
18530 dc        20   0  807672  23940  16088 S   0.0  0.1   0:00.06 node

# After running `ab -c 100 -n 1000000 http://localhost:8090/`
  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
18530 dc        20   0 1361908 479804  16280 S   0.0  2.9   1:04.47 node

# After running `ab` again
  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
18530 dc        20   0 1765124 882320  15940 S   0.0  5.4   2:08.59 node

So the memory usage (RES) went from 23MB to 480MB to 880MB. It looks like the server is leaking about 400 bytes for every request .

dcposch avatar Feb 03 '16 01:02 dcposch

Haha well I just added some printfs and it looks like new async_req is called for every request, and the corresponding delete is never called.

https://github.com/nodejs/node-v0.x-archive/blob/feature/openssl-1.0.1f-in-v.08/test/addons/async-hello-world/binding.cc#L44

dcposch avatar Feb 03 '16 01:02 dcposch

@ChALkeR actually this is a troll issue.

The async_req objects are created and freed correctly -- it's just that they're created quickly (eg you can create a million of them with ab -n 1000000) and freed very slowly.

CHeck out the call to sleep(1) here: https://github.com/nodejs/node-v0.x-archive/blob/feature/openssl-1.0.1f-in-v.08/test/addons/async-hello-world/binding.cc#L18

So what this test does is:

  • Accept HTTP requests
  • Answer each request immediately, but queue up a simulated "CPU intensive" job
  • Queue grows without bound, can be millions of jobs long
  • Jobs each take a second to complete and UV_THREADPOOL_SIZE is only 4 by default, so the queue shrinks by about 4 / second

This is not a memory leak, just a silly test.

dcposch avatar Feb 03 '16 02:02 dcposch

CC @indutny

dcposch avatar Feb 03 '16 02:02 dcposch

Changing the JS above to actually wait for the simulated job to finish before sending HTTP response. This fixes the unbounded queue problem, and makes the requests take ~3000ms each instead of ~5ms each.

var assert = require('assert');
var binding = require('./build/Release/binding');
var http = require("http");
var called = false;

process.on('exit', function () {
  assert(called);
});

binding(5, function (err, val) {
  assert.equal(null, err);
  assert.equal(10, val);
  process.nextTick(function () {
    called = true;
  });
});

var server = http.createServer();
server.on("request", function(req, res) {
  if (req.url === '/favicon.ico') {
    return;
  }

  binding(5, function (err, val) {
    assert.equal(null, err);
    assert.equal(10, val);
    process.nextTick(function () {
      called = true;
    });

    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.write("hello world !!");
    res.end();
  });
});
server.listen(8090);
console.log("server start 8090");

dcposch avatar Feb 03 '16 02:02 dcposch

you mean if there is no sleep(1) queue shrinks quickly ?

batobolg avatar Feb 03 '16 02:02 batobolg

our async job can't long then 1 second ?

batobolg avatar Feb 03 '16 03:02 batobolg

with sleep(1) after ab I waited quite a while it don't shrink up the queue.

batobolg avatar Feb 03 '16 08:02 batobolg

with sleep(1) after ab I waited quite a while it don't shrink up the queue.

@batobolg it does, but so slowly that you need to add logging to see it. if you run ab -n 1000000 then you'd have to wait 250k seconds = about three days for memory use to go from ~800MB back to ~20MB

i think we can close this issue. it's not a bug in node, just an old test case (which isn't even in the codebase anymore as far as i can tell) that was doing something silly (simulating "cpu-intensive" jobs, adding millions of them to an unbounded work queue)

dcposch avatar Feb 04 '16 02:02 dcposch

OK, It may be a silly test, but we care about memory for the long run. I will wait 3 days for this. after 3 days I well check it going to half or more. by the way now is

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 5343 vagrant 20 0 1237772 327164 8784 S 0.0 32.2 5:48.03 node test.js

RES is 327164

batobolg avatar Feb 04 '16 03:02 batobolg

few days, now top command shows, PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 6596 vagrant 20 0 1807632 696452 864 S 0.3 68.4 19:57.22 node test.js 5343 vagrant 20 0 1300552 107560 120 S 0.0 10.6 8:00.94 node test.js

RES from 327164 to 107560 even few hour later

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 6596 vagrant 20 0 1828056 720812 900 S 0.0 70.8 20:03.44 node 5343 vagrant 20 0 1300552 95220 116 S 0.0 9.4 8:00.94 node

RES to 95220

but new one PID is 6596 is little bit increase some, may be after some days it will be OK. I close this issue. thank you. thank you @dcposch

batobolg avatar Feb 09 '16 05:02 batobolg