Erlang / Erlang Advanced Interview questions
What is the significance of the binary reference count and off-heap binary garbage collection?
Any binary larger than 64 bytes is allocated once, off the process heap, and shared by reference rather than copied whenever it's passed around within reach of the same underlying data (for instance, sub-binary matches, or being stored in multiple variables). A reference count tracks how many live references point to that off-heap blob.
flowchart LR
A[Large binary allocated off-heap, refcount=1] --> B[Process A holds a reference]
A --> C[Sub-binary match creates another reference, refcount=2]
B --> D[Process A garbage collected, its reference dropped, refcount=1]
C --> E[Last holder GC'd, refcount=0]
E --> F[Off-heap memory freed]
The blob is only actually freed once its reference count drops to zero, which only happens as a side effect of garbage collecting every process/reference that held a pointer to it. This is precisely why off-heap binaries can outlive what you'd naively expect from looking at process code alone — the memory isn't released the moment a variable "goes out of scope" the way it might in a language with deterministic destructors; it's released only when GC actually runs and drops the last reference.
More Related questions...