Jesper Louis Andersen
Mon Jan 8 20:20:18 CET 2018
A sub-binary is a pointer into an already existing binary. It is, roughly,
a triple of a pointer to the original binary, an offset into the binary,
and a length (from that offset). They are quick to copy around, and they
are making efficient use of the fact that data in an Erlang system is
However, the caveat is that a sub-binary can keep an underlying binary
"alive" for longer than it should. There are situations in which the ERTS
will convert a sub-binary into a copy with a new binary, and the situation
is slowly getting better and better. But if you want to be sure, you need
to add a binary:copy/1 to a binary to make it into a full-blown
In the example Alex writes, the f(A) in (3) merely forgets the ref-counted
binary pointer. But the binary in (2) and (4) keeps it alive in the system.
On Mon, Jan 8, 2018 at 8:09 PM Frank Muller <frank.muller.erl@REDACTED>
> OTP team,
> Anyone can shed some light on this please?
> Okay, maybe I'm totally confused here, but is it a pointer to the original
>> binary or a new binary? Looking at the source code it seems to build/make
>> a new binary at the end of erts_gc_binary_part(). And from the shell...
>> 1> A = <<"A cookie is yummy!">>.
>> <<"A cookie is yummy!">>
>> 2> B = erlang:binary_part(A, 0, 8).
>> <<"A cookie">>
>> 3> f(A).
>> 4> B.
>> <<"A cookie">>
>> A is gone and we still have B. Does Erlang actually returns references?
>> On 1/7/2018 at 1:21 PM, "Frank Muller" <frank.muller.erl@REDACTED>
>> Thank you Roger!!!
>> It's a sub-binary:
>>> On 6 January 2018 at 21:38, Frank Muller <frank.muller.erl@REDACTED>
>>> > Hi Everyone
>>> > Is the binary returned when calling binary_part/3:
>>> > 1. a sub-binary pointing to the original one
>>> > 2. or a new binary
>>> > Thank you.
>>> > Frank
>>> > _______________________________________________
>>> > erlang-questions mailing list
>>> > erlang-questions@REDACTED
>>> > http://erlang.org/mailman/listinfo/erlang-questions
> erlang-questions mailing list
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the erlang-questions