1erts_alloc(3) C Library Functions erts_alloc(3)
2
3
4
6 erts_alloc - An Erlang runtime system internal memory allocator
7 library.
8
9
11 erts_alloc is an Erlang runtime system internal memory allocator
12 library. erts_alloc provides the Erlang runtime system with a number of
13 memory allocators.
14
16 The following allocators are present:
17
18 temp_alloc:
19 Allocator used for temporary allocations.
20
21 eheap_alloc:
22 Allocator used for Erlang heap data, such as Erlang process heaps.
23
24 binary_alloc:
25 Allocator used for Erlang binary data.
26
27 ets_alloc:
28 Allocator used for ets data.
29
30 driver_alloc:
31 Allocator used for driver data.
32
33 literal_alloc:
34 Allocator used for constant terms in Erlang code.
35
36 sl_alloc:
37 Allocator used for memory blocks that are expected to be short-
38 lived.
39
40 ll_alloc:
41 Allocator used for memory blocks that are expected to be long-
42 lived, for example, Erlang code.
43
44 fix_alloc:
45 A fast allocator used for some frequently used fixed size data
46 types.
47
48 exec_alloc:
49 Allocator used by the HiPE application for native executable code.
50
51 std_alloc:
52 Allocator used for most memory blocks not allocated through any of
53 the other allocators described above.
54
55 sys_alloc:
56 This is normally the default malloc implementation used on the spe‐
57 cific OS.
58
59 mseg_alloc:
60 A memory segment allocator. It is used by other allocators for
61 allocating memory segments and is only available on systems that
62 have the mmap system call. Memory segments that are deallocated are
63 kept for a while in a segment cache before they are destroyed. When
64 segments are allocated, cached segments are used if possible
65 instead of creating new segments. This to reduce the number of sys‐
66 tem calls made.
67
68 sys_alloc, literal_alloc and temp_alloc are always enabled and cannot
69 be disabled. exec_alloc is only available if it is needed and cannot be
70 disabled. mseg_alloc is always enabled if it is available and an allo‐
71 cator that uses it is enabled. All other allocators can be enabled or
72 disabled. By default all allocators are enabled. When an allocator is
73 disabled, sys_alloc is used instead of the disabled allocator.
74
75 The main idea with the erts_alloc library is to separate memory blocks
76 that are used differently into different memory areas, to achieve less
77 memory fragmentation. By putting less effort in finding a good fit for
78 memory blocks that are frequently allocated than for those less fre‐
79 quently allocated, a performance gain can be achieved.
80
82 Internally a framework called alloc_util is used for implementing allo‐
83 cators. sys_alloc and mseg_alloc do not use this framework, so the fol‐
84 lowing does not apply to them.
85
86 An allocator manages multiple areas, called carriers, in which memory
87 blocks are placed. A carrier is either placed in a separate memory seg‐
88 ment (allocated through mseg_alloc), or in the heap segment (allocated
89 through sys_alloc).
90
91 * Multiblock carriers are used for storage of several blocks.
92
93 * Singleblock carriers are used for storage of one block.
94
95 * Blocks that are larger than the value of the singleblock carrier
96 threshold (sbct) parameter are placed in singleblock carriers.
97
98 * Blocks that are smaller than the value of parameter sbct are placed
99 in multiblock carriers.
100
101 Normally an allocator creates a "main multiblock carrier". Main multi‐
102 block carriers are never deallocated. The size of the main multiblock
103 carrier is determined by the value of parameter mmbcs.
104
105 Sizes of multiblock carriers allocated through mseg_alloc are decided
106 based on the following parameters:
107
108 * The values of the largest multiblock carrier size (lmbcs)
109
110 * The smallest multiblock carrier size (smbcs)
111
112 * The multiblock carrier growth stages (mbcgs)
113
114 If nc is the current number of multiblock carriers (the main multiblock
115 carrier excluded) managed by an allocator, the size of the next
116 mseg_alloc multiblock carrier allocated by this allocator is roughly
117 smbcs+nc*(lmbcs-smbcs)/mbcgs when nc <= mbcgs, and lmbcs when nc >
118 mbcgs. If the value of parameter sbct is larger than the value of
119 parameter lmbcs, the allocator may have to create multiblock carriers
120 that are larger than the value of parameter lmbcs, though. Singleblock
121 carriers allocated through mseg_alloc are sized to whole pages.
122
123 Sizes of carriers allocated through sys_alloc are decided based on the
124 value of the sys_alloc carrier size (ycs) parameter. The size of a car‐
125 rier is the least number of multiples of the value of parameter ycs
126 satisfying the request.
127
128 Coalescing of free blocks are always performed immediately. Boundary
129 tags (headers and footers) in free blocks are used, which makes the
130 time complexity for coalescing constant.
131
132 The memory allocation strategy used for multiblock carriers by an allo‐
133 cator can be configured using parameter as. The following strategies
134 are available:
135
136 Best fit:
137 Strategy: Find the smallest block satisfying the requested block
138 size.
139
140 Implementation: A balanced binary search tree is used. The time
141 complexity is proportional to log N, where N is the number of sizes
142 of free blocks.
143
144 Address order best fit:
145 Strategy: Find the smallest block satisfying the requested block
146 size. If multiple blocks are found, choose the one with the lowest
147 address.
148
149 Implementation: A balanced binary search tree is used. The time
150 complexity is proportional to log N, where N is the number of free
151 blocks.
152
153 Address order first fit:
154 Strategy: Find the block with the lowest address satisfying the
155 requested block size.
156
157 Implementation: A balanced binary search tree is used. The time
158 complexity is proportional to log N, where N is the number of free
159 blocks.
160
161 Address order first fit carrier best fit:
162 Strategy: Find the carrier with the lowest address that can satisfy
163 the requested block size, then find a block within that carrier
164 using the "best fit" strategy.
165
166 Implementation: Balanced binary search trees are used. The time
167 complexity is proportional to log N, where N is the number of free
168 blocks.
169
170 Address order first fit carrier address order best fit:
171 Strategy: Find the carrier with the lowest address that can satisfy
172 the requested block size, then find a block within that carrier
173 using the "address order best fit" strategy.
174
175 Implementation: Balanced binary search trees are used. The time
176 complexity is proportional to log N, where N is the number of free
177 blocks.
178
179 Age order first fit carrier address order first fit:
180 Strategy: Find the oldest carrier that can satisfy the requested
181 block size, then find a block within that carrier using the
182 "address order first fit" strategy.
183
184 Implementation: A balanced binary search tree is used. The time
185 complexity is proportional to log N, where N is the number of free
186 blocks.
187
188 Age order first fit carrier best fit:
189 Strategy: Find the oldest carrier that can satisfy the requested
190 block size, then find a block within that carrier using the "best
191 fit" strategy.
192
193 Implementation: Balanced binary search trees are used. The time
194 complexity is proportional to log N, where N is the number of free
195 blocks.
196
197 Age order first fit carrier address order best fit:
198 Strategy: Find the oldest carrier that can satisfy the requested
199 block size, then find a block within that carrier using the
200 "address order best fit" strategy.
201
202 Implementation: Balanced binary search trees are used. The time
203 complexity is proportional to log N, where N is the number of free
204 blocks.
205
206 Good fit:
207 Strategy: Try to find the best fit, but settle for the best fit
208 found during a limited search.
209
210 Implementation: The implementation uses segregated free lists with
211 a maximum block search depth (in each list) to find a good fit
212 fast. When the maximum block search depth is small (by default 3),
213 this implementation has a time complexity that is constant. The
214 maximum block search depth can be configured using parameter mbsd.
215
216 A fit:
217 Strategy: Do not search for a fit, inspect only one free block to
218 see if it satisfies the request. This strategy is only intended to
219 be used for temporary allocations.
220
221 Implementation: Inspect the first block in a free-list. If it sat‐
222 isfies the request, it is used, otherwise a new carrier is created.
223 The implementation has a time complexity that is constant.
224
225 As from ERTS 5.6.1 the emulator refuses to use this strategy on
226 other allocators than temp_alloc. This because it only causes prob‐
227 lems for other allocators.
228
229 Apart from the ordinary allocators described above, some pre-allocators
230 are used for some specific data types. These pre-allocators pre-allo‐
231 cate a fixed amount of memory for certain data types when the runtime
232 system starts. As long as pre-allocated memory is available, it is
233 used. When no pre-allocated memory is available, memory is allocated in
234 ordinary allocators. These pre-allocators are typically much faster
235 than the ordinary allocators, but can only satisfy a limited number of
236 requests.
237
239 Warning:
240 Only use these flags if you are sure what you are doing. Unsuitable
241 settings can cause serious performance degradation and even a system
242 crash at any time during operation.
243
244
245 Memory allocator system flags have the following syntax: +M<S><P> <V>,
246 where <S> is a letter identifying a subsystem, <P> is a parameter, and
247 <V> is the value to use. The flags can be passed to the Erlang emulator
248 (erl(1)) as command-line arguments.
249
250 System flags effecting specific allocators have an uppercase letter as
251 <S>. The following letters are used for the allocators:
252
253 * B: binary_alloc
254
255 * D: std_alloc
256
257 * E: ets_alloc
258
259 * F: fix_alloc
260
261 * H: eheap_alloc
262
263 * I: literal_alloc
264
265 * L: ll_alloc
266
267 * M: mseg_alloc
268
269 * R: driver_alloc
270
271 * S: sl_alloc
272
273 * T: temp_alloc
274
275 * X: exec_alloc
276
277 * Y: sys_alloc
278
279 Flags for Configuration of mseg_alloc
280 +MMamcbf <size>:
281 Absolute maximum cache bad fit (in kilobytes). A segment in the
282 memory segment cache is not reused if its size exceeds the
283 requested size with more than the value of this parameter. Defaults
284 to 4096.
285
286 +MMrmcbf <ratio>:
287 Relative maximum cache bad fit (in percent). A segment in the mem‐
288 ory segment cache is not reused if its size exceeds the requested
289 size with more than relative maximum cache bad fit percent of the
290 requested size. Defaults to 20.
291
292 +MMsco true|false:
293 Sets super carrier only flag. Defaults to true. When a super car‐
294 rier is used and this flag is true, mseg_alloc only creates carri‐
295 ers in the super carrier. Notice that the alloc_util framework can
296 create sys_alloc carriers, so if you want all carriers to be cre‐
297 ated in the super carrier, you therefore want to disable use of
298 sys_alloc carriers by also passing +Musac false. When the flag is
299 false, mseg_alloc tries to create carriers outside of the super
300 carrier when the super carrier is full.
301
302 Note:
303 Setting this flag to false is not supported on all systems. The flag
304 is then ignored.
305
306
307 +MMscrfsd <amount>:
308 Sets super carrier reserved free segment descriptors. Defaults to
309 65536. This parameter determines the amount of memory to reserve
310 for free segment descriptors used by the super carrier. If the sys‐
311 tem runs out of reserved memory for free segment descriptors, other
312 memory is used. This can however cause fragmentation issues, so you
313 want to ensure that this never happens. The maximum amount of free
314 segment descriptors used can be retrieved from the erts_mmap tuple
315 part of the result from calling erlang:system_info({allocator,
316 mseg_alloc}).
317
318 +MMscrpm true|false:
319 Sets super carrier reserve physical memory flag. Defaults to true.
320 When this flag is true, physical memory is reserved for the whole
321 super carrier at once when it is created. The reservation is after
322 that left unchanged. When this flag is set to false, only virtual
323 address space is reserved for the super carrier upon creation. The
324 system attempts to reserve physical memory upon carrier creations
325 in the super carrier, and attempt to unreserve physical memory upon
326 carrier destructions in the super carrier.
327
328 Note:
329 What reservation of physical memory means, highly depends on the
330 operating system, and how it is configured. For example, different
331 memory overcommit settings on Linux drastically change the behavior.
332
333 Setting this flag to false is possibly not supported on all systems.
334 The flag is then ignored.
335
336
337 +MMscs <size in MB>:
338 Sets super carrier size (in MB). Defaults to 0, that is, the super
339 carrier is by default disabled. The super carrier is a large con‐
340 tinuous area in the virtual address space. mseg_alloc always tries
341 to create new carriers in the super carrier if it exists. Notice
342 that the alloc_util framework can create sys_alloc carriers. For
343 more information, see +MMsco.
344
345 +MMmcs <amount>:
346 Maximum cached segments. The maximum number of memory segments
347 stored in the memory segment cache. Valid range is [0, 30].
348 Defaults to 10.
349
350 Flags for Configuration of sys_alloc
351 +MYe true:
352 Enables sys_alloc.
353
354 Note:
355 sys_alloc cannot be disabled.
356
357
358 +MYm libc:
359 malloc library to use. Only libc is available. libc enables the
360 standard libc malloc implementation. By default libc is used.
361
362 +MYtt <size>:
363 Trim threshold size (in kilobytes). This is the maximum amount of
364 free memory at the top of the heap (allocated by sbrk) that is kept
365 by malloc (not released to the operating system). When the amount
366 of free memory at the top of the heap exceeds the trim threshold,
367 malloc releases it (by calling sbrk). Trim threshold is specified
368 in kilobytes. Defaults to 128.
369
370 Note:
371 This flag has effect only when the emulator is linked with the GNU C
372 library, and uses its malloc implementation.
373
374
375 +MYtp <size>:
376 Top pad size (in kilobytes). This is the amount of extra memory
377 that is allocated by malloc when sbrk is called to get more memory
378 from the operating system. Defaults to 0.
379
380 Note:
381 This flag has effect only when the emulator is linked with the GNU C
382 library, and uses its malloc implementation.
383
384
385 Flags for Configuration of Allocators Based on alloc_util
386 If u is used as subsystem identifier (that is, <S> = u), all allocators
387 based on alloc_util are effected. If B, D, E, F, H, L, R, S, or T is
388 used as subsystem identifier, only the specific allocator identifier is
389 effected.
390
391 +M<S>acul <utilization>|de:
392 Abandon carrier utilization limit. A valid <utilization> is an
393 integer in the range [0, 100] representing utilization in percent.
394 When a utilization value > 0 is used, allocator instances are
395 allowed to abandon multiblock carriers. If de (default enabled) is
396 passed instead of a <utilization>, a recommended non-zero utiliza‐
397 tion value is used. The value chosen depends on the allocator type
398 and can be changed between ERTS versions. Defaults to de, but this
399 can be changed in the future.
400
401 Carriers are abandoned when memory utilization in the allocator
402 instance falls below the utilization value used. Once a carrier is
403 abandoned, no new allocations are made in it. When an allocator
404 instance gets an increased multiblock carrier need, it first tries
405 to fetch an abandoned carrier from another allocator instance. If
406 no abandoned carrier can be fetched, it creates a new empty car‐
407 rier. When an abandoned carrier has been fetched, it will function
408 as an ordinary carrier. This feature has special requirements on
409 the allocation strategy used. Only the strategies aoff, aoffcbf,
410 aoffcaobf, ageffcaoffm, ageffcbf and ageffcaobf support abandoned
411 carriers.
412
413 This feature also requires multiple thread specific instances to be
414 enabled. When enabling this feature, multiple thread-specific
415 instances are enabled if not already enabled, and the aoffcbf
416 strategy is enabled if the current strategy does not support aban‐
417 doned carriers. This feature can be enabled on all allocators based
418 on the alloc_util framework, except temp_alloc (which would be
419 pointless).
420
421 +M<S>acfml <bytes>:
422 Abandon carrier free block min limit. A valid <bytes> is a positive
423 integer representing a block size limit. The largest free block in
424 a carrier must be at least bytes large, for the carrier to be aban‐
425 doned. The default is zero but can be changed in the future.
426
427 See also acul.
428
429 +M<S>acnl <amount>:
430 Abandon carrier number limit. A valid <amount> is a positive inte‐
431 ger representing max number of abandoned carriers per allocator
432 instance. Defaults to 1000 which will practically disable the
433 limit, but this can be changed in the future.
434
435 See also acul.
436
437 +M<S>as bf|aobf|aoff|aoffcbf|aoffcaobf|ageffcaoff|ageffcbf|ageff‐
438 caobf|gf|af:
439 Allocation strategy. The following strategies are valid:
440
441 * bf (best fit)
442
443 * aobf (address order best fit)
444
445 * aoff (address order first fit)
446
447 * aoffcbf (address order first fit carrier best fit)
448
449 * aoffcaobf (address order first fit carrier address order best
450 fit)
451
452 * ageffcaoff (age order first fit carrier address order first fit)
453
454 * ageffcbf (age order first fit carrier best fit)
455
456 * ageffcaobf (age order first fit carrier address order best fit)
457
458 * gf (good fit)
459
460 * af (a fit)
461
462 See the description of allocation strategies in section The
463 alloc_util Framework.
464
465 +M<S>asbcst <size>:
466 Absolute singleblock carrier shrink threshold (in kilobytes). When
467 a block located in an mseg_alloc singleblock carrier is shrunk, the
468 carrier is left unchanged if the amount of unused memory is less
469 than this threshold, otherwise the carrier is shrunk. See also
470 rsbcst.
471
472 +M<S>atags true|false:
473 Adds a small tag to each allocated block that contains basic infor‐
474 mation about what it is and who allocated it. Use the instrument
475 module to inspect this information.
476
477 The runtime overhead is one word per allocation when enabled. This
478 may change at any time in the future.
479
480 The default is true for binary_alloc and driver_alloc, and false
481 for the other allocator types.
482
483 +M<S>e true|false:
484 Enables allocator <S>.
485
486 +M<S>lmbcs <size>:
487 Largest (mseg_alloc) multiblock carrier size (in kilobytes). See
488 the description on how sizes for mseg_alloc multiblock carriers are
489 decided in section The alloc_util Framework. On 32-bit Unix style
490 OS this limit cannot be set > 64 MB.
491
492 +M<S>mbcgs <ratio>:
493 (mseg_alloc) multiblock carrier growth stages. See the description
494 on how sizes for mseg_alloc multiblock carriers are decided in sec‐
495 tion The alloc_util Framework.
496
497 +M<S>mbsd <depth>:
498 Maximum block search depth. This flag has effect only if the good
499 fit strategy is selected for allocator <S>. When the good fit
500 strategy is used, free blocks are placed in segregated free-lists.
501 Each free-list contains blocks of sizes in a specific range. The
502 maxiumum block search depth sets a limit on the maximum number of
503 blocks to inspect in a free-list during a search for suitable block
504 satisfying the request.
505
506 +M<S>mmbcs <size>:
507 Main multiblock carrier size. Sets the size of the main multiblock
508 carrier for allocator <S>. The main multiblock carrier is allocated
509 through sys_alloc and is never deallocated.
510
511 +M<S>mmmbc <amount>:
512 Maximum mseg_alloc multiblock carriers. Maximum number of multi‐
513 block carriers allocated through mseg_alloc by allocator <S>. When
514 this limit is reached, new multiblock carriers are allocated
515 through sys_alloc.
516
517 +M<S>mmsbc <amount>:
518 Maximum mseg_alloc singleblock carriers. Maximum number of single‐
519 block carriers allocated through mseg_alloc by allocator <S>. When
520 this limit is reached, new singleblock carriers are allocated
521 through sys_alloc.
522
523 +M<S>ramv <bool>:
524 Realloc always moves. When enabled, reallocate operations are more
525 or less translated into an allocate, copy, free sequence. This
526 often reduces memory fragmentation, but costs performance.
527
528 +M<S>rmbcmt <ratio>:
529 Relative multiblock carrier move threshold (in percent). When a
530 block located in a multiblock carrier is shrunk, the block is moved
531 if the ratio of the size of the returned memory compared to the
532 previous size is more than this threshold, otherwise the block is
533 shrunk at the current location.
534
535 +M<S>rsbcmt <ratio>:
536 Relative singleblock carrier move threshold (in percent). When a
537 block located in a singleblock carrier is shrunk to a size smaller
538 than the value of parameter sbct, the block is left unchanged in
539 the singleblock carrier if the ratio of unused memory is less than
540 this threshold, otherwise it is moved into a multiblock carrier.
541
542 +M<S>rsbcst <ratio>:
543 Relative singleblock carrier shrink threshold (in percent). When a
544 block located in an mseg_alloc singleblock carrier is shrunk, the
545 carrier is left unchanged if the ratio of unused memory is less
546 than this threshold, otherwise the carrier is shrunk. See also
547 asbcst.
548
549 +M<S>sbct <size>:
550 Singleblock carrier threshold (in kilobytes). Blocks larger than
551 this threshold are placed in singleblock carriers. Blocks smaller
552 than this threshold are placed in multiblock carriers. On 32-bit
553 Unix style OS this threshold cannot be set > 8 MB.
554
555 +M<S>smbcs <size>:
556 Smallest (mseg_alloc) multiblock carrier size (in kilobytes). See
557 the description on how sizes for mseg_alloc multiblock carriers are
558 decided in section The alloc_util Framework.
559
560 +M<S>t true|false:
561 Multiple, thread-specific instances of the allocator. This option
562 has only effect on the runtime system with SMP support. Default
563 behavior on the runtime system with SMP support is NoSchedulers+1
564 instances. Each scheduler uses a lock-free instance of its own and
565 other threads use a common instance.
566
567 Before ERTS 5.9 it was possible to configure a smaller number of
568 thread-specific instances than schedulers. This is, however, not
569 possible anymore.
570
571 Flags for Configuration of alloc_util
572 All allocators based on alloc_util are effected.
573
574 +Muycs <size>:
575 sys_alloc carrier size. Carriers allocated through sys_alloc are
576 allocated in sizes that are multiples of the sys_alloc carrier
577 size. This is not true for main multiblock carriers and carriers
578 allocated during a memory shortage, though.
579
580 +Mummc <amount>:
581 Maximum mseg_alloc carriers. Maximum number of carriers placed in
582 separate memory segments. When this limit is reached, new carriers
583 are placed in memory retrieved from sys_alloc.
584
585 +Musac <bool>:
586 Allow sys_alloc carriers. Defaults to true. If set to false,
587 sys_alloc carriers are never created by allocators using the
588 alloc_util framework.
589
590 Special Flag for literal_alloc
591 +MIscs <size in MB>:
592 literal_alloc super carrier size (in MB). The amount of virtual
593 address space reserved for literal terms in Erlang code on 64-bit
594 architectures. Defaults to 1024 (that is, 1 GB), which is usually
595 sufficient. The flag is ignored on 32-bit architectures.
596
597 Instrumentation Flags
598 +M<S>atags:
599 Adds a small tag to each allocated block that contains basic infor‐
600 mation about what it is and who allocated it. See +M<S>atags for a
601 more complete description.
602
603 +Mit X:
604 Reserved for future use. Do not use this flag.
605
606 Note:
607 When instrumentation of the emulator is enabled, the emulator uses more
608 memory and runs slower.
609
610
611 Other Flags
612 +Mea min|max|r9c|r10b|r11b|config:
613 Options:
614
615 min:
616 Disables all allocators that can be disabled.
617
618 max:
619 Enables all allocators (default).
620
621 r9c|r10b|r11b:
622 Configures all allocators as they were configured in respective
623 Erlang/OTP release. These will eventually be removed.
624
625 config:
626 Disables features that cannot be enabled while creating an allo‐
627 cator configuration with erts_alloc_config(3).
628
629 Note:
630 This option is to be used only while running erts_alloc_config(3),
631 not when using the created configuration.
632
633
634 +Mlpm all|no:
635 Lock physical memory. Defaults to no, that is, no physical memory
636 is locked. If set to all, all memory mappings made by the runtime
637 system are locked into physical memory. If set to all, the runtime
638 system fails to start if this feature is not supported, the user
639 has not got enough privileges, or the user is not allowed to lock
640 enough physical memory. The runtime system also fails with an out
641 of memory condition if the user limit on the amount of locked mem‐
642 ory is reached.
643
645 Only some default values have been presented here. For information
646 about the currently used settings and the current status of the alloca‐
647 tors, see erlang:system_info(allocator) and erlang:system_info({alloca‐
648 tor, Alloc}).
649
650 Note:
651 Most of these flags are highly implementation-dependent and can be
652 changed or removed without prior notice.
653
654 erts_alloc is not obliged to strictly use the settings that have been
655 passed to it (it can even ignore them).
656
657
658 The erts_alloc_config(3) tool can be used to aid creation of an
659 erts_alloc configuration that is suitable for a limited number of run‐
660 time scenarios.
661
663 erl(1), erlang(3), erts_alloc_config(3), instrument(3)
664
665
666
667Ericsson AB erts 10.5.6 erts_alloc(3)