Memory Analysis & Garbage Collection Tuning
Diagnose memory growth, inspect per-process heaps, and tune Erlang garbage collection to keep long-running nodes healthy.
Memory Analysis & Garbage Collection Tuning is a free Erlang OTP: Distributed & Fault-Tolerant Systems Programming lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Erlang OTP: Distributed & Fault-Tolerant Systems Programming learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why Memory Matters
Erlang nodes can run for months. A small per-process leak or unbounded mailbox grows silently until the node is killed by the OS. Observability into memory is essential.
System-Wide Memory
erlang:memory/0 returns a breakdown: total, processes, atom, binary, ETS and more. Watch the categories that grow over time.
erlang:memory().Per-Process Inspection
process_info/2 reveals a single process heap, message queue length and more — key for finding the culprit.
process_info(Pid, [memory, message_queue_len, heap_size]).The Mailbox Trap
If a process receives faster than it handles, its mailbox grows without bound. A large message_queue_len is the classic sign of an overloaded consumer.
Binaries and Refc Leaks
Large binaries are reference-counted and shared off-heap. A process holding a tiny sub-binary can pin a huge parent binary. Watch the binary memory category closely.
Finding the Worst Offenders
Sort all processes by memory to find leaks fast.
lists:sort(fun({_,A},{_,B}) -> A > B end,
[{P, element(2, process_info(P, memory))} || P <- processes()]).How Erlang GC Works
Each process has its own private heap and is garbage-collected independently — there is no global stop-the-world pause. Collection runs when a process heap fills.
Triggering GC Manually
For a process you suspect is holding garbage, force a collection and re-measure.
erlang:garbage_collect(Pid),
process_info(Pid, memory).Tuning the Heap
Spawn options like min_heap_size and fullsweep_after tune GC for hot processes, trading memory for fewer collections.
spawn_opt(fun work/0, [{min_heap_size, 1000}, {fullsweep_after, 10}]).Hibernation
Idle processes can call hibernate to compact their heap to the minimum, freeing memory until the next message arrives — great for many mostly-idle connections.
proc_lib:hibernate(?MODULE, loop, [State]).Watching ETS Tables
ETS tables live outside process heaps and do not shrink automatically. A growing ets memory category often means stale rows; periodically prune or use tables with a bounded size.
ets:info(my_table, memory).Quick Check
Test your memory tuning knowledge.
Recap
You learned to analyze and tune memory:
erlang:memory/0gives a system-wide breakdownprocess_info/2inspects per-process heap and mailbox- Growing mailboxes and pinned binaries are common leak sources
- Each process is GC-ed independently; no global pause
- Tune with spawn options and use
hibernatefor idle processes
Frequently asked questions
Is the “Memory Analysis & Garbage Collection Tuning” lesson free?
Yes — the full text of “Memory Analysis & Garbage Collection Tuning” is free to read here on the web, and the Erlang OTP: Distributed & Fault-Tolerant Systems Programming course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Erlang OTP: Distributed & Fault-Tolerant Systems Programming course, upgrade to CoddyKit PRO.
What will I learn in “Memory Analysis & Garbage Collection Tuning”?
Diagnose memory growth, inspect per-process heaps, and tune Erlang garbage collection to keep long-running nodes healthy. You practise Erlang OTP: Distributed & Fault-Tolerant Systems Programming with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Erlang OTP: Distributed & Fault-Tolerant Systems Programming?
No prior experience is required. Erlang OTP: Distributed & Fault-Tolerant Systems Programming on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Memory Analysis & Garbage Collection Tuning” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Erlang OTP: Distributed & Fault-Tolerant Systems Programming lesson?
Yes. Every Erlang OTP: Distributed & Fault-Tolerant Systems Programming lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Erlang Profiling Techniques
- Tracing & Debugging Distributed Systems
- Metrics & Monitoring Integration
- Memory Analysis & Garbage Collection Tuning