Last active
February 4, 2016 22:36
-
-
Save stolen/1e562b7c21258efaa870 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-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