bun icon indicating copy to clipboard operation
bun copied to clipboard

Blob allocations cause excessive memory, and GC does not release all memory / Windows 10 / `Blob,Uint8Array,ArrayBuffer`

Open mizulu opened this issue 6 months ago • 4 comments

What version of Bun is running?

1.2.18

What platform is your computer?

Windows (issue), Linux (no issue)

What steps can reproduce the bug?

  1. Run the following function on Windows in Bun:
function create1GBData(): Blob | (Uint8Array & { size: number }) {
    const sizeInBytes = 1024 * 1024 * 1024; // 1GB
    const buffer = new ArrayBuffer(sizeInBytes);
    const uint8Array = new Uint8Array(buffer);

    // Fill with pattern
    for (let i = 0; i < sizeInBytes; i++) {
        uint8Array[i] = i % 256;
    }

    return new Blob([uint8Array]);
}
  1. Allocate 14 blobs using this function (Array.from({length: 14}, create1GBData)).
  2. Observe memory usage (e.g., via Task Manager).
  3. Force garbage collection (manual GC).
  4. sleep and observe memory utilization.

What is the expected behavior?

  • Memory usage for Blob allocation should be comparable on Windows and Linux.
  • Memory should be released after GC, similar to the behavior on Linux and in Node.js.
  • Returning a Blob should not incur a 5GB+ overhead compared to returning a Uint8Array.

What do you see instead?

  • On Windows, allocating 14 blobs (~1GB each) results in total memory usage of ~20GB.
  • After manual GC, around 7GB of memory is not released.
  • Returning the Uint8Array instead of a Blob allocate less memory but holds on to more memory after GC
  • On Linux, the same code does not exhibit high memory usage when using Blob, and memory is released after GC.
  • Node.js does not show this excess memory use on either platform.

Additional information

  • This issue may indicate a platform-specific memory management bug in Bun's Blob implementation on Windows.

Image

Image

mizulu avatar Jun 21 '25 18:06 mizulu

Bun

Image

Node

Image

mizulu avatar Jun 22 '25 01:06 mizulu

@mizulu Just curious: does this repro on the latest version of Bun for you?

fitzn avatar Dec 09 '25 21:12 fitzn

@fitzn I tried with a simple script (not the full one) generated 8 blobs of 1GB actual memory that got allocated was 14gb

still excessive memory usage as noted by

import { memoryUsage } from "bun:jsc";

however it is able to clear the memory after reference removal and GC forced.

mizulu avatar Dec 12 '25 10:12 mizulu

@mizulu Roger that. Thanks!

fitzn avatar Dec 12 '25 22:12 fitzn