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?


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: