1.\" $NetBSD: jemalloc.3,v 1.12 2022/12/04 01:29:32 uwe Exp $ 2.\" 3.\" Copyright (c) 1980, 1991, 1993 4.\" The Regents of the University of California. All rights reserved. 5.\" 6.\" This code is derived from software contributed to Berkeley by 7.\" the American National Standards Committee X3, on Information 8.\" Processing Systems. 9.\" 10.\" Redistribution and use in source and binary forms, with or without 11.\" modification, are permitted provided that the following conditions 12.\" are met: 13.\" 1. Redistributions of source code must retain the above copyright 14.\" notice, this list of conditions and the following disclaimer. 15.\" 2. Redistributions in binary form must reproduce the above copyright 16.\" notice, this list of conditions and the following disclaimer in the 17.\" documentation and/or other materials provided with the distribution. 18.\" 3. Neither the name of the University nor the names of its contributors 19.\" may be used to endorse or promote products derived from this software 20.\" without specific prior written permission. 21.\" 22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32.\" SUCH DAMAGE. 33.\" 34.\" @(#)malloc.3 8.1 (Berkeley) 6/4/93 35.\" $FreeBSD: src/lib/libc/stdlib/malloc.3,v 1.73 2007/06/15 22:32:33 jasone Exp $ 36.\" 37.Dd June 21, 2011 38.Dt JEMALLOC 3 39.Os 40.Sh NAME 41.Nm jemalloc , 42.Nm malloc.conf 43.Nd the default system allocator 44.Sh LIBRARY 45.Lb libc 46.Sh SYNOPSIS 47.Ft const char * 48.Va _malloc_options ; 49.Sh DESCRIPTION 50The 51.Nm 52is a general-purpose concurrent 53.Xr malloc 3 54implementation specifically designed to be scalable 55on modern multi-processor systems. 56It is the default user space system allocator in 57.Nx . 58.Pp 59When the first call is made to one of the memory allocation 60routines such as 61.Fn malloc 62or 63.Fn realloc , 64various flags that affect the workings of the allocator are set or reset. 65These are described below. 66.Pp 67The 68.Dq name 69of the file referenced by the symbolic link named 70.Pa /etc/malloc.conf , 71the value of the environment variable 72.Ev MALLOC_OPTIONS , 73and the string pointed to by the global variable 74.Va _malloc_options 75will be interpreted, in that order, character by character as flags. 76.Pp 77Most flags are single letters. 78Uppercase letters indicate that the behavior is set, or on, 79and lowercase letters mean that the behavior is not set, or off. 80The following options are available. 81.Bl -tag -width "A " -offset 3n 82.It Em A 83All warnings (except for the warning about unknown 84flags being set) become fatal. 85The process will call 86.Xr abort 3 87in these cases. 88.It Em H 89Use 90.Xr madvise 2 91when pages within a chunk are no longer in use, but the chunk as a whole cannot 92yet be deallocated. 93This is primarily of use when swapping is a real possibility, due to the high 94overhead of the 95.Fn madvise 96system call. 97.It Em J 98Each byte of new memory allocated by 99.Fn malloc , 100.Fn realloc 101will be initialized to 0xa5. 102All memory returned by 103.Fn free , 104.Fn realloc 105will be initialized to 0x5a. 106This is intended for debugging and will impact performance negatively. 107.It Em K 108Increase/decrease the virtual memory chunk size by a factor of two. 109The default chunk size is 1 MB. 110This option can be specified multiple times. 111.It Em N 112Increase/decrease the number of arenas by a factor of two. 113The default number of arenas is four times the number of CPUs, or one if there 114is a single CPU. 115This option can be specified multiple times. 116.It Em P 117Various statistics are printed at program exit via an 118.Xr atexit 3 119function. 120This has the potential to cause deadlock for a multi-threaded process that exits 121while one or more threads are executing in the memory allocation functions. 122Therefore, this option should only be used with care; it is primarily intended 123as a performance tuning aid during application development. 124.It Em Q 125Increase/decrease the size of the allocation quantum by a factor of two. 126The default quantum is the minimum allowed by the architecture (typically 8 or 12716 bytes). 128This option can be specified multiple times. 129.It Em S 130Increase/decrease the size of the maximum size class that is a multiple of the 131quantum by a factor of two. 132Above this size, power-of-two spacing is used for size classes. 133The default value is 512 bytes. 134This option can be specified multiple times. 135.It Em U 136Generate 137.Dq utrace 138entries for 139.Xr ktrace 1 , 140for all operations. 141Consult the source for details on this option. 142.It Em V 143Attempting to allocate zero bytes will return a 144.Dv NULL 145pointer instead of a valid pointer. 146(The default behavior is to make a minimal allocation and return a 147pointer to it.) 148This option is provided for System V compatibility. 149This option is incompatible with the 150.Em X 151option. 152.It Em X 153Rather than return failure for any allocation function, 154display a diagnostic message on 155.Dv stderr 156and cause the program to drop 157core (using 158.Xr abort 3 ) . 159This option should be set at compile time by including the following in 160the source code: 161.Bd -literal -offset indent 162_malloc_options = "X"; 163.Ed 164.It Em Z 165Each byte of new memory allocated by 166.Fn malloc , 167.Fn realloc 168will be initialized to 0. 169Note that this initialization only happens once for each byte, so 170.Fn realloc 171does not zero memory that was previously allocated. 172This is intended for debugging and will impact performance negatively. 173.El 174.Pp 175Extra care should be taken when enabling 176any of the options in production environments. 177The 178.Em A , 179.Em J , 180and 181.Em Z 182options are intended for testing and debugging. 183An application which changes its behavior when these options are used 184is flawed. 185.Sh IMPLEMENTATION NOTES 186The 187.Nm 188allocator uses multiple arenas in order to reduce lock 189contention for threaded programs on multi-processor systems. 190This works well with regard to threading scalability, but incurs some costs. 191There is a small fixed per-arena overhead, and additionally, arenas manage 192memory completely independently of each other, which means a small fixed 193increase in overall memory fragmentation. 194These overheads are not generally an issue, 195given the number of arenas normally used. 196Note that using substantially more arenas than the default is not likely to 197improve performance, mainly due to reduced cache performance. 198However, it may make sense to reduce the number of arenas if an application 199does not make much use of the allocation functions. 200.Pp 201Memory is conceptually broken into equal-sized chunks, 202where the chunk size is a power of two that is greater than the page size. 203Chunks are always aligned to multiples of the chunk size. 204This alignment makes it possible to find 205metadata for user objects very quickly. 206.Pp 207User objects are broken into three categories according to size: 208.Bl -enum -offset 3n 209.It 210Small objects are smaller than one page. 211.It 212Large objects are smaller than the chunk size. 213.It 214Huge objects are a multiple of the chunk size. 215.El 216.Pp 217Small and large objects are managed by arenas; huge objects are managed 218separately in a single data structure that is shared by all threads. 219Huge objects are used by applications infrequently enough that this single 220data structure is not a scalability issue. 221.Pp 222Each chunk that is managed by an arena tracks its contents in a page map as 223runs of contiguous pages (unused, backing a set of small objects, or backing 224one large object). 225The combination of chunk alignment and chunk page maps makes it possible to 226determine all metadata regarding small and large allocations in constant time. 227.Pp 228Small objects are managed in groups by page runs. 229Each run maintains a bitmap that tracks which regions are in use. 230Allocation requests can be grouped as follows. 231.Bl -bullet -offset 3n 232.It 233Allocation requests that are no more than half the quantum (see the 234.Em Q 235option) are rounded up to the nearest power of two (typically 2, 4, or 8). 236.It 237Allocation requests that are more than half the quantum, but no more than the 238maximum quantum-multiple size class (see the 239.Em S 240option) are rounded up to the nearest multiple of the quantum. 241.It 242Allocation requests that are larger than the maximum quantum-multiple size 243class, but no larger than one half of a page, are rounded up to the nearest 244power of two. 245.It 246Allocation requests that are larger than half of a page, but small enough to 247fit in an arena-managed chunk (see the 248.Em K 249option), are rounded up to the nearest run size. 250.It 251Allocation requests that are too large to fit in an arena-managed chunk are 252rounded up to the nearest multiple of the chunk size. 253.El 254.Pp 255Allocations are packed tightly together, which can be an issue for 256multi-threaded applications. 257If you need to assure that allocations do not suffer from cache line sharing, 258round your allocation requests up to the nearest multiple of the cache line 259size. 260.Sh DEBUGGING 261The first thing to do is to set the 262.Em A 263option. 264This option forces a coredump (if possible) at the first sign of trouble, 265rather than the normal policy of trying to continue if at all possible. 266.Pp 267It is probably also a good idea to recompile the program with suitable 268options and symbols for debugger support. 269.Pp 270If the program starts to give unusual results, coredump or generally behave 271differently without emitting any of the messages mentioned in the next 272section, it is likely because it depends on the storage being filled with 273zero bytes. 274Try running it with the 275.Em Z 276option set; 277if that improves the situation, this diagnosis has been confirmed. 278If the program still misbehaves, 279the likely problem is accessing memory outside the allocated area. 280.Pp 281Alternatively, if the symptoms are not easy to reproduce, setting the 282.Em J 283option may help provoke the problem. 284In truly difficult cases, the 285.Em U 286option, if supported by the kernel, can provide a detailed trace of 287all calls made to these functions. 288.Pp 289Unfortunately, 290.Nm 291does not provide much detail about the problems it detects; 292the performance impact for storing such information would be prohibitive. 293There are a number of allocator implementations available on the Internet 294which focus on detecting and pinpointing problems by trading performance for 295extra sanity checks and detailed diagnostics. 296.Sh ENVIRONMENT 297The following environment variables affect the execution of the allocation 298functions: 299.Bl -tag -width ".Ev MALLOC_OPTIONS" 300.It Ev MALLOC_OPTIONS 301If the environment variable 302.Ev MALLOC_OPTIONS 303is set, the characters it contains will be interpreted as flags to the 304allocation functions. 305.El 306.Sh EXAMPLES 307To dump core whenever a problem occurs: 308.Bd -literal -offset indent 309ln -s 'A' /etc/malloc.conf 310.Ed 311.Pp 312To specify in the source that a program does no return value checking 313on calls to these functions: 314.Bd -literal -offset indent 315_malloc_options = "X"; 316.Ed 317.Sh RETURN VALUES 318If any of the memory allocation/deallocation functions detect an error or 319warning condition, a message will be printed to file descriptor 320.Dv STDERR_FILENO . 321Errors will result in the process dumping core. 322If the 323.Em A 324option is set, all warnings are treated as errors. 325.Pp 326.\" 327.\" XXX: The _malloc_message should be documented 328.\" better in order to be worth mentioning. 329.\" 330The 331.Va _malloc_message 332variable allows the programmer to override the function which emits 333the text strings forming the errors and warnings if for some reason 334the 335.Dv stderr 336file descriptor is not suitable for this. 337Please note that doing anything which tries to allocate memory in 338this function is likely to result in a crash or deadlock. 339.Pp 340All messages are prefixed by 341.Dq Ao Ar progname Ac Ns Li \&: Pq malloc . 342.Sh SEE ALSO 343.Xr emalloc 3 , 344.Xr malloc 3 , 345.Xr memory 3 , 346.Xr memoryallocators 9 347.\" 348.\" XXX: Add more references that could be worth reading. 349.\" 350.Rs 351.%A Jason Evans 352.%T "A Scalable Concurrent malloc(3) Implementation for FreeBSD" 353.%D April 16, 2006 354.%O BSDCan 2006 355.%U http://people.freebsd.org/~jasone/jemalloc/bsdcan2006/jemalloc.pdf 356.Re 357.Rs 358.%A Poul-Henning Kamp 359.%T "Malloc(3) revisited" 360.%I USENIX Association 361.%B Proceedings of the FREENIX Track: 1998 USENIX Annual Technical Conference 362.%D June 15-19, 1998 363.%U http://www.usenix.org/publications/library/proceedings/usenix98/freenix/kamp.pdf 364.Re 365.Rs 366.%A Paul R. Wilson 367.%A Mark S. Johnstone 368.%A Michael Neely 369.%A David Boles 370.%T "Dynamic Storage Allocation: A Survey and Critical Review" 371.%D 1995 372.%I University of Texas at Austin 373.%U ftp://ftp.cs.utexas.edu/pub/garbage/allocsrv.ps 374.Re 375.Sh HISTORY 376The 377.Nm 378allocator became the default system allocator first in 379.Fx 7.0 380and then in 381.Nx 5.0 . 382In both systems it replaced the older so-called 383.Dq phkmalloc 384implementation. 385.Sh AUTHORS 386.An Jason Evans Aq Mt jasone@canonware.com 387