[erlang-questions] How to examine binaries identified by erlang:memory/2?

Scott Lystig Fritchie fritchie@REDACTED
Tue Mar 17 06:50:06 CET 2009


Paul Fisher <pfisher@REDACTED> wrote:

pf> My question: Is there any way to identify the "leaked" binaries?  If
pf> I could look at a few, it would probably be enough to figure out the
pf> culprit immediately.

Not that I'm aware of.  However, I can suggest at least one place to
start looking.  (I've been bitten by this, very unpleasant, yes.)

If I'm not wildly mistaken (and I could be, I really should be asleep
right now), then doing this a few thousand times will "leak" a lot of
memory, apparently in binaries.

    BigBin = list_to_binary(lists:duplicate(10*1024*1024, $x)),
    <<_:8777666/binary, Desired:90/binary, _/binary>> = BigBin,
    ets:insert(SomeTable, {SomeKey, ..., Desired, ...}),

IIRC, according to section 4.1 of the Erlang Efficiency Guide, BigBin
will be a reference counted binary.  Desired will be a sub binary.  Both
will refer to the 10 MBytes in BigBin's blob area thingie handwaving
really- ought-to-go-to-sleep.  BigBin's ref count will be two and will
drop to one after BigBin is GC'ed, but it won't drop to zero until the
reference buried in the SomeTable ETS table goes away.

If I've mangled the details, I hope a soul both enlightened and
wide-awake can correct me.

-Scott



More information about the erlang-questions mailing list