Erlang / Erlang Basics Interview questions
How do you troubleshoot process mailbox overflow?
A growing mailbox means a process is receiving messages faster than it processes them, which if left unchecked can exhaust memory and eventually crash the node. The first step is confirming which process(es) are affected.
Pid = whereis(my_worker), {message_queue_len, Len} = process_info(Pid, message_queue_len). %% or, node-wide, via recon: recon:proc_count(message_queue_len, 5).
Once you've identified the culprit, common causes and fixes include:
- Slow receive loop — the process is doing too much per message; profile and optimize the hot path, or offload work to helper processes.
- Unbounded producer — switch producers from
casttocallso they block and naturally throttle instead of flooding the mailbox. - Selective receive scanning cost — a
receivewith a narrow pattern has to skip past unmatched messages already in the queue; a mismatched, ever-growing backlog of unrelated messages makes each receive progressively slower.
Tools like observer's process view, or the recon library's
proc_count/2, make it straightforward to spot the offending process across a whole running node
rather than checking processes one at a time.
More Related questions...