@@ -40,12 +40,15 @@ defmodule Benchee.Benchmark.Measure.Memory do
40
40
41
41
defp measure_memory ( fun , tracer ) do
42
42
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 )
44
44
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 )
46
46
mem_collected = get_collected_memory ( tracer )
47
47
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 }
49
52
end
50
53
51
54
@ spec graceful_exit ( Exception . kind ( ) , any ( ) , pid ( ) , pid ( ) ) :: no_return
@@ -80,10 +83,10 @@ defmodule Benchee.Benchmark.Measure.Memory do
80
83
send ( reply_to , { ref , acc } )
81
84
82
85
{ :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 ) )
84
87
85
88
{ :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 ) )
87
90
88
91
:done ->
89
92
exit ( :normal )
@@ -93,8 +96,15 @@ defmodule Benchee.Benchmark.Measure.Memory do
93
96
defp listen_gc_end ( pid , tag , acc , mem_before ) do
94
97
receive do
95
98
{ :trace , ^ pid , ^ tag , info } ->
96
- mem_after = Keyword . fetch! ( info , :heap_size )
99
+ mem_after = total_memory ( info )
97
100
tracer_loop ( pid , acc + mem_before - mem_after )
98
101
end
99
102
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
100
110
end
0 commit comments