Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Created September 19, 2012 05:20
Show Gist options
  • Save JoshCheek/3747823 to your computer and use it in GitHub Desktop.
Save JoshCheek/3747823 to your computer and use it in GitHub Desktop.
Ruby gc not collecting array
THE_STRING = 'a random 369-characterish string'
def self.find_array
ObjectSpace.each_object Array do |array|
return array if array.any? && array.all? { |s| s == THE_STRING }
end
nil
end
def self.print_whether_array_exists
if array = find_array
puts "IT EXISTS WITH SIZE #{array.size}"
else
puts "IT DOES NOT EXIST"
end
end
array = []
require 'time'
time = Time.now + 2
array << THE_STRING while time > Time.now
print_whether_array_exists
array = nil
GC.enable
GC.start
print_whether_array_exists
@JoshCheek
Copy link
Author

JoshCheek commented Sep 19, 2012

Looks like a bug in MRI to me:

$ ruby -v gc.rb
ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-darwin15]
IT EXISTS WITH SIZE 2716941
IT EXISTS WITH SIZE 2716941

Rubinius GC's it as expected:

$ ruby -v gc.rb
rubinius 3.48 (2.3.1 b39b6dda 2016-07-20 3.6.2) [x86_64-darwin15.5.0]
IT EXISTS WITH SIZE 2042403
IT DOES NOT EXIST

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment