The Ruby Symbol is a Memory Leak

This is just a quick bit that I learned early in my Ruby career and always found interesting.

Symbols Are Memory Leaks

So is this the end of the world?

No

Symbols are a unique thing in ruby. It’s immutable, constant and unique. You can re-use the same Symbol over and over without allocating extra memory. As opposed to using a String over and over which will allocate memory with each instance.

So how does that make it a memory leak?

One key difference between the Symbol and the String is that a Symbol is never garbage-collected. Once the memory is allocated it is never released.

So what’s this mean?

Use a Symbol to represent only something you’ll use again.
Don’t generate random dynamic Symbols you’ll use only once or infrequently, you’ll never get that memory back.

Want to learn more about the Ruby Symbol have some links:
http://www.ruby-doc.org/core-1.9.3/Symbol.html
http://glu.ttono.us/articles/2005/08/19/understanding-ruby-symbols
http://www.tricksonrails.com/2010/06/avoid-memory-leaks-in-ruby-rails-code-and-protect-against-denial-of-service/
http://www.rubytips.org/2008/01/26/what-is-a-ruby-symbol-symbols-explained/