Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

Bun 8 min read How-to mediumAdept

Debugging JavaScript Memory Leaks

Bun implements V8's writeHeapSnapshot API, so heap snapshots can be loaded and diffed in Chrome DevTools, where a steadily growing Delta column reveals leaks. bun:jsc's heapStats also reports per-type object counts, including objects protected from GC. Passing a Blob instead of a Uint8Array to repeated Response objects cut memory from ~1 GB to ~60 MB by avoiding per-Response copies. Common leak sources are closures retaining JSLexicalScope objects, active AbortSignals kept reachable by listeners (shown via JSC's isReachableFromOpaqueRoots), bound functions, and EventEmitter listeners that are never removed.

Related posts

  • MemLab: An open source framework for finding JavaScript memory leaks

    Meta Engineering mediumAdept

    MemLab finds JavaScript memory leaks by driving a headless browser with Puppeteer through a navigate-away/target/return sequence, then diffing heap snapshots as (SB \ SA) ∩ SA' to find objects that should have been freed. It filters candidates with framework knowledge such as detached React Fiber nodes, then generates and clusters retainer traces from GC roots. A lazily constructed graph-view API supports custom leak detectors, Jest memory assertions and heap analysis. At Meta it helped cut Facebook.com OOM crashes by 50%, React 18's aggressive Fiber cleanup on unmount cut average memory by nearly 25%, and interning Relay cache-key strings cut p99 memory by 20%.

  • Bun v1.0.21

    Bun mediumAdept

    Bun v1.0.21 fixes 33 bugs and focuses on memory: blob.arrayBuffer() now uses copy-on-write virtual memory mappings for blobs of 8 MB or more (1 MB with --smol), so 100 copies of a 128 MB blob use 192 MB instead of 12.9 GB. bun:sqlite now reports SQLite's memory to the garbage collector, cutting usage 4x for 100,000 prepared statements, and throws SQLiteError with extended error codes and messages. The release also adds console.table(), Blob support in Bun.CryptoHasher and a toBeObject matcher. It fixes path-string memory leaks in Bun.file, Bun.write and node:fs errors, plus several TLS and WebSocket crashes.

  • Bun v0.6.9

    Bun mediumAdept

    Bun v0.6.9 focuses on memory reductions: zero-copy string Response bodies in Bun.serve() and Response (60 MB vs Deno's 425 MB and Node's 1414 MB after 1000 responses of a 12 MB string), 50% less memory for cold bun install, and fixed leaks when importing modules. It also fixes non-ASCII filename imports, mockResolvedValue and missing function properties on mocks in bun test, a GC-related crash in the CommonJS require() loader, and a ~192-byte-per-call leak in node:crypto hashing.

  • Bun v1.2.2

    Bun mediumAdept

    Bun v1.2.2 fixes 38 bugs and cuts idle JavaScript memory usage by 10–30% (28% for Next.js, 11% for Elysia). The fix integrates JavaScriptCore's garbage collector timers into Bun's event loop, and on Linux GC thread suspension moves from SIGUSR1 to SIGPWR so it no longer conflicts with applications that use SIGUSR1. The release also fixes a vite build regression caused by an event-loop bug in async file writes, and improves reliability of Bun.SQL prepared statements, S3 multipart uploads and the CSS parser. Node.js compatibility additions include fs.glob, NODE_PATH resolution, and fixes for fs.Dir, createRequire on virtual paths, pnpm, and DuckDB native modules.

  • Bun v1.1.43

    Bun mediumAdept

    Bun v1.1.43 adds a first-class S3 client with a Blob/Response-style API that works against any S3-compatible store, experimental HTML bundling in bun build, V8-format heap snapshots generated from its JavaScriptCore fork, and new bun install options including --filter, --lockfile-only and --peer. Node.js compatibility improves: node:path passes 100% of Node's tests, node:zlib passes 98%, and fixes land for process.kill, Node-API error codes and process.memoryUsage().arrayBuffers, which differs because JavaScriptCore owns typed-array buffers differently from V8. Parser fixes replace hard SIGILL crashes on deeply nested code with recoverable stack overflow errors by removing recursion and splitting a 1,300-line visitor function.

The quest for software engineering knowledge.

Look:
Layout: