GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Creating a lot of worker threads at once doesn't fully release their memory once they're terminated · Issue #51868 · nodejs/node Closed DescriptionVersionv20.11.1 PlatformLinux ovps2 5.15.0-1051-oracle #57-Ubuntu SMP Wed Jan 24 18:31:24 UTC 2024 aarch64 aarch64 aarch64 GNU/Linux SubsystemNo response What steps will reproduce the bug?The following code creates 50 worker threads which load 20mb of data into memory and then terminates them. This is repeated 4 times, and the RSS memory usage either grows or slightly decreases with each iteration. Main file: const { Worker } = require("worker_threads") const path = require("path") const sleep = ms => new Promise(resolve => setTimeout(resolve, ms)) async function doLeak() { const jobs = [] for(let i=0;i<50;i++) { const worker = new Worker(path.join(__dirname, "worker.js")) jobs.push(new Promise(resolve => { worker.once("message", () => { worker.terminate() resolve() }) })) } await Promise.all(jobs) } async function main() { console.log("RSS on start:", Math.floor(process.memoryUsage().rss/1024/1024), "MB") for(let i=1;i<=4;i++) { console.log("Starting...", i) await doLeak() await sleep(5000) // This is not required, but helps show that the memory isn't released even after waiting console.log("Finished! RSS:", Math.floor(process.memoryUsage().rss/1024/1024), "MB") } } main() worker.js: const { parentPort } = require("worker_threads") const fs = require("fs") const path = require("path") // Create some data in memory const data = fs.readFileSync(path.join(__dirname, "20mb.txt")) parentPort.postMessage("hi") For convenience, text file with 20mb of random data: 20mb.txt How often does it reproduce? Is there a required condition?Every time What is the expected behavior? Why is that the expected behavior?RSS memory doesn't leak What do you see instead?RSS memory grows drastically (from ~36mb up to ~700mb after running the provided example code) Additional informationThe memory leak only occurs if the worker threads are created at the same time (i.e. awaiting for 100ms between each worker's creation will not cause it to happen). It also requires the worker threads to have some data in their memory, which leads me to believe the excessive RSS memory is a result of failed de-allocation of the worker threads' memory segments (I could be wrong). Note that it doesn't seem to matter when the worker threads are terminated. Only their creation matters. I've reproduced this on
I've been unable to reproduce this on Reacted by dolfies, User and Ilyas Shabi Activity
MetadataMetadataAssigneesLabelsNo labels TypeProjectsMilestoneRelationshipsDevelopmentIssue actions |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |