Ruby Symbol GC Misconception

Its Sunday, but I just finished interviewing a Ruby developer to join my team. He will not be joining the team, but not for reasons posted here.

I showed him a piece of code from that was like…

def hire
  data = JSON.parse(params[:data], symbolize_name: true )
  # more stuff wich included N+1 Query

He was quick to point out that the I am creating symbols from a user input. I was curious why he thought it was a problem and he described that Symbols in Ruby don’t get garbage collected. Knowing it was a little higher than what I expected I asked “which interview question website did you pick it from” and I figured out it was Toptal.

That is not entirely true if you are on Ruby 2.2 or higher.

There are two kinds of Symbols in Ruby. Some are mortal while others are immortal. Most symbols you make dynamically get cleared.

A good article on that matter