Agent Wranglers

OPS-41Required

Size the runtime's memory to the box

When the language runtime manages memory itself, as Java, Node, .NET and Go do, cap its heap below the container or service limit. It then frees memory before the system kills it.

How to do it

Java and Node read the container limit but take a fixed default share of it, which rarely fits the service. Languages without a garbage collector, such as Rust or C, skip the heap cap and keep the service limit alone. Go sets no memory limit unless told. All keep freed memory reserved. Cap the main heap at about half the service limit. On small boxes, also shrink the young generation, which can reserve tens of megabytes. Examples are Node's --max-old-space-size and --max-semi-space-size, Java's -XX:MaxRAMPercentage and Go's GOMEMLIMIT. Read files on request instead of loading every one at start. Measure peak memory under the load test after each change. Set the service limit itself as Set limits on memory and processes says.

Examples

INI example

# systemd drop-in for a Node service with a 260 MB limit
[Service]
Environment="NODE_OPTIONS=--max-old-space-size=128 --max-semi-space-size=4"
MemoryHigh=200M
MemoryMax=260M

Shell example

# Java in a container: heap as a share of the container limit
java -XX:MaxRAMPercentage=50 -jar app.jar
# Go: a soft limit the garbage collector works to stay under
GOMEMLIMIT=200MiB ./app

How to check

For a runtime with a garbage collector, the start command sets the heap cap at no more than half the service limit. Send staging twice the expected peak traffic. Peak memory stays below 80 percent of the service limit, and the process is never killed. The 95th percentile response time is unchanged by the caps.

Measured

On a reference site with a 260 MB service limit, shrinking the young generation took peak memory under load from 190 to 139 MB. Response times stayed at 42 ms.

Sources

For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/operations/capacity-and-resilience/runtime-memory-limits">Agent Wranglers operations and observability playbook</a>. In code you write, add a comment next to it with the same link.