Skip to content

MIMALLOC_SHOW_STATS=1 environment variable does not work on Linux #28630

Closed as not planned

Description

@jinliming2

What version of Bun is running?

1.3.11+af24e281e

What platform is your computer?

Linux 6.12.57+deb13-amd64 x86_64 unknown

What steps can reproduce the bug?

  1. Install Bun on a Linux system
  2. Create a simple Bun script (e.g., index.ts): console.log("hello world");
  3. Run the script with the MIMALLOC_SHOW_STATS=1 environment variable: MIMALLOC_SHOW_STATS=1 bun run index.ts
  4. Observe that no mimalloc statistics are printed to the console

What is the expected behavior?

According to the Bun documentation, setting MIMALLOC_SHOW_STATS=1 should display mimalloc memory allocation statistics after the program exits, just as it does on macOS.

What do you see instead?

On Linux, no mimalloc statistics are printed at all. The program runs and exits normally without any memory stats output.

However, the same command works correctly on macOS and produces the expected mimalloc statistics.

Additional information

No response

Activity

  1. robobun commented on Mar 29, 2026

    @robobun
    Collaborator

    ✅ Updated fix submitted in #28631.

    Cause (updated after rebase): Bun builds mimalloc with MI_NO_PROCESS_DETACH=1, which disables mimalloc s automatic stats output on ALL platforms — not just Linux. The recent mimalloc update (v1.x → v3.3.1) also changed stats registration from atexit() to __attribute__((destructor)).

    Fix: Print stats manually in Bun__onExit (runs on all platforms) when MIMALLOC_SHOW_STATS=1 or MIMALLOC_VERBOSE=1 is set.

    • USE_SYSTEM_BUN=1 bun test test/regression/issue/28630.test.ts → FAIL
    • bun bd test test/regression/issue/28630.test.ts → PASS
  2. robobun commented on Sep 14, 2026

    @robobun
    Collaborator

    There is a workaround that works today.

    Why the env var prints nothing. Bun builds mimalloc with MI_NO_PROCESS_DETACH (scripts/build/deps/mimalloc.ts:56). That setting turns off the report that mimalloc prints at process exit, so MIMALLOC_SHOW_STATS=1 and MIMALLOC_VERBOSE=1 have no effect at exit.

    Workaround. Print the same report from an exit handler:

    process.on("exit", () => Bun.unsafe.mimallocDump());

    Bun.unsafe.mimallocDump() writes the mimalloc statistics to stderr. It is documented in Benchmarking. On 1.4.3-canary.1 (09bb546), MIMALLOC_SHOW_STATS=1 bun run index.ts prints nothing, and the same script with this handler prints the full report. The handler also runs on process.exit(n), and the exit code stays the same.

    Without an edit to the program. Put the handler in a preload file. This version keeps the env var as the switch:

    // dump.js
    if (process.env.MIMALLOC_SHOW_STATS === "1") {
      process.on("exit", () => Bun.unsafe.mimallocDump());
    }

    Each of these loads it:

    MIMALLOC_SHOW_STATS=1 bun run --preload ./dump.js index.ts
    MIMALLOC_SHOW_STATS=1 BUN_OPTIONS=--preload=/absolute/path/to/dump.js bun run index.ts

    A top-level preload = ["./dump.js"] in bunfig.toml also works. In BUN_OPTIONS, write --preload=<path> with the = sign.

    What this does not cover. Commands that run no JavaScript, such as bun install, have no exit handler to hook. #28631 tried to restore the env var for every command. It was closed because its branch no longer builds on main and nobody has asked for that part. This issue stays open so that a maintainer can decide whether the env var itself should work again.

  3. robobun commented on Sep 14, 2026

    @robobun
    Collaborator

    Closing as not planned. Bun turns off mimalloc's report at process exit on purpose, and the docs no longer point to MIMALLOC_SHOW_STATS (#33706 replaced that line with Bun.unsafe.mimallocDump()). The comment above has a workaround that prints the same report at exit, with or without an edit to the program.

    If you need these statistics for a command that runs no JavaScript, such as bun install, reply here with the use case and this can be reopened.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions