Skip to content

Instantly share code, notes, and snippets.

@stolen
Last active February 4, 2016 22:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stolen/1e562b7c21258efaa870 to your computer and use it in GitHub Desktop.
Save stolen/1e562b7c21258efaa870 to your computer and use it in GitHub Desktop.
-module(bininspector).
% For inspecting binary usage by all accessible ETS tables, run
% [{T, (catch ets:foldl(fun bininspector:collect/2, {0, 0}, T))} || T <- ets:all()].
-export([collect/2]).
collect(B, {S, R}) when is_bitstring(B) ->
{S + byte_size(B), R + binary:referenced_byte_size(B)};
collect(T, Acc) when is_tuple(T) ->
collect(tuple_to_list(T), Acc);
collect(L, Acc) when is_list(L) ->
lists:foldl(fun collect/2, Acc, L);
collect(M, Acc) when is_map(M) ->
maps:fold(fun(K, V, AccIn) -> collect(K, collect(V, AccIn)) end, Acc, M);
collect(F, Acc) when is_function(F) ->
{_, Env} = erlang:fun_info(F, env),
BoundVals = [Val || {Bindings, _, _, _} <- Env, {_, Val} <- Bindings],
collect(BoundVals, Acc);
collect(X, Acc) when is_number(X); is_atom(X); is_reference(X); is_pid(X); is_port(X) ->
Acc.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment