Repository navigation
Closed
Labels
Workeruses the web or node worker apiuses the web or node worker apiatwbugSomething isn't workingSomething isn't workingmemory usagenode.jsCompatibility with Node.js APIsCompatibility with Node.js APIs
Description
What version of Bun is running?
1.0.3+f77df12894e7952ea58605432bf9f6d252fff273
What platform is your computer?
Linux 5.15.90.1-microsoft-standard-WSL2 x86_64 x86_64
What steps can reproduce the bug?
Create a main file which starts up a worker, waits until it completes its work, then logs memory usage.
async function run() {
for (let i = 0; i < 100; i++) {
await new Promise((resolve) => {
const worker = new Worker('./worker.ts')
const on_message = () => {
worker.removeEventListener('message', on_message)
resolve(1)
}
worker.addEventListener('message', on_message)
worker.postMessage('start')
})
console.log(process.memoryUsage().rss)
}
}
run()And a worker which allocates a large amount of memory
function on_message() {
const arrays = []
for (let i = 0; i < 1_000_000; i++) {
arrays.push(new Array(100).fill(i))
}
self.postMessage(arrays.length)
self.removeEventListener('message', on_message)
}
self.addEventListener('message', on_message)Then run main with bun run main.ts
What is the expected behavior?
Memory usage should spike as the worker begins, then drop sometime afterwards as garbage collection is run. The script should continue running until the loop completes.
When a (functionally) identical script and worker (code below) is run via Node, memory usage stays around the same every call.
import { Worker } from 'worker_threads'
async function run() {
for (let i = 0; i < 100; i++) {
await new Promise((resolve) => {
const worker = new Worker('./node/worker.js')
const on_message = () => {
worker.removeListener('message', on_message)
resolve(1)
}
worker.addListener('message', on_message)
worker.postMessage('start')
})
console.log(i)
}
}
run()import { parentPort } from 'worker_threads'
function on_message() {
const arrays = []
for (let i = 0; i < 1_000_000; i++) {
arrays.push(new Array(100).fill(i))
}
parentPort.postMessage(arrays.length)
parentPort.removeListener('message', on_message)
}
parentPort.addListener('message', on_message)Results in node:
> node node/main.js
971173888
983752704
979537920
988639232
986144768
979836928
984797184
986599424
985792512
^CWhat do you see instead?
Memory usage continues to rise, eventually crashing (saturates 8gb of RAM and 2gb of swap) at around the 8th iteration.
Results in Bun:
> bun bun/main.ts
1050456064
2005811200
2962059264
3915100160
4867907584
5819809792
6765035520
^CAdditional information
- Adding
smolto eitherbun runornew Worker()has no effect. - I fixed this by calling
Bun.gc(true)immediately afterself.postMessage()in a personal project, but couldn't reproduce the results here. - I don't have enough knowledge of swap memory to know if this is unexpected, but after crashing, the swap usage often grows.
Reacted by KingKong, Flashantik, Joshua Schumacher, Pavol Daniš, Aaron Queen, Nicolas Ngomai, Max Milton, Vlad Sirenko, ChallenAi, Miks Kalniņš and 3 more
Reacted by Kolton Guthrie, Miks Kalniņš, Shiv and cimak
Metadata
Metadata
Assignees
Labels
Workeruses the web or node worker apiuses the web or node worker apiatwbugSomething isn't workingSomething isn't workingmemory usagenode.jsCompatibility with Node.js APIsCompatibility with Node.js APIs
Activity
[-]Bun runs out of memory when using Workers[/-][+]Bun doesn't clean up memory used by Workers[/+][-]Bun doesn't clean up memory used by Workers[/-][+]Bun leaks memory in Workers[/+]AriPerkkio commentedon Sep 19, 2023
I think I've run into similar issue when using Bun's
Worker's. This error message is being thrown and Bun crashes when memory leaks enough:Electroid commentedon Sep 19, 2023
Thanks for reporting, this is a known issue that we will be fixing.
jack-weilage commentedon Sep 19, 2023
Interesting! In my case, Bun never returned an error before crashing.
jack-weilage commentedon Oct 4, 2023
Any follow-up on when this might be fixed? This is preventing me from using Workers in bun.
jack-weilage commentedon Dec 29, 2023
I just re-ran the reproduction code and the memory leak seems to have been mostly fixed somewhere between v1.0.11 and v1.0.12. Memory usage used to double every iteration, but now (in my testing) memory usage went from
1013653504to1034555392after 1000 iterations. That averages out to ~2000 per iteration, as compared to ~1000000000 per iteration when I originally reported.jack-weilage commentedon Dec 29, 2023
Should this issue be closed (the massive memory leak is fixed), or kept open (there still seems to be a tiny leak left over)?
dgertych-monterail commentedon Mar 18, 2024
It's still valid for me for the most recent released version (1.0.33) even for canary :(
The memory is growing on each worker passage, it's not free at all even if the worker has been terminated.
MeguminSama commentedon Apr 2, 2024
I was running into issues possibly caused by #5659 so I tried to use workers as a workaround, hoping that when the worker gets terminated, the memory might be freed. Unfortunately, this doesn't seem to be the case.
kingkong404 commentedon May 8, 2024
Currently a major issue impacting us using Bun in production.
rafrafek commentedon Jul 10, 2024
Collab with Deno maybe? denoland/deno#18414
5 remaining items
kahy9 commentedon Nov 6, 2025
Still valid, please fix that
zengyun261 commentedon Mar 31, 2026
The issue remains unresolved on Windows 11 x64.
alii commentedon May 13, 2026
Couldn't reproduce this on Bun 1.3.13 (linux x64). Closing — if you're still hitting it on the latest release, please drop a fresh repro and we'll reopen.
fix: add periodic Bun.gc(true) in usage worker to reclaim structured-…
fix: add periodic Bun.gc(true) in worker to reclaim structured-clone …
fix: bound cache-keepalive staging map to stop request-body memory leak
fix: stop leaking request bodies into the post-processor worker
fix: retire the post-processor worker, compute usage on the main thread
fix: replace Bun worker with main-thread UsageCollector to eliminate …
fix: replace Bun worker with main-thread UsageCollector to eliminate …
fix: replace Bun worker with main-thread UsageCollector to eliminate …
fix: replace Bun worker with main-thread UsageCollector to eliminate …
fix: replace Bun worker with main-thread UsageCollector to eliminate …
fix: replace Bun worker with main-thread UsageCollector to eliminate …