[erlang-questions] question about binary memory

Dmytro Lytovchenko dmytro.lytovchenko@REDACTED
Mon Sep 19 17:44:03 CEST 2016


Binary memory stores all binary data larger than 64 bytes. Every process
that had such a binary holds a reference-counted pointer to this memory.
When garbage collection runs, unused refc pointers are detected and
refcount is reduced. At 0 binary will be freed.

This means that if more than 1 process has seen certain large binary, ALL
of them must have GC before memory is freed. If you use hibernate on
processes, this problem is solved automatically by hibernate procedure,
otherwise it may take long time before all processes in chain have had the
GC. You can assist them and AFTER your processing has finished, call
erlang:garbage_collect manually.

mån 19 sep. 2016 kl 17:26 skrev 叶少波 <shaobo.ye@REDACTED>:

>
> Hi,
>
> My question is about the binary memory usage.
> I implemented a server which accepts Tcp connection. All the Tcp
> connections use binary options.
>
> Before I set up 10000 Tcp connections to my server, the memory usage is as
> below:
> (node1@REDACTED)1> erlang:memory().
> [{total,100311433},
>  {processes,24898112},
>  {processes_used,24895144},
>  {system,75413321},
>  {atom,1517705},
>  {atom_used,1506108},
>  {binary,3115928},
>  {code,48956174},
>  {ets,3556416},
>  {maximum,102757346}]
>
> after I set up 10000 Tcp connections, every connection is hold by one
> gen_server; the memory usage is as below:
> (node1@REDACTED)5>erlang:memory().
> [{total,2616776425},
>  {processes,92150752},
>  {processes_used,92138976},
>  {system,2524625673},
>  {atom,1517705},
>  {atom_used,1506108},
>  {binary,2437117184},
>  {code,48956174},
>  {ets,13264464},
>  {maximum,2848153566}]
>
> And I check the binary hold by all the processes on the node with the
> command below:
> List = [begin {binary, L} = erlang:process_info(P, binary), {P,
> (lists:foldl(fun({_, I, _}, Acc) -> Acc + I end, 0, L))} end || P <-
> processes() -- [self()]], lists:foldl(fun({_, A}, Acc) -> A + Acc end, 0,
> List).
> the result is 23113860.  It is much less than the value 2437117184 reported
> by erlang:memory().
>
> My question is :
> why does the VM allocate so much memory for binary?
> Does the Tcp ports allocate their send/recv buffer on binary heap?
> Is there any way I can see what are in the binary heap?
>
> Thanks,
> BRs/Michael
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20160919/6dcc0924/attachment.htm>


More information about the erlang-questions mailing list