         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)

  defp listen_gc_end(pid, tag, acc, mem_before) do
    receive do
      {:trace, ^pid, ^tag, info} ->
        mem_after = Keyword.fetch!(info, :heap_size)
        tracer_loop(pid, acc + mem_before - mem_after)
    end
  end
end


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

    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
