From fced464e573205136053cc944a7ad7c185f3fc5f Mon Sep 17 00:00:00 2001 From: crimson-knight Date: Thu, 19 Nov 2026 18:31:19 -0500 Subject: [PATCH] Generate apps with a 16 MiB starting GC heap Every generated environment file sets server.gc_initial_heap_mib: 16, and the generated README says where to change it. 16 MiB is the smallest starting heap that kept POST /posts p50 at or below beta.10 in a generated app with the beta.11 changes; 4 and 8 MiB did not. Amber reads the key once amberframework/amber#1452 is in the pinned release; earlier Amber ignores it. --- spec/commands/new_command_spec.cr | 7 +++++++ src/amber_cli/commands/new_command.cr | 13 +++++++++++++ 2 files changed, 20 insertions(+) diff --git a/spec/commands/new_command_spec.cr b/spec/commands/new_command_spec.cr index 2ed91c7..db5bcab 100644 --- a/spec/commands/new_command_spec.cr +++ b/spec/commands/new_command_spec.cr @@ -355,6 +355,13 @@ 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 + %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") + routes = File.read(File.join(destination, "config/routes.cr")) lean_block = routes[routes.index!("pipeline :lean_read do")...routes.index!("pipeline :web do")] lean_block.should contain("Amber::Pipe::Error.new") diff --git a/src/amber_cli/commands/new_command.cr b/src/amber_cli/commands/new_command.cr index 91c4f56..73a2301 100644 --- a/src/amber_cli/commands/new_command.cr +++ b/src/amber_cli/commands/new_command.cr @@ -40,6 +40,12 @@ module AmberCLI::Commands VALID_DATABASES = %w[pg mysql sqlite] 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 + getter database : String = "sqlite" getter template : String = "ecr" getter app_type : String = "web" @@ -504,6 +510,10 @@ only the error and security-header pipes. Forms and anything else that needs a session stay on the `web` pipeline, with the session, flash and CSRF pipes. To 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. + 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. @@ -784,6 +794,9 @@ server: host: #{host} port: 3000 secret_key_base: "#{secret}" + # The garbage collector starts with this much heap (MiB), so a busy server does not + # collect over and over while its heap grows. 0 keeps the collector's own few hundred KiB. + gc_initial_heap_mib: #{GC_INITIAL_HEAP_MIB} database: url: "#{database_url}"