Take the 2-minute tour ×

If you create 10,000 strings in a loop, a lot of garbage collection has to take place which uses up a lot of resources.

If you do the same thing with symbols, you create objects which cannot be garbage collected.

Which is worse?

share|improve this question

3 Answers 3

If you refer to the same symbol in your loop, then it doesn't have to recreate that object everytime i.e.

while i < 10000
  i += 1

Now if you use a string there instead, the string will be recreated 10K times. In general, use symbols in cases where you almost treat the literal like a constant or a key. A very good example for me would be

link_to "News", :action => 'news'

instead of

link_to "News", "action" => 'news'

action being re-used over and over again within your application.

share|improve this answer
I can add a rule of thumb: never, ever, convert any user-supplied strings to Symbols. That would open a possibility of a DOS attack. –  Arsen7 Jul 19 '11 at 9:58
up vote 6 down vote accepted

Seeing as symbols are almost always created via literals, there isn't much potential for a memory explosion here. Their behavior is pretty much required by their usage: every time you refer to a symbol, it's the same one.

Similarly, strings need to be unique in Ruby. This is due to the way they're used - text processing etc.

Decide which one to use depending on their semantics, don't optimize prematurely.

share|improve this answer

If you are using Ruby 2.2.0 or later, it should usually be OK to dynamically create a lot of symbols, because they will be garbage collected according to the Ruby 2.2.0-preview1 announcement, which has a link to more details about the new symbol GC. However, if you pass your dynamic symbols to some kind of code that converts it to an ID (an internal Ruby implementation concept used in the C source code), then in that case it will get pinned and never get garbage collected. I'm not sure how commonly that happens.

When deciding whether to use symbols or strings you should consider:

  • Symbols cannot be changed after they are created.
  • Symbols do not have a lot of the methods that strings have, like start_with?
  • Symbols can very efficiently be compared to eachother for equality.
  • Symbols are supposed to represent the name of something according to the Symbol docs. I wouldn't use them to store anything that couldn't be considered a name.
share|improve this answer

Your Answer

