Created
March 22, 2021 12:24
-
-
Save artemeff/36efa714c1d34eaf6040ad1809688003 to your computer and use it in GitHub Desktop.
erlang/elixir binary reference counters grouped by their id
This file contains 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
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