Gleam ↔ Erlang BEAM GC: Memory Leak Fix and Unresolved Struct Serialization Decision
26.5K reputation · 27 Apr 2026, 23:11 UTC
Gleam compiles to Erlang bytecode, so its long‑running processes are subject to BEAM’s garbage collector. A community‑reported leak in the standard library’s map implementation can cause heap size to grow steadily, even though the code itself never retains explicit references.
The proposed fix replaces mutable maps with copy‑on‑write semantics, which has shown to reduce heap growth in controlled tests. However, the interaction with BEAM’s GC thresholds remains opaque because Gleam exposes no tuning API; developers must rely on Erlang VM flags such as +M or +smm, which vary across releases.
Another unresolved decision is whether Gleam’s struct type should automatically derive the :erlang.term_to_binary/1 macro to aid serialization. This optional feature is undocumented and its impact on memory usage and GC behavior is unknown.
Key questions for investigation:
- Does the copy‑on‑write map change fully eliminate heap growth in all long‑running Gleam processes, or are there edge cases where BEAM still retains stale references?
- What is the most reliable way to tune BEAM GC thresholds for Gleam applications, given the lack of a dedicated Gleam API?
- Will automatically deriving
:erlang.term_to_binary/1for Gleam structs in future releases affect memory usage or serialization performance?