Symbol GC in Ruby 2.2

Originally published at: http://www.sitepoint.com/symbol-gc-ruby-2-2/

What is symbol GC and why should you care? Ruby 2.2 was just released and, in addition to incremental GC, one of the other big features is Symbol GC. If you’ve been around the Ruby landscape, you’ve heard the term “symbol DoS”. A symbol denial of service attack occurs when a system creates so many symbols that it runs out of memory. This is because, prior to Ruby 2.2, symbols lived forever. For example in Ruby 2.1:

Continue reading this article on SitePoint

One of the best features of the last 8th years, by far.

This has been one of te weakest points of Ruby for any interactive app, and clearly one of the most common weak point of any Rails app out there. (It has also been one of my favourite Ruby’s tales for new comers :slight_smile: )

This “bug” has actually changed the respect we have towards Symbols, and even the way/place we use them, making us pick Strings in a lot of cases or at least forcing us to add dumb validations here and there.

Personally, I celebrate Symbol GC even more than named params or even some of the performance improvements.

Symbols, welcome back!

A few clarifications for JRuby:

  • JRuby mimicked MRI’s non-GCable Symbols in earlier versions, but JRuby 9000 – which will ship with Ruby 2.2 support – makes Symbols GCable just like MRI.
  • In JRuby, we do not use Symbols for IDs like method and constant names, so they will not cause an “immortal symbol” to be created.

If you find another really common gotcha, reach out to me on twitter and I’ll try to keep this section updated.

Please submit a patch for the Ruby security doc if you find more! https://github.com/ruby/ruby/blob/trunk/doc/security.rdoc#symbols

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.