[erlang-questions] Rules regarding the refcount of binaries

Ulf Wiger <>
Wed Jun 3 21:31:38 CEST 2009


John Haugeland wrote:
>
> Are the rules for when binaries are reference counted and what the
> results thereof are well documented somewhere?  Several threads on
> this mailing list have implied that the use of large binaries in
> programs has led to what appear to be memory leaks, which in turn
> suggests that refcounted binaries aren't destroyed when their
> reference count hits zero.

Hi John,

The issue, as I understand it, is that if you extract small
binaries out of bigger ones, the smaller binaries will be
represented as "sub-binaries" pointing into the larger ones.
This will mean that the refcound of the bigger binaries will
never hit zero.

A naive workaround is to copy the smaller binaries. Not sure
exactly what the best way is, but (and I believe I've had a
few beers too many tonight to come up with an optimal suggestion
right now*) list_to_binary(binary_to_list(Bin)) is sure to work. :)

If the binaries are in fact reasonably small, this will not
be terribly expensive...

* Esteemed colleague Jan Henry Nytstrom successfully defended
his PhD thesis, and we had to celebrate, of course.

BR,
Ulf W


More information about the erlang-questions mailing list