[erlang-questions] Troubleshooting binary memory usage

ash <>
Fri Feb 10 12:28:02 CET 2017


Hi,
I've been getting high values for `erlang:memory(binary)` so I've been 
looking at the following:
(1) `[erlang:process_info(Pid, binary) || Pid <- processes()]`
(2) `instrument:memory_data/0`
(3) `erlang:system_info({allocator, binary_alloc})`
Unfortunately no two of these sources agree. For example, I can try to 
compare the number of live binaries according to the different data 
sources:
```
(fun () ->
          ProcInfoCount = dict:size(lists:foldl(
                                      fun(Pid, D) ->
                                              case 
erlang:process_info(Pid, binary) of
                                                  {binary, Bins} ->
                                                      lists:foldl(
                                                        fun({Addr,_,_}, 
D1) ->
                                                                
dict:update_counter(Addr,1,D1)
                                                        end,
                                                        D,
                                                        Bins);
                                                  Other -> D
                                              end
                                      end,
                                      dict:new(),
                                      processes())),
          {_,Data}=instrument:descr(instrument:memory_data()),
          InstrumentCount = lists:foldl(fun (T={binary,_,_,_},Acc) -> 
Acc + 1;
                                            (_,Acc) -> Acc
                                        end,
                                        0,
                                        Data),
          GetCallCount = fun(K, Calls) ->
                                 case lists:keyfind(K, 1, Calls) of
                                     {_, X, Y} -> X*1000000000+Y;
                                     _ -> 0
                                 end
                         end,
          AllocCount = lists:foldl(fun({instance,_,PropList}, A) ->
                                           Calls = 
proplists:get_value(calls, PropList, []),
                                           Allocs = 
GetCallCount(binary_alloc, Calls),
                                           Frees = 
GetCallCount(binary_free, Calls),
                                           A + (Allocs - Frees)
                                   end,
                                   0,
                                   erlang:system_info({allocator, 
binary_alloc})),
          {ProcInfoCount, InstrumentCount, AllocCount}
  end)().
```
This yields `{2874,6371,7715}` at time of writing.
Can anyone suggest reasons for the different counts? I've found that 
some processes show up in (2) but not (1), but I don't know why this is.
Thanks,
Ash


More information about the erlang-questions mailing list