Skip to content

Instantly share code, notes, and snippets.

@artemeff
Created Mar 22, 2021
Embed
What would you like to do?
erlang/elixir binary reference counters grouped by their id
defmodule BinaryRefc do
def run(take \\ 50, sort_by \\ :pids_count) do
ref_procs =
Enum.reduce(Process.list(), %{}, fn(pid, acc) ->
[binary: refc_list] = Process.info(pid, [:binary])
Enum.reduce(refc_list, acc, fn({ref, size, _count}, acc) ->
acc
|> Map.put_new({ref, size}, [])
|> Map.update!({ref, size}, fn(list) -> [pid | list] end)
end)
end)
ref_procs
|> Enum.sort_by(sort_by_fn(sort_by), &>=/2)
|> Enum.take(take)
|> Enum.map(fn({{ref, size}, pids}) -> [binary_ref: ref, binary_size: size, pids_count: length(pids), pids: pids] end)
end
defp sort_by_fn(:pids_count) do
fn({_, pids}) -> length(pids) end
end
defp sort_by_fn(:binary_size) do
fn({{_ref, size}, _pids}) -> size end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment