Erlang / Erlang Advanced Interview questions
How do you troubleshoot a memory leak caused by large binaries not being garbage collected?
Large (>64 byte) binaries live off the process heap and are reference-counted, but the counter only drops when the owning process's heap is actually garbage collected — a process that receives many large binaries but rarely triggers a GC cycle (because its own small heap of local variables never fills up) can hold references to megabytes of off-heap binary data far longer than expected.
%% inspect suspects erlang:process_info(Pid, binary). %% list of {BinId, Size, RefCount} recon:bin_leak(10). %% recon helper: top 10 processes by binary refs
Common fixes:
- Force a GC explicitly on long-lived, low-activity processes holding binaries via
erlang:garbage_collect(Pid), either periodically or after processing a large payload. - Shrink the reference's lifetime — if only a small slice of a large binary is actually needed
long-term, copy just that slice with
binary:copy/1so the full original can be released. - Check for accidental retention in a process dictionary entry or long-lived accumulator that's never cleared.
More Related questions...