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