日本語

How Much Memory Does Bun’s –smol Save?

​​Bun​memory,TypeScript

When I run Bun with --smol, as in bun --smol app.ts, peak process memory (max RSS) dropped from 50.0 MB to 32.9 MB in a workload that creates and discards small objects. In a Bun.serve server that returns JSON for each request, it dropped from 46.2 MB to 26.8 MB. On the other hand, in a workload that keeps 2 million objects alive, peak RSS did not decrease.

This article is for anyone running Bun on a low-memory server, a container, or a small VPS who wants to keep memory use down and needs to decide whether to add --smol. I report the numbers from five workloads as I measured them. I also cover how much speed was lost and the ways of setting the flag that did not take effect.

Note

The test environment was Ubuntu 24.04 (x86_64, 4 cores, 16 GB RAM) with Bun 1.4.4. MB here means 1024×1024 bytes. The numbers are medians of 5 runs each (3 runs for Bun.serve) in this environment. They will differ with other workloads or environments. I did not test macOS, Windows, containers with a memory limit set through cgroup v2 (such as Docker), the smol option for Workers, or how things change when the process runs for days.

What –smol changes

The output of bun --help describes --smol as "Use less memory, but run garbage collection more often." The official documentation for the bun run --smol section says that frequent garbage collection (GC) can make execution slower. It also says that Bun sizes the GC heap according to the available memory, including cgroup limits, with or without --smol, and that --smol is useful when you want the heap to grow more slowly.

Reading the Bun 1.4.4 source, when --smol is set, the VM of the JavaScript engine (JavaScriptCore) is created with the small heap type (HeapType::Small). Without it, the type is HeapType::Large.

How I measured

At the end of each workload script, I print two values:

  • Max RSS: the peak physical memory used by the process, taken from VmHWM in Linux's /proc/self/status
  • Peak JS heap: the maximum of heapStats().heapSize from bun:jsc, sampled several times during the workload

Calling heapStats() takes time each time it runs, so when comparing timings I ran separate sets of 5 runs without calling it. In the output shown in this article, the runs that print a numeric peak JS heap are the ones that called heapStats(), so their timings are not suitable for comparison. The measurement function was as follows.

ts
// measure.ts
import { heapStats } from "bun:jsc";
import { readFileSync } from "node:fs";

const noHeap = process.env.NO_HEAP === "1";
let maxHeap = 0;
export function sample() {
  if (noHeap) return;
  const h = heapStats().heapSize;
  if (h > maxHeap) maxHeap = h;
}
export function report(label: string, start: number) {
  const ms = performance.now() - start;
  sample();
  const status = readFileSync("/proc/self/status", "utf8");
  const hwm = Number(status.match(/VmHWM:\s+(\d+)/)![1]) / 1024;
  const heap = noHeap ? "記録なし" : `${(maxHeap / 1048576).toFixed(1)} MB`;
  console.log(`${label}: 最大RSS ${hwm.toFixed(1)} MB / JSヒープ最大 ${heap} / ${ms.toFixed(0)} ms`);
}

The workload that creates discarded data is churn.ts. It creates 10 million objects in total. I varied only the number held in an array at once (batch), passed as an argument, to compare a case with little data alive at a time against one with a lot.

ts
// churn.ts
import { report, sample } from "./measure";
const batch = Number(process.argv[2]);
const start = performance.now();
let made = 0;
while (made < 10_000_000) {
  const rows = [];
  for (let i = 0; i < batch; i++) rows.push({ id: i, x: i * 2, tags: [i, i + 1] });
  made += rows.length;
  if (made % 50_000 < batch) sample();
}
report(`一度に ${batch} 件`, start);

The data-retaining workload retain.ts creates 2 million objects of the same shape and keeps them all in an array until the end.

Results across the five workloads

I graphed the median max RSS. Gray is without --smol, light blue is with --smol.

Horizontal bar chart comparing max RSS with and without --smol across five workloads. Discarding 5,000 objects at a time drops from 50.0 to 32.9, Bun.serve drops from 46.2 to 26.8, and the other three are nearly the same
Max RSS (MB) with and without –smol. Decreased for discarding 5,000 objects at a time and for Bun.serve

Except for Bun.serve, the max RSS in the graph is the median of runs that called heapStats(). I also recorded max RSS in runs that did not call heapStats(), and the values differed, so the table shows both. Timings are medians of runs that did not call heapStats().

Workload Max RSS (with heapStats) Max RSS (without heapStats) Time or count
Empty script 19.0 MB → 18.9 MB ― ―
Discard, 5,000 at a time 50.0 MB → 32.9 MB 51.9 MB → 32.9 MB 273 ms → 814 ms
Discard, 100,000 at a time 105.8 MB → 107.4 MB 127.3 MB → 128.9 MB 509 ms → 516 ms
Retain 2 million objects 231.8 MB → 232.0 MB 235.1 MB → 247.1 MB 183 ms → 239 ms
Bun.serve (20,000 requests) ― 46.2 MB → 26.8 MB 7,842 req/s → 4,524 req/s

Every column is in the order "without --smol → with --smol." For Bun.serve, I did not call heapStats() inside the server, and the values come from reading /proc.

Memory used just by starting up did not change. The difference appeared only in the workload that keeps little data alive at a time.

With little data alive at once, max RSS fell by a third

bun churn.ts 5000 creates and discards arrays of 5,000 objects, repeating this 2,000 times. At any point, at most 5,000 objects are alive.

text
$ bun churn.ts 5000
一度に 5000 件: 最大RSS 49.3 MB / JSヒープ最大 14.4 MB / 1786 ms
$ bun --smol churn.ts 5000
一度に 5000 件: 最大RSS 32.5 MB / JSヒープ最大 3.8 MB / 1301 ms

Across 5 runs, the median peak JS heap went from 14.6 MB to 3.8 MB, and max RSS went from 50.0 MB to 32.9 MB (a 34% decrease). Even in runs that did not call heapStats(), max RSS went from 51.9 MB to 32.9 MB.

The time, however, rose a lot. In runs that did not call heapStats(), it went from 273 ms to 814 ms, roughly three times as long.

text
$ NO_HEAP=1 bun churn.ts 5000
一度に 5000 件: 最大RSS 52.3 MB / JSヒープ最大 記録なし / 276 ms
$ NO_HEAP=1 bun --smol churn.ts 5000
一度に 5000 件: 最大RSS 32.9 MB / JSヒープ最大 記録なし / 818 ms

With a lot of data alive at once, the difference disappeared

With the same 10 million objects, when 100,000 were put into an array at a time, the difference disappeared whether or not --smol was set. The peak JS heap was around 41 MB in both cases, and max RSS was around 106 MB. In runs that did not call heapStats(), max RSS was around 128 MB in both cases.

text
$ bun churn.ts 100000
一度に 100000 件: 最大RSS 106.5 MB / JSヒープ最大 44.0 MB / 3190 ms
$ bun --smol churn.ts 100000
一度に 100000 件: 最大RSS 107.4 MB / JSヒープ最大 41.0 MB / 3031 ms

In retain.ts, which keeps 2 million objects alive, the median peak JS heap dropped from 163.9 MB to 122.7 MB. But max RSS was nearly the same, 231.8 MB and 232.0 MB. In runs that did not call heapStats(), max RSS went from 235.1 MB to 247.1 MB, so it was higher with --smol. The time rose from 183 ms to 239 ms.

text
$ bun retain.ts
2000000 件を持ち続ける: 最大RSS 231.8 MB / JSヒープ最大 170.2 MB / 2407 ms
$ bun --smol retain.ts
2000000 件を持ち続ける: 最大RSS 232.0 MB / JSヒープ最大 126.8 MB / 2232 ms
Point

What --smol reduced was the memory of a workload that keeps little data alive at a time and repeatedly creates and discards it. In a workload that keeps a large amount of data alive, adding --smol did not reduce max RSS, and it made the run slower.

Bun.serve: memory fell by 40%, and throughput fell by 40% too

I set up a server that creates an array of 1,000 objects for each request and returns it as JSON.

ts
// server.ts
const server = Bun.serve({
  port: 3000,
  fetch() {
    const items = [];
    for (let i = 0; i < 1000; i++) items.push({ id: i, name: `item-${i}`, price: i * 10 });
    return Response.json(items);
  },
});
console.log(`listening on ${server.url}`);

I started this and, from another process on the same machine (load.ts), sent 20,000 requests in total, 50 at a time concurrently. load.ts only uses fetch to send a request and sends the next one once the body has been read. I read the server's memory from /proc/<process ID>/status.

The first run without --smol looked like this:

text
起動直後: VmHWM: 16708 kB VmRSS: 16708 kB
$ bun load.ts
20000 回 / 2.55 秒 / 1秒あたり 7842 回
負荷のあと: VmHWM: 47292 kB VmRSS: 47292 kB

The third run, started with bun --smol server.ts, looked like this:

text
起動直後: VmHWM: 16620 kB VmRSS: 16620 kB
$ bun load.ts
20000 回 / 4.42 秒 / 1秒あたり 4524 回
負荷のあと: VmHWM: 27388 kB VmRSS: 27388 kB

Across 3 runs, the median max RSS after the load went from 46.2 MB to 26.8 MB (a 42% decrease). The number of requests handled per second went from 7,842 to 4,524 (a 42% decrease). With this server, throughput dropped by roughly the same percentage as memory.

Ways to enable –smol, and ways that did not work

I judged whether the setting took effect by whether the peak JS heap in bun churn.ts 5000 fell into the 3–4 MB range (it was in the 13–15 MB range when it did not take effect).

On the command line, put it right after bun, as in bun --smol churn.ts. The official documentation gives the example bun --smol run index.tsx.

The environment variable BUN_OPTIONS prepends command-line arguments every time Bun runs. Running with BUN_OPTIONS=--smol worked.

text
$ BUN_OPTIONS=--smol bun churn.ts 5000
一度に 5000 件: 最大RSS 33.2 MB / JSヒープ最大 3.2 MB / 1251 ms

The method of writing smol = true in bunfig.toml gave different results depending on how Bun was invoked. It worked with bun run churn.ts, but not with bun churn.ts, which does not use run.

text
$ cat bunfig.toml
smol = true
$ bun churn.ts 5000
一度に 5000 件: 最大RSS 49.8 MB / JSヒープ最大 14.6 MB / 1684 ms
$ bun run churn.ts 5000
一度に 5000 件: 最大RSS 34.5 MB / JSヒープ最大 3.8 MB / 1246 ms

With bun test, the peak JS heap stayed in the 13–14 MB range whether I passed --smol or set smol = true under [test] in bunfig.toml. These results come from a test that created and discarded 5,000-object arrays inside the test, the same as churn.ts, and then reported the peak JS heap.

text
$ bun test 2>&1 | grep -E 'JSヒープ|pass|fail|Ran'
JSヒープ最大 14.1 MB
(pass) 使い捨てのオブジェクトを作り続ける [909.60ms]
 1 pass
 0 fail
Ran 1 test across 1 file. [912.00ms]
$ bun test --smol 2>&1 | grep -E 'JSヒープ|pass|fail|Ran'
JSヒープ最大 13.6 MB
(pass) 使い捨てのオブジェクトを作り続ける [774.03ms]
 1 pass
 0 fail
Ran 1 test across 1 file. [778.00ms]

In the Bun 1.4.4 source, the startup code for bun run passes the --smol value on as the heap type setting (mini_mode). The startup code for bun test, by contrast, does not pass mini_mode, so it falls back to the default of false.

With a 64 MB memory limit, the heap without –smol did not change

The official documentation says that even without --smol, the heap size is set according to the available memory. So I created a cgroup v1 group with a 64 MB limit on the test machine and ran churn.ts 5000 from a shell placed in it.

bash
mkdir -p /sys/fs/cgroup/memory/smol-verify
echo $((64*1024*1024)) > /sys/fs/cgroup/memory/smol-verify/memory.limit_in_bytes
text
$ cat /sys/fs/cgroup/memory/smol-verify/memory.limit_in_bytes
67108864
$ bash -c 'echo $$ > /sys/fs/cgroup/memory/smol-verify/cgroup.procs; bun  churn.ts 5000'
一度に 5000 件: 最大RSS 49.4 MB / JSヒープ最大 14.4 MB / 1737 ms
$ bash -c 'echo $$ > /sys/fs/cgroup/memory/smol-verify/cgroup.procs; bun --smol churn.ts 5000'
一度に 5000 件: 最大RSS 32.4 MB / JSヒープ最大 3.8 MB / 1275 ms

As in the run without a limit, the peak JS heap was 14.4 MB without --smol and 3.8 MB with it. Under this cgroup v1 limit of 64 MB, the heap without --smol did not shrink. It shrank only when --smol was added.

​Posted by 86