Beware of “String.to_atom”

Boobalan AP
Nov 9, 2020

--

https://unsplash.com/photos/OgvqXGL7XO4

Beware of converting any data type to atom dynamically, as atoms once created won’t be garbage collected.

We may not face this scenario often. But in scenarios where atoms are required, we might overlook this and convert dynamic values to atoms.

One such case is registering dynamic GenServer or Agent with a name.

Use {:global, term} or via_tuple to handle such scenarios appropriately.

More info at

https://hexdocs.pm/elixir/String.html#to_atom/1

https://hexdocs.pm/elixir/GenServer.html#module-name-registration

--

--

Boobalan AP
Boobalan AP

No responses yet