Skip to content

Instantly share code, notes, and snippets.

@timothyb89
Created June 16, 2017 23:13
Show Gist options
  • Save timothyb89/d9620912ee6ec66cfc9ca0f6bf6a8b1a to your computer and use it in GitHub Desktop.
Save timothyb89/d9620912ee6ec66cfc9ca0f6bf6a8b1a to your computer and use it in GitHub Desktop.
child_process memory issue local reproduction attempt
// use to attempt to reproduce locally like so:
// docker run --rm=true -v $(pwd)/local.js:/local.js node:6.11.0 node -e "require('./local').reproduce_sync_exec()"
const child_process = require('child_process');
const os = require('os');
function debug() {
const free = os.freemem();
const total = os.totalmem();
console.log('process: ', process.memoryUsage(), 'os: ', {
free, total,
usedMiB: (total - free) / (1024 * 1024)
});
}
function spawn(command, args, options = {}) {
return new Promise((resolve, reject) => {
const child = child_process.spawn(command, args, options);
child.on('error', err => {
reject(err);
});
child.on('close', code => {
if (code === 0) {
resolve(code);
} else {
reject(code);
}
});
});
}
function spawn_child() {
return spawn('ls', ['/']).then(() => {
debug();
});
}
function reproduce_async_spawn() {
spawn_child()
.then(spawn_child)
.then(spawn_child)
.then(spawn_child)
.then(spawn_child)
.then(spawn_child)
.then(spawn_child)
.then(spawn_child)
.then(spawn_child)
.then(spawn_child)
.then(() => {
console.log('done');
})
.catch(err => {
console.log(err);
});
}
function reproduce_sync_exec() {
for (let i = 0; i < 10; i++) {
child_process.execSync('ls /');
debug();
}
console.log('done');
}
function reproduce_sync_spawn() {
for (let i = 0; i < 10; i++) {
child_process.spawnSync('ls' ['/']);
debug();
}
console.log('done');
}
module.exports = {
reproduce_async_spawn,
reproduce_sync_exec,
reproduce_sync_spawn
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment