Skip to content

Instantly share code, notes, and snippets.

@artemeff
Created March 22, 2021 12:24
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 artemeff/36efa714c1d34eaf6040ad1809688003 to your computer and use it in GitHub Desktop.
Save artemeff/36efa714c1d34eaf6040ad1809688003 to your computer and use it in GitHub Desktop.
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