Runtime and memory · 3 MIN READ
GC cost, allocation rate and memory budgets
Distinguish reducing live data from tuning collection frequency and respect a container memory limit.
A service pays for memory in more than one way: live objects occupy space, new allocations create work, and collection uses CPU. Tuning a single GC knob cannot repair an unbounded cache or a queue that retains every request body.
Measure two different problems
A high allocation rate with a small live heap suggests churn: many objects become garbage quickly. A growing live heap suggests retained data or a growing workload. Capture representative profiles before changing code; removing one temporary allocation is not helpful if a cache retains gigabytes indefinitely.
Treat settings as tradeoffs
GOGC influences the heap-growth target and therefore the tradeoff between collection frequency and memory. GOMEMLIMIT is a soft runtime memory limit, not an operating-system hard cap. Native allocations and other process memory can require additional budget. Leave measured headroom under the container limit rather than making the two numbers equal.
# Illustrative native-service configuration, not an iOS setting:
GOGC=100 GOMEMLIMIT=400MiB ./serviceThe 400 MiB value is an example, not a recommendation for every 512 MiB container. Measure your binary, traffic and non-runtime memory before choosing a value.
Reduce work before aggressive tuning
Avoid retaining whole request graphs in queues, bound cache entries, and reuse buffers only with clear ownership and size limits. Increasing a memory allowance may reduce collection frequency but raises memory use; forcing very frequent GC can waste CPU. A pool is an optimization with lifecycle constraints, not a guarantee that objects will stay cached or that memory use is bounded.
TAKE THIS WITH YOU
First bound live data and measure allocation churn. Then tune collection within an observed process budget and recheck latency under load.
Check your understanding
Which statement is correct about gomemlimit?
Go to the source
Progress stays in this browser. No account needed.