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