Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Account for the old generation in memory measurements #216

Merged
merged 1 commit into from
May 1, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions lib/benchee/benchmark/measure/memory.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,15 @@ defmodule Benchee.Benchmark.Measure.Memory do

defp measure_memory(fun, tracer) do
word_size = :erlang.system_info(:wordsize)
{:garbage_collection_info, info_before} = Process.info(self(), :garbage_collection_info)
{:garbage_collection_info, heap_before} = Process.info(self(), :garbage_collection_info)
result = fun.()
{:garbage_collection_info, info_after} = Process.info(self(), :garbage_collection_info)
{:garbage_collection_info, heap_after} = Process.info(self(), :garbage_collection_info)
mem_collected = get_collected_memory(tracer)

{(info_after[:heap_size] - info_before[:heap_size] + mem_collected) * word_size, result}
memory_used =
(total_memory(heap_after) - total_memory(heap_before) + mem_collected) * word_size

{memory_used, result}
end

@spec graceful_exit(Exception.kind(), any(), pid(), pid()) :: no_return
Expand Down Expand Up @@ -80,10 +83,10 @@ defmodule Benchee.Benchmark.Measure.Memory do
send(reply_to, {ref, acc})

{:trace, ^pid, :gc_minor_start, info} ->
listen_gc_end(pid, :gc_minor_end, acc, Keyword.fetch!(info, :heap_size))
listen_gc_end(pid, :gc_minor_end, acc, total_memory(info))

{:trace, ^pid, :gc_major_start, info} ->
listen_gc_end(pid, :gc_major_end, acc, Keyword.fetch!(info, :heap_size))
listen_gc_end(pid, :gc_major_end, acc, total_memory(info))

:done ->
exit(:normal)
Expand All @@ -93,8 +96,15 @@ defmodule Benchee.Benchmark.Measure.Memory do
defp listen_gc_end(pid, tag, acc, mem_before) do
receive do
{:trace, ^pid, ^tag, info} ->
mem_after = Keyword.fetch!(info, :heap_size)
mem_after = total_memory(info)
tracer_loop(pid, acc + mem_before - mem_after)
end
end

defp total_memory(info) do
# `:heap_size` seems to only contain the memory size of the youngest
# generation `:old_heap_size` has the old generation. There is also
# `:recent_size` but that seems to already be accounted for.
Keyword.fetch!(info, :heap_size) + Keyword.fetch!(info, :old_heap_size)
end
end