Show the correct amount of free memory available on the system (#1179)

* Show the correct amount of free memory available on the system

Attempts to use the `available_memory` stat, otherwise tries to
cobble it together using cached+buffered+free as per the erlang
docs on `:memsup:get_system_memory_data` and only failing that
flals back to `free_memory`

* Use `Map.new` rather than `Enum.into`

Co-authored-by: Jonatan Kłosko <jonatanklosko@gmail.com>

* Update lib/livebook/system_resources.ex

Co-authored-by: Jonatan Kłosko <jonatanklosko@gmail.com>
This commit is contained in:
Aaron Seigo 2022-05-09 15:43:00 +02:00 committed by GitHub
parent f5afac9497
commit 2a8c95c388
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -66,11 +66,21 @@ defmodule Livebook.SystemResources do
defp measure() do
memory_data = :memsup.get_system_memory_data()
memory = %{total: memory_data[:total_memory], free: memory_data[:free_memory]}
free_memory = free_memory(Map.new(memory_data))
memory = %{total: memory_data[:total_memory], free: free_memory}
:ets.insert(@name, {:memory, memory})
memory
end
defp free_memory(%{available_memory: available}), do: available
defp free_memory(%{cached_memory: cached, buffered_memory: buffered, free_memory: free}) do
cached + buffered + free
end
defp free_memory(%{free_memory: free}), do: free
defp free_memory(_), do: 0
defp schedule() do
Process.send_after(self(), :measure, 15000)
end