Skip to content

Instantly share code, notes, and snippets.

@dmke
Last active August 29, 2015 14:22
Show Gist options
  • Save dmke/1e851b13c096916f1626 to your computer and use it in GitHub Desktop.
Save dmke/1e851b13c096916f1626 to your computer and use it in GitHub Desktop.
Ruby Symbol GC

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.

Results

Engine: ruby, Version 1.9.3 (rbenv 1.9.3-p551)

  • 1861 Symbols after boot
  • 1001861 Symbols after generating 1000000
  • 1001861 Symbols after GC'ing

Finished in 2.68s

Engine: ruby, Version 2.0.0 (rbenv 2.0.0-p645)

  • 2298 Symbols after boot
  • 1002298 Symbols after generating 1000000
  • 1002298 Symbols after GC'ing

Finished in 2.34s

Engine: ruby, Version 2.1.6 (rbenv 2.1.6)

  • 2418 Symbols after boot
  • 1002418 Symbols after generating 1000000
  • 1002418 Symbols after GC'ing

Finished in 2.29s

Engine: ruby, Version 2.2.2 (rbenv 2.2.2)

  • 2524 Symbols after boot
  • 2888 Symbols after generating 1000000
  • 2524 Symbols after GC'ing

Finished in 1.76s

Engine: rbx, Version 2.1.0 (rbenv rbx-2.5.5)

  • 8413 Symbols after boot
  • 1008413 Symbols after generating 1000000
  • 1008413 Symbols after GC'ing

Finished in 2.59s

Engine: jruby, Version 1.9.3 (rbenv jruby-1.7.20)

  • 965 Symbols after boot
  • 1000965 Symbols after generating 1000000
  • 1000965 Symbols after GC'ing

Finished in 3.30s

Engine: jruby, Version 2.2.0 (rbenv jruby-9000-dev)

  • 973 Symbols after boot
  • 636418 Symbols after generating 1000000
  • 636418 Symbols after GC'ing

Finished in 3.60s

#!/usr/bin/env ruby
start = Time.now
N = (ARGV[0] || 1_000_000).to_i
def make_symbol(i)
name = "symbol_%10d" % i
name.to_sym
end
printf "## Engine: %s, Version %s (rbenv %s)\n\n", RUBY_ENGINE, RUBY_VERSION, ENV["RBENV_VERSION"]
printf "- `%d` Symbols after boot\n", Symbol.all_symbols.size
N.times {|i| make_symbol(i) }
printf "- `%d` Symbols after generating %d \n", Symbol.all_symbols.size, N
GC.start
printf "- `%d` Symbols after GC'ing\n\n", Symbol.all_symbols.size
printf "Finished in %.02fs\n\n", (Time.now - start)
#!/bin/sh
rubies="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"
for r in $rubies; do
RBENV_VERSION=$r ruby ./bench.rb $*
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment