In this post, we'll show you what atoms are in Elixir, why you should monitor them, and how to do so with AppSignal. Let's prevent your app from crashing with a pop up of an error such as:
1no more index entries in atom_tab (max=1048576)
What Is an Atom in Elixir and How Are They Stored?
An atom is one of the basic types in Elixir. They are named constants, and their value is their name. Atoms are great for sending messages and representing constants; they're meant to be used as a developer tool.
One of the most common use cases of atoms is to use them as the key of a key-value tuple pair, e.g., the response of an HTTP request.
1url = 'http://www.example.com/users'
2
3case HTTPoison.get(url) do
4 {:ok, %HTTPoison.Response{status_code: 200, body: body}} ->
5 IO.puts body
6 {:ok, %HTTPoison.Response{status_code: 404}} ->
7 IO.puts "Not found :("
8 {:error, %HTTPoison.Error{reason: reason}} ->
9 IO.inspect reason
10end
Atoms are everywhere in Elixir. If you're using an Erlang module, you'll notice that they're accessed through an atom. Likewise, Elixir modules are atoms as well!
1iex> Integer == :"Elixir.Integer"
2true
true
, false
, nil
, and aliases are atoms as well.
1iex> true == :true
2true
3iex> false == :false
4true
5iex> nil == :nil
6true
Once declared, atoms are stored in the atom table
using an integer reference.
This table has a default limit of 1048576
. If the limit is hit at runtime,
the application will crash and a crash dump
generated to inform you about it.
As previously mentioned, atoms are meant to be used as developer tools and they should never be created dynamically during an application's runtime.
Creating the Atom Tracking Dashboard on AppSignal
Since AppSignal's motto is "doing all the heavy-lifting so that developers can focus on code", we've added atoms to our Erlang Magic Dashboard. Magic dashboards are automatically created when you add AppSignal to your Elixir application.
Tracking atom creation in your application is a good way of discovering issues in your codebase related to dynamic atom creation. Hitting the atom table entries limit will cause your application to crash.
With AppSignal, you can also set up a dashboard to track atom limit and count. This dashboard will show a graphic with two lines: one for the defined atom limit from your application and the other for the current atom count.
You can also add this graph to a new or existing dashboard with the following JSON:
1{
2 "title": "Erlang Atoms",
3 "description": "",
4 "visuals": [
5 {
6 "title": "Atom usage",
7 "description": "Usage of atoms in Erlang and the configured limit.",
8 "line_label": "%name% %hostname%-%type%",
9 "display": "LINE",
10 "format": "number",
11 "draw_null_as_zero": true,
12 "metrics": [
13 {
14 "name": "erlang_atoms",
15 "fields": [
16 {
17 "field": "GAUGE"
18 }
19 ],
20 "tags": [
21 {
22 "key": "hostname",
23 "value": "*"
24 },
25 {
26 "key": "type",
27 "value": "*"
28 }
29 ]
30 }
31 ],
32 "type": "timeseries"
33 }
34 ]
35}
We've set up a couple of scenarios demonstrating how a healthy and an unhealthy situation can look like.
A Healthy Application With a Constant Number of Atoms in Elixir
This example shows a healthy situation. When the application starts up, the atom count is increased and then it maintains a constant value over time.
Increasing the Number of Atoms in Elixir Over Time
In this example, you can see how the atom counter increases over time, hitting the limit and thus making the application crash.
This might be because we're creating atoms dynamically at runtime. i.e. creating atoms based on arbitrary data sent in a web request, such as user attributes.
What Did We Learn?
If your app is crashing with the error no more index entries in atom_tab (max=1048576)
or not performing as expected, you can take a look at the atom tracking graphics
that can help you find some dynamically created atoms.
Check Out AppSignal, We Have Stroopwafels 🍪
If you haven’t yet tried AppSignal for monitoring your Elixir apps, take five minutes and check it out. There is a 30 day trial with no credit card required.
As we’re a Dutch company, we also ship stroopwafels (crazy good Dutch cookies) worldwide to our users. All you need to do to get your box is start your trial and reach out to our support 📧➡️😋