Skip to content

Commit 3781a8e

Browse files
authored
Merge pull request #216 from PragTob/account-for-old-generation-memory-measurement
Account for the old generation in memory measurements
2 parents 4fb3e4f + ab26948 commit 3781a8e

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

lib/benchee/benchmark/measure/memory.ex

+16-6
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,15 @@ defmodule Benchee.Benchmark.Measure.Memory do
4040

4141
defp measure_memory(fun, tracer) do
4242
word_size = :erlang.system_info(:wordsize)
43-
{:garbage_collection_info, info_before} = Process.info(self(), :garbage_collection_info)
43+
{:garbage_collection_info, heap_before} = Process.info(self(), :garbage_collection_info)
4444
result = fun.()
45-
{:garbage_collection_info, info_after} = Process.info(self(), :garbage_collection_info)
45+
{:garbage_collection_info, heap_after} = Process.info(self(), :garbage_collection_info)
4646
mem_collected = get_collected_memory(tracer)
4747

48-
{(info_after[:heap_size] - info_before[:heap_size] + mem_collected) * word_size, result}
48+
memory_used =
49+
(total_memory(heap_after) - total_memory(heap_before) + mem_collected) * word_size
50+
51+
{memory_used, result}
4952
end
5053

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

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

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

8891
:done ->
8992
exit(:normal)
@@ -93,8 +96,15 @@ defmodule Benchee.Benchmark.Measure.Memory do
9396
defp listen_gc_end(pid, tag, acc, mem_before) do
9497
receive do
9598
{:trace, ^pid, ^tag, info} ->
96-
mem_after = Keyword.fetch!(info, :heap_size)
99+
mem_after = total_memory(info)
97100
tracer_loop(pid, acc + mem_before - mem_after)
98101
end
99102
end
103+
104+
defp total_memory(info) do
105+
# `:heap_size` seems to only contain the memory size of the youngest
106+
# generation `:old_heap_size` has the old generation. There is also
107+
# `:recent_size` but that seems to already be accounted for.
108+
Keyword.fetch!(info, :heap_size) + Keyword.fetch!(info, :old_heap_size)
109+
end
100110
end

0 commit comments

Comments
 (0)