So garbage collection of symbols has been introduced from Ruby 2.2+ version. I wrote the following the code snippet in irb :
before = Symbol.all_symbols.size #=>3331
100_000.times do |i|
"sym#{i}".to_sym
end
Symbol.all_symbols.size #=> 18835
GC.start
Symbol.all_symbols.size #=>3331
So as expected it collected all the symbols generated dynamically with to_sym
.
So how does the GC know which symbols to collect? Would it collect the symbols even if they were referenced in the program? How does symbol garbage collection work? If one of the symbols I created were being referenced in the program would it still collect it?
I am using Ruby 2.2.1.