Skip to content

Instantly share code, notes, and snippets.

@bachue
Last active December 10, 2015 22:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bachue/4502552 to your computer and use it in GitHub Desktop.
Save bachue/4502552 to your computer and use it in GitHub Desktop.
A test to prove Symbol can't be GC in both Ruby 1.9 and Ruby 1.8
MAX_LENGTH = 10
def generate(base_str)
[('a'..'z'), ('A'..'Z'), ('0'..'9')].map(&:to_a).flatten.each do |char|
str = base_str + char
str.to_sym # You can comment this statement and compare the result
generate(str) if str.size <= MAX_LENGTH
end
end
main = Thread.start {
generate('')
Thread.kill(counter)
puts 'generating is over ...'
}
counter = Thread.start {
loop do
GC.start # GC here to free memery
puts "Current number of Objects: " + ObjectSpace.each_object(Object).count.to_s
sleep 1
end
}
counter.join
main.join
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment