Skip to content

Instantly share code, notes, and snippets.

@ChALkeR
Last active December 2, 2020 09:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChALkeR/f39029f2187d1ab81fa199ab221ca639 to your computer and use it in GitHub Desktop.
Save ChALkeR/f39029f2187d1ab81fa199ab221ca639 to your computer and use it in GitHub Desktop.
Stream memory usage
'use strict';
const fs = require('fs');
fs.writeFileSync('test-small.bin', Buffer.alloc(100));
fs.writeFileSync('test-large.bin', Buffer.alloc(100000));
const blockMaxSize = 4 * 1024 * 1024; // 4 MiB
const out = { buf: [], length: 0 };
let built = 0;
function tick() {
built++;
if (built % 1000 === 0) {
console.log(`RSS [${built}]: ${process.memoryUsage().rss / 1024 / 1024} MiB`);
}
let stream;
if (built % 2 === 0) {
const stream = fs.createReadStream('./test-small.bin');
stream.on('data', function (data) {
//data = Buffer.from(data); // WARNING: uncommenting this line fixes things somehow
out.buf.push(data)
out.length += data.length
if (out.length >= blockMaxSize) {
out.buf = []
out.length = 0
}
});
stream.once('end', tick);
} else {
const stream = fs.createReadStream('./test-large.bin');
stream.on('data', function() {});
stream.once('end', tick);
/* Alternatively:
fs.readFileSync('./test-large.bin');
setImmediate(tick);
*/
}
}
tick();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment