From 8589d27100fd356fbd226aec944a341be3735a7a Mon Sep 17 00:00:00 2001 From: crimson-knight Date: Sat, 21 Nov 2026 07:26:25 -0500 Subject: [PATCH] Start generated apps with an 8 MiB heap 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. --- spec/commands/new_command_spec.cr | 4 +++- src/amber_cli/commands/new_command.cr | 12 ++++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/spec/commands/new_command_spec.cr b/spec/commands/new_command_spec.cr index 02b1c09..9e653d9 100644 --- a/spec/commands/new_command_spec.cr +++ b/spec/commands/new_command_spec.cr @@ -396,12 +396,14 @@ describe AmberCLI::Commands::NewCommand do production["session"]["expires"].as_i.should eq(1_209_600) production["static"]["headers"]["Cache-Control"].as_s.should eq("no-cache") - AmberCLI::Commands::NewCommand::GC_INITIAL_HEAP_MIB.should be > 0 + # 8 MiB keeps the generated app under 20 MiB peak memory; see the constant. + AmberCLI::Commands::NewCommand::GC_INITIAL_HEAP_MIB.should eq 8 %w[development test production].each do |environment_name| environment = YAML.parse(File.read(File.join(destination, "config/environments/#{environment_name}.yml"))) environment["server"]["gc_initial_heap_mib"].as_i.should eq(AmberCLI::Commands::NewCommand::GC_INITIAL_HEAP_MIB) end File.read(File.join(destination, "README.md")).should contain("server.gc_initial_heap_mib") + File.read(File.join(destination, "README.md")).should contain("trades memory for less collector work") routes = File.read(File.join(destination, "config/routes.cr")) lean_block = routes[routes.index!("pipeline :lean_read do")...routes.index!("pipeline :web do")] diff --git a/src/amber_cli/commands/new_command.cr b/src/amber_cli/commands/new_command.cr index 9f6c3b5..c9ac69e 100644 --- a/src/amber_cli/commands/new_command.cr +++ b/src/amber_cli/commands/new_command.cr @@ -41,10 +41,12 @@ module AmberCLI::Commands VALID_TEMPLATES = %w[ecr] # Starting garbage-collector heap, in MiB, written to `server.gc_initial_heap_mib` - # in every generated environment file. 16 MiB is the smallest starting heap that - # kept POST p50 at or below beta.10 in a generated app (4 and 8 MiB did not); it - # adds about 8 MiB of peak memory on a small JSON route. - GC_INITIAL_HEAP_MIB = 16 + # in every generated environment file. 8 MiB keeps a generated app under 20 MiB + # peak memory on its page-cached, JSON and form-post routes at 32 connections + # (15.2 to 16.8 MiB, against 23.5 to 25.6 MiB at 16 MiB). A 16 MiB heap collects + # less often, so allocation-heavy routes cost about 18% more CPU per request at + # 8 MiB; an app that prefers that trade raises the value per environment. + GC_INITIAL_HEAP_MIB = 8 getter database : String = "sqlite" getter template : String = "ecr" @@ -522,6 +524,8 @@ give a page a session, move its route to `routes :web`. The garbage collector starts with a #{GC_INITIAL_HEAP_MIB} MiB heap (`server.gc_initial_heap_mib` in `config/environments/*.yml`, or `AMBER_SERVER_GC_INITIAL_HEAP_MIB`), so a busy server does not collect over and over while its heap grows. 0 keeps the collector's own size. +A larger value trades memory for less collector work: at 16 MiB the app peaks near +24 MiB under load instead of 16 MiB, and JSON and form posts use less CPU per request. Production writes no per-request log line. The edge proxy keeps access logs. The `Amber::Pipe::Logger` lines in `config/routes.cr` show where to turn it back on.