Skip to content

Start generated apps with an 8 MiB heap - #60

Merged
crimson-knight merged 1 commit into
mainfrom
perf/start-with-8-mib-heap
Nov 21, 2026
Merged

crimson-knight merged 1 commit into
mainfrom
perf/start-with-8-mib-heap

Conversation

@crimson-knight

Copy link
Copy Markdown
Member

Generated apps now start the garbage collector with an 8 MiB heap (server.gc_initial_heap_mib: 8 in every environment file), down from 16 MiB, so a generated app stays under 20 MiB of memory under load. The constant's comment and the generated README state the trade and how to raise the value per environment (config/environments/*.yml or AMBER_SERVER_GC_INITIAL_HEAP_MIB).

Measurements

Harness: home-page-memory-bench (mem.rb, mem_post.rb) on an app from this branch's generator with the Post scaffold and 50 seeded rows, crystal-alpha build --release (Crystal 1.21.0, LLVM 21.1.8), arm64 macOS, production mode, one process, oha. "All" = amber v2-dev with amberframework/amber#1456 (Logger), #1457 (pooled compression) and #1458 (asset tags); "base" = amber v2-dev 814705ee. The machine was shared with heavy jobs (load average 40 to 270), so requests/s and p99 are approximate; peak RSS, bytes and CPU time per request are the steadier figures.

Sweep on "all": starting heap 0, 4, 8, 16 MiB at 1, 4 and 32 connections (15 s, 2 rounds, interleaved)

Route Connections Peak RSS 0 / 4 / 8 / 16 MiB CPU µs per request 0 / 4 / 8 / 16 p50 ms 0 / 4 / 8 / 16
GET / (page cache) 1 9.9 / 11.5 / 15.6 / 24.1 62.5 / 61.0 / 56.4 / 60.9 0.15 / 0.15 / 0.14 / 0.19
GET / 4 10.8 / 11.5 / 15.9 / 24.6 20.1 / 18.5 / 17.7 / 17.3 0.10 / 0.13 / 0.13 / 0.12
GET / 32 13.3 / 14.4 / 15.2 / 23.9 14.0 / 14.1 / 13.9 / 13.5 0.45 / 0.47 / 0.52 / 0.45
GET /api/posts (JSON, 50 rows) 1 10.3 / 11.1 / 15.4 / 24.0 204 / 188 / 178 / 143 0.75 / 0.56 / 0.59 / 0.36
GET /api/posts 4 10.4 / 11.1 / 15.4 / 23.9 120 / 110 / 110 / 102 0.52 / 0.45 / 0.60 / 0.46
GET /api/posts 32 14.5 / 14.4 / 15.3 / 23.7 107 / 111 / 108 / 92 5.2 / 5.6 / 5.8 / 3.8
POST /posts (form + CSRF, 302) 1 12.2 / 12.9 / 17.5 / 25.9 256 / 232 / 250 / 251 0.78 / 1.26 / 1.24 / 1.23
POST /posts 4 12.4 / 13.2 / 17.5 / 26.1 121 / 107 / 91 / 89 0.50 / 0.38 / 0.34 / 0.34
POST /posts 32 17.1 / 15.9 / 16.7 / 25.4 89 / 95 / 91 / 83 3.19 / 3.69 / 3.44 / 2.92

8 against 16 MiB, base and all, 32 connections (15 s, 3 rounds, interleaved ABBA)

Route Build Peak RSS 8 / 16 MiB Req/s 8 / 16 p50 ms 8 / 16 CPU µs/req 8 / 16
GET / all 15.22 / 23.45 26,217 / 20,966 0.54 / 0.53 16.0 / 15.4
GET / base 15.25 / 23.92 29,543 / 23,525 0.50 / 0.55 15.3 / 15.5
GET /api/posts all 15.45 / 24.00 6,055 / 7,029 3.91 / 3.09 98.1 / 82.5
GET /api/posts base 15.34 / 24.25 6,064 / 6,192 3.86 / 3.37 97.1 / 88.9
POST /posts all 16.81 / 25.59 5,302 / 5,450 4.10 / 3.33 101.3 / 85.8
POST /posts base 20.69 / 26.30 5,185 / 7,342 4.13 / 3.17 106.5 / 87.2
GET /posts/new (form, full render, gzip) all 15.03 / 23.67 7,114 / 5,383 2.33 / 2.40 64.8 / 63.0
GET /posts/new base 16.33 / 24.52 4,835 / 3,991 3.45 / 3.21 88.6 / 79.3

The choice: 8 MiB

  • 16 MiB puts every route at 23.5 to 26.3 MiB, over the 20 MB target. 8 MiB puts every route of the "all" build at 15.0 to 16.8 MiB and saves 8.2 to 8.8 MiB of peak memory.
  • 4 MiB and 0 save 1 to 5 MiB more, but CPU per request and p50 get worse at low connection counts (POST at 1 connection: p50 1.26 ms at 4 MiB; JSON at 1 connection: 188 to 204 µs per request), and the sweep does not show a steady memory gain at 32 connections (POST peaks at 15.9 to 17.1 MiB at 0 and 4 MiB).
  • The cost of 8 against 16 MiB falls on allocation-heavy routes: the 16 MiB heap collects less often, so GET /api/posts and POST /posts used 16 to 18% less CPU per request there, and p50 at 32 connections was 0.8 ms lower. GET / and the form page show no difference. Requests/s moved both ways under the shared load.
  • Until generated apps pin an Amber release with the pooled compression pipe (Add Amber::Pipe::Compression with pooled zlib streams amber#1457), POST /posts peaks at 20.7 MiB at 8 MiB (base row); with it, 16.8 MiB.

Specs

spec/commands/new_command_spec.cr checks the constant is 8, that all three environment files carry it, and that the README names the setting and the trade-off.

Measured on an Amber CLI app with the Post scaffold (amber v2-dev with the Logger and Compression changes, crystal-alpha release build), 32 connections: peak RSS at 8 MiB is 15.2 MiB on GET /, 15.5 MiB on GET /api/posts and 16.8 MiB on POST /posts, against 23.5, 24.0 and 25.6 MiB at 16 MiB. The 16 MiB heap collects less often, so GET /api/posts and POST /posts used about 18% less CPU per request there; the README says how to make that trade.
@crimson-knight
crimson-knight merged commit 4aaaed1 into main Nov 21, 2026
13 checks passed
@crimson-knight
crimson-knight deleted the perf/start-with-8-mib-heap branch November 21, 2026 12:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant