Just a small benchmark to clear up the myth, Ruby wouldn'd garbage collect any Symbols. In fact, MRI 2.2.0 introduced Symbol GC in December 2014 (see notable changes in the release announcement).
The benchmark is quite simple: It basically cretes a bunch of Symbols via String#to_sym
and counts Symbol::all_symbols
afterwards.
I've used rbenv to test the Symbol GC (or the lack thereof) of these interpreter implementations:
- 1.9.3-p551
- 2.0.0-p645
- 2.1.6
- 2.2.2
- rbx-2.5.5
- jruby-1.7.20
- jruby-9000-dev
To get the results by yourself, simply execute bench.rb
in the interpreter of your choice.
1861
Symbols after boot1001861
Symbols after generating 10000001001861
Symbols after GC'ing
Finished in 2.68s
2298
Symbols after boot1002298
Symbols after generating 10000001002298
Symbols after GC'ing
Finished in 2.34s
2418
Symbols after boot1002418
Symbols after generating 10000001002418
Symbols after GC'ing
Finished in 2.29s
2524
Symbols after boot2888
Symbols after generating 10000002524
Symbols after GC'ing
Finished in 1.76s
8413
Symbols after boot1008413
Symbols after generating 10000001008413
Symbols after GC'ing
Finished in 2.59s
965
Symbols after boot1000965
Symbols after generating 10000001000965
Symbols after GC'ing
Finished in 3.30s
973
Symbols after boot636418
Symbols after generating 1000000636418
Symbols after GC'ing
Finished in 3.60s