[erlang-questions] load_file() and purge()

Per Hedeland per@REDACTED
Fri Nov 25 13:38:46 CET 2016


On 2016-11-25 13:20, Salikhov Dinislam wrote:
> 
> Let's assume there is a module forty_two.beam loaded by VM.
> Then the module is updated and hot loaded:
>     > code:load_file(forty_two).
>     {module, forty_two}
> 
> Then let's assume that there is no process using an old version of the module (for example, the module contains only pure functions).
> Despite of that following attempt to update the module fails:
>     > code:load_file(forty_two).
>     Loading of forty_two.beam failed: not_purged
>     {error,not_purged}
> 
> Is there any rationale why the old code is not automatically purged?

code:load_file/1 *never* purges old code. Try the handy shell function
l/1, defined in c.erl:

l(Mod) ->
    code:purge(Mod),
    code:load_file(Mod).

> IMO, it would be convenient if _unused_ old version of the code would be implicitly removed in such case.

You could define your own load function with appropriate use of
code:soft_purge/1 for that.

--Per Hedeland



More information about the erlang-questions mailing list