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?
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?
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.
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
:im_using_this_symbol_here
end
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.