$ uname -a
Linux docker-desktop 5.15.133.1-microsoft-standard-WSL2 #1 SMP Thu Oct 5 21:02:42 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux
$ node --version
v20.14.0
$ node mem.js
before write: rss: 40.05MB, heapTotal: 4.20MB, heapUsed: 3.53MB, external: 1.33MB
writing 0: rss: 42.77MB, heapTotal: 4.45MB, heapUsed: 3.87MB, external: 1.45MB
writing 5000000: rss: 2249.27MB, heapTotal: 1316.65MB, heapUsed: 1268.95MB, external: 1.45MB
writing 10000000: rss: 3962.07MB, heapTotal: 2235.37MB, heapUsed: 2169.62MB, external: 1.45MB
writing 15000000: rss: 3963.01MB, heapTotal: 2210.23MB, heapUsed: 2147.29MB, external: 1.45MB
writing 20000000: rss: 4011.84MB, heapTotal: 2243.73MB, heapUsed: 2147.31MB, external: 1.45MB
writing 25000000: rss: 4774.08MB, heapTotal: 3005.14MB, heapUsed: 2113.35MB, external: 1.45MB
writing 30000000: rss: 4790.29MB, heapTotal: 3020.89MB, heapUsed: 2113.53MB, external: 1.45MB
writing 35000000: rss: 4925.86MB, heapTotal: 3155.89MB, heapUsed: 2113.31MB, external: 1.45MB
writing 40000000: rss: 6049.37MB, heapTotal: 3517.00MB, heapUsed: 3406.85MB, external: 1.45MB
writing 45000000: rss: 6030.36MB, heapTotal: 2752.64MB, heapUsed: 2113.42MB, external: 1.45MB
writing 50000000: rss: 6030.34MB, heapTotal: 2606.64MB, heapUsed: 2113.43MB, external: 1.45MB
writing 55000000: rss: 6032.05MB, heapTotal: 2925.14MB, heapUsed: 2113.35MB, external: 1.45MB
writing 60000000: rss: 6032.02MB, heapTotal: 3478.17MB, heapUsed: 3407.50MB, external: 1.45MB
writing 65000000: rss: 6032.27MB, heapTotal: 3477.92MB, heapUsed: 3407.79MB, external: 1.45MB
writing 70000000: rss: 6040.72MB, heapTotal: 3478.67MB, heapUsed: 3406.11MB, external: 1.45MB
writing 75000000: rss: 6067.33MB, heapTotal: 2930.64MB, heapUsed: 2113.26MB, external: 1.45MB
writing 80000000: rss: 6033.58MB, heapTotal: 2790.64MB, heapUsed: 2113.26MB, external: 1.45MB
writing 85000000: rss: 6034.83MB, heapTotal: 2941.64MB, heapUsed: 2113.26MB, external: 1.45MB
writing 90000000: rss: 6033.95MB, heapTotal: 2903.64MB, heapUsed: 2113.26MB, external: 1.45MB
writing 95000000: rss: 6033.57MB, heapTotal: 3002.64MB, heapUsed: 2113.45MB, external: 1.45MB
after write: rss: 5996.64MB, heapTotal: 3439.52MB, heapUsed: 3367.73MB, external: 1.45MB
onclose: rss: 6034.87MB, heapTotal: 2916.64MB, heapUsed: 2113.49MB, external: 1.45MB
onclose + 1s: rss: 5474.29MB, heapTotal: 2916.64MB, heapUsed: 2121.69MB, external: 1.45MB
onclose + 60s: rss: 2550.00MB, heapTotal: 4.45MB, heapUsed: 3.02MB, external: 1.45MB
Last active
June 4, 2024 06:00
-
-
Save unarist/db07fac36ba15b96e87c430272bcc9e8 to your computer and use it in GitHub Desktop.
NodeJSのWriteStreamで、細かいチャンク(というか空)を大量に書き込む前後で、rssとheapTotalの差が36MBから2.5GBに広がる話。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs') | |
const ws = fs.createWriteStream("/dev/null"); | |
const fmt = n => (n / 1024 / 1024).toFixed(2) + "MB"; | |
const wait = ms => new Promise(resolve => setTimeout(resolve, ms)); | |
const log = msg => { | |
globalThis.gc?.(); | |
const usage = process.memoryUsage(); | |
console.log(`${msg}: rss: ${fmt(usage.rss)}, heapTotal: ${fmt(usage.heapTotal)}, heapUsed: ${fmt(usage.heapUsed)}, external: ${fmt(usage.external)}`); | |
}; | |
(async () => { | |
log("before write"); | |
for (let i = 0; i < 100 * 1000 * 1000; i++) { | |
ws.write(""); | |
if (i % (5 * 1000 * 1000) === 0) { | |
await wait(0); | |
log("writing " + i); | |
} | |
} | |
ws.end(); | |
log("after write"); | |
ws.on("close", () => { | |
log("onclose"); | |
setTimeout(() => log("onclose + 1s"), 1000); | |
setTimeout(() => log("onclose + 60s"), 60000); | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment