Node.js virtual memory is constantly increasing when using a child process

I use the spawn-child npm package to create a shell in which I run a binary file that was originally built in C ++. I provide Stdin to a binary, and then the binary will send Stdout continuously every second. On the node part, when I start getting Stdout from binary, I have a listener onthat will look something like stdout.on('data', function (data) {})where I send this data to the SSE channel.

Everything works fine, but the main problem is the constant memory growth of the node process, which I see when I hit the binary every time with Stdin. I outlined what my code looks like, is there an elegant way to control memory growth, if so, please share.

var sseChannel = require('sse-channel'),
    spawnCommand = require('spawn-command'),
    cmd = 'path to the binary file',
    globalArray = [],
    uuid = require('uuid');

module.exports = function(app) {
  var child = spawnCommand(cmd),
    privateChannel = new sseChannel({
      historySize: 0,
      cors: {
        origins: ['*']
      },
      pingInterval: 15 * 1000,
      jsonEncode: false
    });
  srvc = {
    get: function(req, res) {
      globalArray[uuid.v4()] = res;
      child.stdin.write('a json object in a format that is expected by binary' + '\n'); // req.query.<queryVal>
      child.stdout.on('data', function(data) {
        privateChannel.send(JSON.stringify(data));
      });
    },
    delete: function(sessionID) {
       var response = globalArray[sessionID];
       privateChannel.removeClient(response);
       response.end();
       delete globalArray[sessionID];
    }
  }
}
Run codeHide result

This code is intended to enumerate how it will look in the application. In this case, a snapshot of the startup code snippet will not work.

I collected the heapdump at two different intervals, and this is how the statistics look, there is a huge increase in the value of Typed Array, which could be done to support or suppress the growth of Typed Array,

enter image description here

enter image description here

+4
source share
1 answer

, , data HTTP-, . , gc.

() , , ( , data ). node.js-based, ipc-, JavaScript stdout /. node.js-based (no-ipc), , , , ( , , ).

, , , ( spawn() get()).

+5

All Articles