erts_alloc(3erl)
π NAME π
erts_alloc β an Erlang runtime system internal memory allocator library. Bestie, this is the erts_alloc(3erl) page from the C Library Functions corner of the manual, and itβs all about the allocator glow-up that keeps the runtimeβs memory game strong π
π DESCRIPTION π
Okay so erts_alloc is an Erlang runtime system internal memory allocator library. erts_alloc provides the Erlang runtime system with a number of memory allocators. Thatβs the whole main-character premise, and yes, itβs giving infrastructure icon π
π 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 defaultmallocimplementation 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 themmapsystem 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. 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. Itβs literally memory-block friend groups, and the vibe check is fragmentation reduction β¨
π οΈ 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. Not them being the exceptions to the rule, honestly iconic π
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) parameter are placed in singleblock carriers. - Blocks that are smaller than the value of parameter
sbctare 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.
Sizes of multiblock carriers allocated through mseg_alloc are decided based on the following parameters:
- The values of the largest multiblock carrier size (
lmbcs) - The smallest multiblock carrier size (
smbcs) - The multiblock carrier growth stages (
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) 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. Constant time coalescing? Obsessed π«Ά
The memory allocation strategy used for multiblock carriers by an allocator can be configured using parameter 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.
- 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 is because it only causes problems for other allocators. Respect the boundary, bestie π
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.
β οΈ SYSTEM FLAGS EFFECTING ERTS_ALLOC β οΈ
β οΈ 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. No cap, this is the βdo not just start typing random flagsβ zone. π
Memory allocator system flags have the following syntax:
+M<S><P> <V>
where <S> is a letter identifying a subsystem, <P> is a parameter, and <V> is the value to use. The flags can be passed to the Erlang emulator (erl(1)) as command-line arguments.
System flags effecting specific allocators have an uppercase letter as <S>. The following letters are used for the allocators:
B:binary_allocD:std_allocE:ets_allocF:fix_allocH:eheap_allocI:literal_allocL:ll_allocM:mseg_allocR:driver_allocS:sl_allocT:temp_allocY:sys_alloc
π§ Flags for Configuration of mseg_alloc π§
+MMamcbf <size>β Absolute maximum cache bad fit (in kilobytes). A segment in the memory segment cache is not reused if its size exceeds the requested size with more than the value of this parameter. Defaults to4096.+MMrmcbf <ratio>β Relative maximum cache bad fit (in percent). A segment in the memory segment cache is not reused if its size exceeds the requested size with more than relative maximum cache bad fit percent of the requested size. Defaults to20.+MMsco true|falseβ Sets the super carrier only flag. Defaults totrue. When a super carrier is used and this flag istrue,mseg_alloconly creates carriers in the super carrier. Notice that thealloc_utilframework can createsys_alloccarriers, so if you want all carriers to be created in the super carrier, you therefore want to disable use ofsys_alloccarriers by also passing+Musac false. When the flag isfalse,mseg_alloctries to create carriers outside of the super carrier when the super carrier is full.
Note: Setting this flag to false is not supported on all systems. The flag is then ignored.
+MMscrfsd <amount>β Sets super carrier reserved free segment descriptors. Defaults to65536. This parameter determines the amount of memory to reserve for free segment descriptors used by the super carrier. If the system runs out of reserved memory for free segment descriptors, other memory is used. This can however cause fragmentation issues, so you want to ensure that this never happens. The maximum amount of free segment descriptors used can be retrieved from theerts_mmaptuple part of the result from callingerlang:system_info({allocator, mseg_alloc}).+MMscrpm true|falseβ Sets the super carrier reserve physical memory flag. Defaults totrue. When this flag istrue, physical memory is reserved for the whole super carrier at once when it is created. The reservation is after that left unchanged. When this flag is set tofalse, only virtual address space is reserved for the super carrier upon creation. The system attempts to reserve physical memory upon carrier creations in the super carrier, and attempt to unreserve physical memory upon carrier destructions in the super carrier.
Note: What reservation of physical memory means, highly depends on the operating system, and how it is configured. For example, different memory overcommit settings on Linux drastically change the behavior.
Note: Setting this flag to false is possibly not supported on all systems. The flag is then ignored.
+MMscs <size in MB>β Sets super carrier size (in MB). Defaults to0, that is, the super carrier is by default disabled. The super carrier is a large continuous area in the virtual address space.mseg_allocalways tries to create new carriers in the super carrier if it exists. Notice that thealloc_utilframework can createsys_alloccarriers. For more information, see+MMsco.+MMmcs <amount>β Maximum cached segments. The maximum number of memory segments stored in the memory segment cache. Valid range is[0, 30]. Defaults to10.
πΎ Flags for Configuration of sys_alloc πΎ
+MYe trueβ Enablessys_alloc.
Note: sys_alloc cannot be disabled. Periodt. π
+MYtt <size>β Trim threshold size (in kilobytes). This is the maximum amount of free memory at the top of the heap (allocated bysbrk) that is kept bymalloc(not released to the operating system). When the amount of free memory at the top of the heap exceeds the trim threshold,mallocreleases it (by callingsbrk). Trim threshold is specified in kilobytes. Defaults to128.
Note: This flag has effect only when the emulator is linked with the GNU C library, and uses its malloc implementation.
+MYtp <size>β Top pad size (in kilobytes). This is the amount of extra memory that is allocated bymallocwhensbrkis called to get more memory from the operating system. Defaults to0.
Note: This flag has effect only when the emulator is linked with the GNU C library, and uses its malloc implementation.
π§ Flags for Configuration of Allocators Based on alloc_util π§
If u is used as subsystem identifier (that is, <S> = 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.
+M<S>acul <utilization>|deβ Abandon carrier utilization limit. A valid<utilization>is an integer in the range[0, 100]representing utilization in percent. When a utilization value> 0is used, allocator instances are allowed to abandon multiblock carriers. Ifde(default enabled) is passed instead of a<utilization>, a recommended non-zero utilization value is used. The value chosen depends on the allocator type and can be changed between ERTS versions. Defaults tode, but this can be changed in the future.
Carriers are abandoned when memory utilization in the allocator instance falls below the utilization value used. Once a carrier is abandoned, no new allocations are made in it. When an allocator instance gets an increased multiblock carrier need, it first tries to fetch an abandoned carrier from another allocator instance. If no abandoned carrier can be fetched, it creates a new empty carrier. When an abandoned carrier has been fetched, it will function as an ordinary carrier. This feature has special requirements on the allocation strategy used. Only the strategies aoff, aoffcbf, aoffcaobf, ageffcaoffm, ageffcbf and ageffcaobf support abandoned carriers.
This feature also requires multiple thread specific instances to be enabled. When enabling this feature, multiple thread-specific instances are enabled if not already enabled, and the aoffcbf strategy is enabled if the current strategy does not support abandoned carriers. This feature can be enabled on all allocators based on the alloc_util framework, except temp_alloc (which would be pointless).
+M<S>acfml <bytes>β Abandon carrier free block min limit. A valid<bytes>is a positive integer representing a block size limit. The largest free block in a carrier must be at least bytes large, for the carrier to be abandoned. The default is zero but can be changed in the future.
See also acul.
+M<S>acnl <amount>β Abandon carrier number limit. A valid<amount>is a positive integer representing max number of abandoned carriers per allocator instance. Defaults to1000which will practically disable the limit, but this can be changed in the future.
See also acul.
+M<S>acful <utilization>|deβ Abandon carrier free utilization limit. When the utilization of a carrier falls belows this limiterts_allocinstructs the OS that unused memory in the carrier can be re-used for allocation by other OS procesesses. On Unix this is done by callingmadvise(..., ..., MADV_FREE)on the unused memory region, on Windows it is done by callingVirtualAlloc(..., ..., MEM_RESET, PAGE_READWRITE). Defaults to0which means that no memory will be marked as re-usable by the OS.
A valid <utilization> is an integer in the range [0, 100] representing utilization in percent. If this value is larger than the acul limit it will be lowered to the current acul limit. If de (default enabled) is passed instead of a <utilization>, a recommended non-zero utilization value is used. The value chosen depends on the allocator type and can be changed between ERTS versions.
See also acul.
+M<S>as bf|aobf|aoff|aoffcbf|aoffcaobf|ageffcaoff|ageffcbf|ageffcaobf|gf|afβ 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.
+M<S>asbcst <size>β Absolute singleblock carrier shrink threshold (in kilobytes). When a block located in anmseg_allocsingleblock carrier is shrunk, the carrier is left unchanged if the amount of unused memory is less than this threshold, otherwise the carrier is shrunk. See alsorsbcst.+M<S>atags true|falseβ Adds a small tag to each allocated block that contains basic information about what it is and who allocated it. Use theinstrumentmodule to inspect this information.
The runtime overhead is one word 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.
+M<S>cp B|D|E|F|H||L|R|S|@|::β 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 withbinary_alloc.D: Carrier pool associated withstd_alloc.E: Carrier pool associated withets_alloc.F: Carrier pool associated withfix_alloc.H: Carrier pool associated witheheap_alloc.L: Carrier pool associated withll_alloc.R: Carrier pool associated withdriver_alloc.S: Carrier pool associated withsl_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 +Mucg :, 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.
+M<S>e true|falseβ Enables allocator<S>.+M<S>lmbcs <size>β largest multiblock carrier size formseg_alloc, in kilobytes π. Wanna know howmseg_allocmultiblock carrier sizes get decided? Peep the section The alloc_util Framework. Also bestie, on 32-bit Unix style OS this limit literally cannot be set > 64 MB, no cap.+M<S>mbcgs <ratio>βmseg_allocmultiblock carrier growth stages π±. Again, the whole "how sizes get decided" tea is in The alloc_util Framework.+M<S>mbsd <depth>β maximum block search depth, iykyk π. This flag only does anything if the good fit strategy is selected for allocator<S>. When good fit is on, free blocks get placed in segregated free-lists, and each free-list holds blocks whose sizes fall in a specific range. This depth sets the max number of blocks to inspect in a free-list while searching for a suitable block that satisfies the request.+M<S>mmbcs <size>β main multiblock carrier size π. Sets the size of the main multiblock carrier for allocator<S>. That main multiblock carrier is allocated throughsys_allocand is never deallocated. Periodt.+M<S>mmmbc <amount>β maximummseg_allocmultiblock carriers π . This is the max number of multiblock carriers allocated throughmseg_allocby allocator<S>. Once you hit this limit, new multiblock carriers get allocated throughsys_allocinstead.+M<S>mmsbc <amount>β maximummseg_allocsingleblock carriers π―. Max number of singleblock carriers allocated throughmseg_allocby allocator<S>. When this limit is reached, new singleblock carriers are allocated throughsys_alloc.+M<S>ramv <bool>β realloc always moves π. When enabled, reallocate operations are more or less translated into an allocate, copy, free sequence. This often reduces memory fragmentation, but bestie, it costs performance. Trade-offs, am I right? π+M<S>rmbcmt <ratio>β relative multiblock carrier move threshold, in percent π. When a block located in a multiblock carrier is shrunk, the block gets moved if the ratio of the size of the freed memory compared to the previous size is more than this threshold β otherwise the block is shrunk at its current location.+M<S>rsbcmt <ratio>β relative singleblock carrier move threshold, in percent πΈ. When a block located in a singleblock carrier is shrunk to a size smaller than the value of parametersbct, the block is left unchanged in the singleblock carrier if the ratio of unused memory is less than this threshold β otherwise it's moved into a multiblock carrier.+M<S>rsbcst <ratio>β relative singleblock carrier shrink threshold, in percent π¦. When a block located in anmseg_allocsingleblock carrier is shrunk, the carrier is left unchanged if the ratio of unused memory is less than this threshold β otherwise the carrier is shrunk. See alsoasbcst.+M<S>sbct <size>β singleblock carrier threshold, in kilobytes β¨. Blocks larger than this threshold get placed in singleblock carriers. Blocks smaller than this threshold get placed in multiblock carriers. On 32-bit Unix style OS this threshold cannot be set > 8 MB, just so you know.+M<S>smbcs <size>β smallestmseg_allocmultiblock carrier size, in kilobytes π§. The deets on how sizes formseg_allocmultiblock carriers are decided live in section The alloc_util Framework.+M<S>t true|falseβ multiple, thread-specific instances of the allocator π―. Default behavior isNoSchedulers+1instances. 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. That is, however, not possible anymore. We love growth π .
π Flags for Configuration of alloc_util π
All allocators based on alloc_util are effected. Yes, all of them.
+Muycs <size>βsys_alloccarrier size π. Carriers allocated throughsys_allocare allocated in sizes that are multiples of thesys_alloccarrier size. This is not true for main multiblock carriers and carriers allocated during a memory shortage, though. Exceptions exist, bestie.+Mummc <amount>β maximummseg_alloccarriers π₯. Maximum number of carriers placed in separate memory segments. When this limit is reached, new carriers are placed in memory retrieved fromsys_alloc.+Musac <bool>β allowsys_alloccarriers π«Ά. Defaults totrue. If set tofalse,sys_alloccarriers are never created by allocators using thealloc_utilframework.
π§ Special Flag for literal_alloc π§
+MIscs <size in MB>βliteral_allocsuper carrier size, in MB π. The amount of virtual address space reserved for literal terms in Erlang code on 64-bit architectures. Defaults to1024(that is, 1 GB), which is usually sufficient. The flag is ignored on 32-bit architectures. Noted.
π Instrumentation Flags π
+M<S>atagsβ adds a small tag to each allocated block that contains basic information about what it is and who allocated it π·οΈ. See+M<S>atagsfor a more complete description.
Note: when instrumentation of the emulator is enabled, the emulator uses more memory and runs slower. π It's giving "debug mode tax."
β Other Flags β
+Mea min|max|r9c|r10b|r11b|configβ 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. Legacy vibes, fr fr.configβ disables features that cannot be enabled while creating an allocator configuration witherts_alloc_config(3erl)π οΈ.
Note: this option is to be used only while running erts_alloc_config(3erl), not when using the created configuration. Don't do it, bestie.
+Mlpm all|noβ lock physical memory π. Defaults tono, that is, no physical memory is locked. If set toall, all memory mappings made by the runtime system are locked into physical memory. If set toall, 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. Highkey intense.+Mdai max|<amount>β set amount of dirty allocator instances π§Ό. Defaults to0. That is, by default no instances will be used. The maximum amount of instances equals the amount of dirty CPU schedulers on the system.
By default, each normal scheduler thread has its own allocator instance for each allocator. All other threads in the system, including dirty schedulers, share one instance for each allocator. By enabling dirty allocator instances, dirty schedulers will get and use their own set of allocator instances. Note that these instances are not exclusive to each dirty scheduler, but instead shared among dirty schedulers. The more instances used the less risk of lock contention on these allocator instances. Memory consumption do however increase with increased amount of dirty allocator instances. Balance, bestie π .
π NOTES π
Only some default values have been presented here. For information about the currently used settings and the current status of the allocators, see erlang:system_info(allocator) and erlang:system_info({allocator, Alloc}). Go look it up, iykyk.
Note: most of these flags are highly implementation-dependent and can be changed or removed without prior notice. π So don't build your whole personality around them.
erts_alloc is not obliged to strictly use the settings that have been passed to it (it can even ignore them). It's giving "suggestions, not commands."
Theerts_alloc_config(3erl)tool can be used to aid creation of anerts_allocconfiguration that is suitable for a limited number of runtime scenarios. Cute lil helper π.
π SEE ALSO π
erl(1), erlang(3erl), erts_alloc_config(3erl), instrument(3erl)
And that's the whole page, bestie! π«Ά This iconic manual page is from Ericsson AB, for erts 13.2.2.5, documenting erts_alloc(3erl). Slay, go forth and allocate responsibly πβ¨
π¦ where this page comes from
- ubuntu noble: erlang-manpages 1:25.3.2.8+dfsg-1ubuntu4 Β· Apache-2.0
- ubuntu noble: erlang-manpages 1:25.3.2.8+dfsg-1ubuntu4.8 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms