Mortal and Immortal Symbols in Ruby

Symbols are one of the most powerful datatypes that Ruby has to offer. Compare
and lookup operations are faster when symbols are used rather than strings. You
can check this post here know more about symbols and their performance when compared to strings.

One disadvantage of using symbols is that, they live in the program’s memory
for the entire life cycle of it’s execution. This is one of the reasons for faster symbol lookups. This also was the reason for many denial of service vulnerabilites in Ruby, for example this one.

From Ruby 2.2, there is a new concept of Mortal and Immortal Symbols. Internally
ruby has three types of symbols. Mortal dynamic symbols, which are symbols
created dynamically by user, Immortal dynamic symbols are the mortal dynamic symbols
that have been promoted to immortal dynamic symbols and then there are immortal
static symbols, which are function names, class names etc. The GC only collects the
Mortal dynamic symbols. An example of such symbols are symbols created by to_sym method.

Share Comments

Hello World

Hi there! I just setup this webpage. As of now, I haven’t started writing any blogs yet, but the idea is to start soon. Most of them will be about stuff related to Programming or Math.

So, until then stay awesome and thank you.

Share Comments