Garbage collection of binaries

Jesper Wilhelmsson jesperw@REDACTED
Tue Oct 14 16:59:44 CEST 2003


On Mon, 13 Oct 2003, James Hague wrote:

> If I write a tail recursive function that loads a large file as a
> binary--say, 10MB--on each iteration, then the emulator quickly runs out of
> memory and dies.  This is apparently because binaries are only garbage
> collected when the regular garbage collector runs, and with large binaries
> it's easy to run out of memory before then.  It's easy to get this to happen
> in a real application.
>
> One hacky solution is to call erlang:garbage_collect all over the place,
> but...yuck.  Is this being addressed in the run-time system at some point?
> Any other workarounds?

Binaries are referenced counted and are removed when their conter reach
zero. This can happen in the garbage collector as you say, but it can also
happen when the process owning it dies.
So, I guess a workaround could be to let a new process handle the binary
in each iteration, and let it die afterwards.

I would of course be possible (and a bit more efficient) to add a BIF that
removes the binary explicitly, but from what I have seen in this list,
introducing new functionality (and in particular one that destructively
removes things from memory) is not that popular right now ;-)
    __
___(  |_______________________  _______________________________________________
    | | ,---. ,--.,--.,--.   ( (
    | ||  _  || o ) o ) p )   ) ) "Beware of bugs in the above code;
    | || (_) || r'| r'| -×--.( (  I have only proved it correct, not tried it."
o,--' | `---' |_| |_| `-----' ) )                               -- Donald Knuth
_`----'______________________( (_______________________________________________
Jesper Wilhelmsson, jesperw@REDACTED                         +46 (0)18 471 1046
Computing Science Department, Uppsala University, Sweden     +46 (0)733 207 207
-------------------------------------------------------------------------------




More information about the erlang-questions mailing list