Understanding why atoms are not garbage collected

I went back to building on ruby during an interview and I was asked to explain symbols. It got me thinking about ruby symbols and elixir atoms. One of the big differences between ruby symbols and elixir atoms is that atoms aren’t garbage collected and thus there is a possibility of atom exhaustion.

I went further and learned that in the older ruby days(ruby < 2.2) there was also a possibility of symbol exhaustion(https://stackoverflow.com/questions/16621073/when-to-use-symbols-instead-of-strings-in-ruby).

So my question is still why aren’t atoms GC’d. My first thought is because module names and some keywords like false, true and nil are internally represented as atoms it is better to have them in memory the whole time. But that doesn’t seem correct to me.

And is there a way to manually free an atom once it has been declared and you are sure it is no longer being used?

1 Like

There is no such thing as “Elixir atom,” it’s rather “Erlang atom.”

There is a very descriptive answer to your question by one of Erlnag core team members.

7 Likes

Ah my mistake, erlang atoms. It is an intersting read. I see that the discussion is pretty old. Interesting to see that even the Prolog community had to make a choice about GC:ing atoms.

Well, technically, counters and atomics might be taken as a proof of concept. Making atom table mutable is surely possible nowadays without much hussle and without a huge performance penalty.

The question is whether it’s really needed.

1 Like

It is the GC which is the problem. If you want to garbage collect atoms then you need to do global GC which would either creates breaks in execution, which is totally unacceptable, or be very costly in memory or execution. @garazdawi in his answer goes through this.

7 Likes