Memory
A good start when programming efficiently is to know
how much memory different data types and operations require. It is
implementation-dependent how much memory the Erlang data types and
other items consume, but the following table shows some figures for
the erts-5.2 system in R9B. There have been no significant
changes in R13.
The unit of measurement is memory words. There exists both a
32-bit and a 64-bit implementation. A word is therefore 4 bytes or
8 bytes, respectively.
Data Type |
Memory Size |
Small integer |
1 word.
On 32-bit architectures: -134217729 < i < 134217728
(28 bits).
On 64-bit architectures: -576460752303423489 < i <
576460752303423488 (60 bits). |
Large integer |
3..N words. |
Atom |
1 word.
An atom refers into an atom table, which also consumes memory.
The atom text is stored once for each unique atom in this table.
The atom table is not garbage-collected. |
Float |
On 32-bit architectures: 4 words.
On 64-bit architectures: 3 words. |
Binary |
3..6 words + data (can be shared). |
List |
1 word + 1 word per element + the size of each element. |
String (is the same as a list of integers) |
1 word + 2 words per character. |
Tuple |
2 words + the size of each element. |
Small Map |
4 words + 2 words per entry (key and value) + the size of each key and value pair. |
Large Map |
At least, 2 words + 2 x N words + 2 x log16(N) words +
the size of each key and value pair, where N is the number of pairs in the Map.
A large Map is represented as a tree internally where each node in the tree is a
"sparse tuple" of arity 16.
|
Pid |
1 word for a process identifier from the current local node
+ 5 words for a process identifier from another node.
A process identifier refers into a process table and a node table,
which also consumes memory. |
Port |
1 word for a port identifier from the current local node
+ 5 words for a port identifier from another node.
A port identifier refers into a port table and a node table,
which also consumes memory. |
Reference |
On 32-bit architectures: 5 words for a reference from the
current local node + 7 words for a reference from another
node.
On 64-bit architectures: 4 words for a reference from the current
local node + 6 words for a reference from another node.
A reference refers into a node table, which also consumes
memory. |
Fun |
9..13 words + the size of environment.
A fun refers into a fun table, which also consumes memory. |
Ets table |
Initially 768 words + the size of each element (6 words +
the size of Erlang data). The table grows when necessary. |
Erlang process |
338 words when spawned, including a heap of 233 words. |
Memory Size of Different Data Types