Erlang / Erlang Advanced Interview questions
What is the two-version code loading rule and what happens when a third version is loaded?
The BEAM keeps at most two versions of a module in memory at once: the current version, which new calls resolve to, and the old version, still executing for any process that was mid-loop when the reload happened. This bounded window is what allows hot code upgrades without an unbounded pile-up of stale code.
code:load_file(my_module). %% current becomes old, new module becomes current %% ... later ... code:load_file(my_module). %% loading a THIRD version
If a third version is loaded while some process is still running the old (now second-oldest) version, the
BEAM has nowhere to put it — so it forcibly kills any process still executing that stale version via
code:purge/1, then promotes the new version to current. This is why long-running loops are written
to periodically make a fully-qualified call back into themselves (e.g. ?MODULE:loop(State)): it
gives them a chance to pick up the newer code before a third reload force-terminates them.
More Related questions...