# erts_alloc
An Erlang runtime system internal memory allocator library.
## Description
`erts_alloc` is an Erlang runtime system internal memory allocator library.
`erts_alloc` provides the Erlang runtime system with a number of memory
allocators.
## Allocators
The following allocators are present:
- **`temp_alloc`** - Allocator used for temporary allocations.
- **`eheap_alloc`** - Allocator used for Erlang heap data, such as Erlang
process heaps.
- **`binary_alloc`** - Allocator used for Erlang binary data.
- **`ets_alloc`** - Allocator used for `ets` data.
- **`driver_alloc`** - Allocator used for driver data.
- **`literal_alloc`** - Allocator used for constant terms in Erlang code.
- **`sl_alloc`** - Allocator used for memory blocks that are expected to be
short-lived.
- **`ll_alloc`** - Allocator used for memory blocks that are expected to be
long-lived, for example, Erlang code.
- **`fix_alloc`** - A fast allocator used for some frequently used fixed size
data types.
- **`std_alloc`** - Allocator used for most memory blocks not allocated through
any of the other allocators described above.
- **`sys_alloc`** - This is normally the default `malloc` implementation used on
the specific OS.
- **`mseg_alloc`** - A memory segment allocator. It is used by other allocators
for allocating memory segments and is only available on systems that have the
`mmap` system call. Memory segments that are deallocated are kept for a while
in a segment cache before they are destroyed. When segments are allocated,
cached segments are used if possible instead of creating new segments. This to
reduce the number of system calls made.
`sys_alloc`, `literal_alloc` and `temp_alloc` are always enabled and cannot be
disabled. `mseg_alloc` is always enabled if it is available and an allocator
that uses it is enabled. All other allocators can be
[enabled or disabled](erts_alloc.md#M_e). By default all allocators are enabled.
When an allocator is disabled, `sys_alloc` is used instead of the disabled
allocator.
The main idea with the `erts_alloc` library is to separate memory blocks that
are used differently into different memory areas, to achieve less memory
fragmentation. By putting less effort in finding a good fit for memory blocks
that are frequently allocated than for those less frequently allocated, a
performance gain can be achieved.
[](){: #alloc_util }
## The alloc_util Framework
Internally a framework called `alloc_util` is used for implementing allocators.
`sys_alloc` and `mseg_alloc` do not use this framework, so the following does
_not_ apply to them.
An allocator manages multiple areas, called carriers, in which memory blocks are
placed. A carrier is either placed in a separate memory segment (allocated
through `mseg_alloc`), or in the heap segment (allocated through `sys_alloc`).
- Multiblock carriers are used for storage of several blocks.
- Singleblock carriers are used for storage of one block.
- Blocks that are larger than the value of the singleblock carrier threshold
([`sbct`](erts_alloc.md#M_sbct)) parameter are placed in singleblock carriers.
- Blocks that are smaller than the value of parameter `sbct` are placed in
multiblock carriers.
Normally an allocator creates a "main multiblock carrier". Main multiblock
carriers are never deallocated. The size of the main multiblock carrier is
determined by the value of parameter [`mmbcs`](erts_alloc.md#M_mmbcs).
[](){: #mseg_mbc_sizes } Sizes of multiblock carriers allocated through
`mseg_alloc` are decided based on the following parameters:
- The values of the largest multiblock carrier size
([`lmbcs`](erts_alloc.md#M_lmbcs))
- The smallest multiblock carrier size ([`smbcs`](erts_alloc.md#M_smbcs))
- The multiblock carrier growth stages ([`mbcgs`](erts_alloc.md#M_mbcgs))
If `nc` is the current number of multiblock carriers (the main multiblock
carrier excluded) managed by an allocator, the size of the next `mseg_alloc`
multiblock carrier allocated by this allocator is roughly
`smbcs+nc*(lmbcs-smbcs)/mbcgs` when `nc <= mbcgs`, and `lmbcs` when
`nc > mbcgs`. If the value of parameter `sbct` is larger than the value of
parameter `lmbcs`, the allocator may have to create multiblock carriers that are
larger than the value of parameter `lmbcs`, though. Singleblock carriers
allocated through `mseg_alloc` are sized to whole pages.
Sizes of carriers allocated through `sys_alloc` are decided based on the value
of the `sys_alloc` carrier size ([`ycs`](erts_alloc.md#Muycs)) parameter. The
size of a carrier is the least number of multiples of the value of parameter
`ycs` satisfying the request.
Coalescing of free blocks are always performed immediately. Boundary tags
(headers and footers) in free blocks are used, which makes the time complexity
for coalescing constant.
[](){: #strategy } The memory allocation strategy used for multiblock carriers
by an allocator can be configured using parameter [`as`](erts_alloc.md#M_as).
The following strategies are available:
- **Best fit** - Strategy: Find the smallest block satisfying the requested
block size.
Implementation: A balanced binary search tree is used. The time complexity is
proportional to log N, where N is the number of sizes of free blocks.
- **Address order best fit** - Strategy: Find the smallest block satisfying the
requested block size. If multiple blocks are found, choose the one with the
lowest address.
Implementation: A balanced binary search tree is used. The time complexity is
proportional to log N, where N is the number of free blocks.
- **Address order first fit** - Strategy: Find the block with the lowest address
satisfying the requested block size.
Implementation: A balanced binary search tree is used. The time complexity is
proportional to log N, where N is the number of free blocks.
- **Address order first fit carrier best fit** - Strategy: Find the _carrier_
with the lowest address that can satisfy the requested block size, then find a
block within that carrier using the "best fit" strategy.
Implementation: Balanced binary search trees are used. The time complexity is
proportional to log N, where N is the number of free blocks.
- **Address order first fit carrier address order best fit** - Strategy: Find
the _carrier_ with the lowest address that can satisfy the requested block
size, then find a block within that carrier using the "address order best fit"
strategy.
Implementation: Balanced binary search trees are used. The time complexity is
proportional to log N, where N is the number of free blocks.
- **Age order first fit carrier address order first fit** - Strategy: Find the
_oldest carrier_ that can satisfy the requested block size, then find a block
within that carrier using the "address order first fit" strategy.
Implementation: A balanced binary search tree is used. The time complexity is
proportional to log N, where N is the number of free blocks.
- **Age order first fit carrier best fit** - Strategy: Find the _oldest carrier_
that can satisfy the requested block size, then find a block within that
carrier using the "best fit" strategy.
Implementation: Balanced binary search trees are used. The time complexity is
proportional to log N, where N is the number of free blocks.
- **Age order first fit carrier address order best fit** - Strategy: Find the
_oldest carrier_ that can satisfy the requested block size, then find a block
within that carrier using the "address order best fit" strategy.
Implementation: Balanced binary search trees are used. The time complexity is
proportional to log N, where N is the number of free blocks.
- **Good fit** - Strategy: Try to find the best fit, but settle for the best fit
found during a limited search.
Implementation: The implementation uses segregated free lists with a maximum
block search depth (in each list) to find a good fit fast. When the maximum
block search depth is small (by default 3), this implementation has a time
complexity that is constant. The maximum block search depth can be configured
using parameter [`mbsd`](erts_alloc.md#M_mbsd).
- **A fit** - Strategy: Do not search for a fit, inspect only one free block to
see if it satisfies the request. This strategy is only intended to be used for
temporary allocations.
Implementation: Inspect the first block in a free-list. If it satisfies the
request, it is used, otherwise a new carrier is created. The implementation
has a time complexity that is constant.
As from ERTS 5.6.1 the emulator refuses to use this strategy on other
allocators than `temp_alloc`. This because it only causes problems for other
allocators.
Apart from the ordinary allocators described above, some pre-allocators are used
for some specific data types. These pre-allocators pre-allocate a fixed amount
of memory for certain data types when the runtime system starts. As long as
pre-allocated memory is available, it is used. When no pre-allocated memory is
available, memory is allocated in ordinary allocators. These pre-allocators are
typically much faster than the ordinary allocators, but can only satisfy a
limited number of requests.
[](){: #flags }
## System Flags Effecting erts_alloc
> #### Warning {: .warning }
>
> Only use these flags if you are sure what you are doing. Unsuitable settings
> can cause serious performance degradation and even a system crash at any time
> during operation.
Memory allocator system flags have the following syntax: `+M
` is a parameter, and `` is a letter identifying a subsystem, ``.
The following letters are used for the allocators:
- `B: binary_alloc`
- `D: std_alloc`
- `E: ets_alloc`
- `F: fix_alloc`
- `H: eheap_alloc`
- `I: literal_alloc`
- `L: ll_alloc`
- `M: mseg_alloc`
- `R: driver_alloc`
- `S: sl_alloc`
- `T: temp_alloc`
- `Y: sys_alloc`
### Flags for Configuration of mseg_alloc
- **`+MMamcbf = u`), all allocators
based on `alloc_util` are effected. If `B`, `D`, `E`, `F`, `H`, `I`, `L`, `R`,
`S`, `T`, `X` is used as subsystem identifier, only the specific allocator
identifier is effected.
- **`+Macul acfml acnl acful as bf|aobf|aoff|aoffcbf|aoffcaobf|ageffcaoff|ageffcbf|ageffcaobf|gf|af`{:
#M_as }** - Allocation strategy. The following strategies are valid:
- `bf` (best fit)
- `aobf` (address order best fit)
- `aoff` (address order first fit)
- `aoffcbf` (address order first fit carrier best fit)
- `aoffcaobf` (address order first fit carrier address order best fit)
- `ageffcaoff` (age order first fit carrier address order first fit)
- `ageffcbf` (age order first fit carrier best fit)
- `ageffcaobf` (age order first fit carrier address order best fit)
- `gf` (good fit)
- `af` (a fit)
See the description of allocation strategies in section
[The alloc_util Framework](erts_alloc.md#strategy).
- **`+Masbcst atags true|false`{: #M_atags }** - Adds a small tag to each allocated
block that contains basic information about what it is and who allocated it.
Use the `m:instrument` module to inspect this information.
The runtime overhead is two words per allocation when enabled. This may change
at any time in the future.
The default is `true` for `binary_alloc` and `driver_alloc`, and `false` for
the other allocator types.
- **`+Mcp B|D|E|F|H||L|R|S|@|:`{: #M_cp }** - Set carrier pool to use for the
allocator. Memory carriers will only migrate between allocator instances that
use the same carrier pool. The following carrier pool names exist:
- **`B`** - Carrier pool associated with `binary_alloc`.
- **`D`** - Carrier pool associated with `std_alloc`.
- **`E`** - Carrier pool associated with `ets_alloc`.
- **`F`** - Carrier pool associated with `fix_alloc`.
- **`H`** - Carrier pool associated with `eheap_alloc`.
- **`L`** - Carrier pool associated with `ll_alloc`.
- **`R`** - Carrier pool associated with `driver_alloc`.
- **`S`** - Carrier pool associated with `sl_alloc`.
- **`@`** - Carrier pool associated with the system as a whole.
Besides passing carrier pool name as value to the parameter, you can also pass
`:`. By passing `:` instead of carrier pool name, the allocator will use the
carrier pool associated with itself. By passing the command line argument
"`+Mucp :`", all allocators that have an associated carrier pool will use the
carrier pool associated with themselves.
The association between carrier pool and allocator is very loose. The
associations are more or less only there to get names for the amount of
carrier pools needed and names of carrier pools that can be easily identified
by the `:` value.
This flag is only valid for allocators that have an associated carrier pool.
Besides that, there are no restrictions on carrier pools to use for an
allocator.
Currently each allocator with an associated carrier pool defaults to using its
own associated carrier pool.
- **`+Me true|false`{: #M_e }** - Enables allocator ``.
- **`+Mlmbcs mbcgs mbsd `. When
the good fit strategy is used, free blocks are placed in segregated
free-lists. Each free-list contains blocks of sizes in a specific range. The
maximum block search depth sets a limit on the maximum number of blocks to
inspect in a free-list during a search for suitable block satisfying the
request.
- **`+Mmmbcs `. The main multiblock
carrier is allocated through `sys_alloc` and is never deallocated.
- **`+Mmmmbc `. When this limit is reached, new multiblock carriers are
allocated through `sys_alloc`.
- **`+Mmmsbc `. When this limit is reached, new singleblock
carriers are allocated through `sys_alloc`.
- **`+Mramv rmbcmt rsbcmt rsbcst sbct smbcs t true|false`{: #M_t }** - Multiple, thread-specific instances of the
allocator. Default behavior is `NoSchedulers+1` instances. Each scheduler uses
a lock-free instance of its own and other threads use a common instance.
Before ERTS 5.9 it was possible to configure a smaller number of
thread-specific instances than schedulers. This is, however, not possible
anymore.
### Flags for Configuration of alloc_util
All allocators based on `alloc_util` are effected.
- **`+Muycs acful`](erts_alloc.md#M_acful) is set. Default is `false`
which means `MADV_FREE` is passed to `madvise()`. If set to `true`,
`MADV_DONTNEED` is passed instead.
### Special Flag for literal_alloc
- **`+MIscs atags`** - Adds a small tag to each allocated block that contains
basic information about what it is and who allocated it. See
[`+Matags`](erts_alloc.md#M_atags) for a more complete description.
> #### Note {: .info }
>
> When instrumentation of the emulator is enabled, the emulator uses more memory
> and runs slower.
### Other Flags
- **`+Mea min|max|r9c|r10b|r11b|config`{: #Mea }** - Options:
- **`min`** - Disables all allocators that can be disabled.
- **`max`** - Enables all allocators (default).
- **`r9c|r10b|r11b`** - Configures all allocators as they were configured in
respective Erlang/OTP release. These will eventually be removed.
- **`+Mlpm all|no`{: #Mlpm }** - Lock physical memory. Defaults to `no`, that
is, no physical memory is locked. If set to `all`, all memory mappings made by
the runtime system are locked into physical memory. If set to `all`, the
runtime system fails to start if this feature is not supported, the user has
not got enough privileges, or the user is not allowed to lock enough physical
memory. The runtime system also fails with an out of memory condition if the
user limit on the amount of locked memory is reached.
- **`+Mdai max|