How Erlang atoms can be garbage collected

Atoms aren't issue unless you are creating them dynamically. If you did that, then you are on your way to crash an Erlang system.

How to create Atoms dynamically? For example calling list_to_atom function inside a loop.

If you are interested in Erlang garbage collection, then read this paper by Joe Armstrong: One Pass Real-Time Generational Mark-Sweep Garbage Collection (1995).

Always keep in mind: Don't create Atoms dynamically!
Well sometimes you might need to create an Atom dynamically BUT don't over use it!


While I'm not sure atoms are garbage-collected, you can easily do without worrying whether you will blow up the system's memory. As @Chiron said, as long as all your atoms are known at compile time you should be ok.

What if I really need to use list_to_atom/1 somehow? Well, you may be able to twist your issue using this kind of function:

atom("apple") -> apple;
atom("orange") -> orange;
atom("banana") -> banana.

One other workaround is list_to_existing_atom/1

But the VM can still eat more and more RAM: other connected Erlang nodes may register atoms globally, that is allocate atoms at run time.