defmodule Forum.VmMemory do
  @moduledoc """
  Snapshot of BEAM VM memory categories.

  These numbers come from `:erlang.memory/0`, so they describe memory held by
  the whole VM rather than memory attributed to a particular Erlang process.
  """

  @doc """
  Returns VM memory categories as JSON-friendly maps.
  """
  def list do
    :erlang.memory()
    |> Enum.map(fn {category, bytes} ->
      %{
        "category" => Atom.to_string(category),
        "bytes" => bytes,
        "kb" => div(bytes, 1024),
        "mb" => Float.round(bytes / 1_048_576, 2)
      }
    end)
    |> Enum.sort_by(& &1["bytes"], :desc)
  end
end
