Open leoarnold opened 2 years ago
It turns out that there is way more leaking than just the return value of the block:
# frozen_string_literal: true
require 'bundler/inline'
gemfile(true) do
source 'https://rubygems.org'
gem 'concurrent-ruby', '1.1.10'
gem 'memory_profiler', '~> 1'
end
class Thing; end
def report(title, &block)
puts title
pp MemoryProfiler.report(&block).retained_memory_by_class
end
report('Warmup') do
Concurrent::Future.execute { Thing.new }.wait
end
report('When waiting for the Future') do
Concurrent::Future.execute { Thing.new }.wait
end
report('When waiting for the Future and actively dereferencing it') do
x = Concurrent::Future.execute { Thing.new }.wait
x = nil
end
yields the output:
Fetching gem metadata from https://rubygems.org/..
Resolving dependencies...
Using bundler 2.3.19
Using memory_profiler 1.0.0
Using concurrent-ruby 1.1.10
Warmup
[{:data=>"Thread", :count=>1048992},
{:data=>"Array", :count=>240},
{:data=>"Concurrent::CachedThreadPool", :count=>216},
{:data=>"Thread::Mutex", :count=>216},
{:data=>"Thread::ConditionVariable", :count=>192},
{:data=>"Concurrent::Event", :count=>144},
{:data=>"Proc", :count=>80},
{:data=>"String", :count=>80},
{:data=>"Thread::Queue", :count=>76},
{:data=>"Concurrent::RubyThreadPoolExecutor::Worker", :count=>40},
{:data=>"Thing", :count=>40}]
When waiting for the Future
[{:data=>"Array", :count=>80}, {:data=>"Thing", :count=>40}]
When waiting for the Future and actively dereferencing it
[{:data=>"Array", :count=>80}, {:data=>"Thing", :count=>40}]
Maybe this "leaking" of the Array
objects is intentional, i.e. keeping a pool of sub-threads in a thread-local variable which is then garbage collected when the parent thread is garbage collected :thinking:
When passing a
block
toConcurrent::Future.execute
andwait
ing for the result, theFuture
can be garbage collected, but the return value of the block seems to stay in memory forever, as demonstrated by the following script:When running the script on several different versions of MRI, I always get something like this never ending output: