Skip site navigation (1)Skip section navigation (2)

FreeBSD Manual Pages


home | help
MALLOC(3)		 BSD Library Functions Manual		     MALLOC(3)

     purpose memory allocation functions

     Standard C	Library	(libc, -lc)

     #include <stdlib.h>

     void *
     malloc(size_t size);

     void *
     calloc(size_t number, size_t size);

     void *
     realloc(void *ptr,	size_t size);

     void *
     reallocf(void *ptr, size_t	size);

     free(void *ptr);

     const char	* _malloc_options;

	 const char *p4);

     #include <malloc_np.h>

     malloc_usable_size(const void *ptr);

     allocated space is	suitably aligned (after	possible pointer coercion) for
     storage of	any type of object.

     of	"number	* size", with the exception that the allocated memory is ex-
     plicitly initialized to zero bytes.

     ory referenced by ptr to size bytes.  The contents	of the memory are un-
     changed up	to the lesser of the new and old sizes.	 If the	new size is
     larger, the contents of the newly allocated portion of the	memory are un-
     defined.  Upon success, the memory	referenced by ptr is freed and a
     identically to malloc() for the specified size.

     with traditional coding styles for	realloc	causing	memory leaks in	li-


     that was requested	during allocation.  The	malloc_usable_size() function
     as	a tool for introspection purposes.  Any	discrepancy between the	re-
     quested allocation	size and the size reported by malloc_usable_size()

     this allocator implementation.

     The "name"	of the file referenced by the symbolic link named
     and the string pointed to by the global variable _malloc_options will be

     10	integer	repetition count.  For example,	"3N" is	equivalent to "NNN".
     Some flags	control	parameter magnitudes, where uppercase increases	the
     magnitude,	and lowercase decreases	the magnitude.	Other flags control
     boolean parameters, where uppercase indicates that	a behavior is set, or
     on, and lowercase means that a behavior is	not set, or off.

     A	     All warnings (except for the warning about	unknown	flags being

     B	     Double/halve the per-arena	lock contention	threshold at which a
	     thread is randomly	re-assigned to an arena.  This dynamic load
	     balancing tends to	push threads away from highly contended	are-
	     nas, which	avoids worst case contention scenarios in which
	     threads disproportionately	utilize	arenas.	 However, due to the
	     highly dynamic load that applications may place on	the allocator,
	     it	is impossible for the allocator	to know	in advance how sensi-
	     tive it should be to contention over arenas.  Therefore, some ap-
	     plications	may benefit from increasing or decreasing this thresh-
	     old parameter.  This option is not	available for some configura-
	     tions (non-PIC).


	     This option is enabled by default.	 See the "M" option for	re-
	     lated information and interactions.

     F	     Double/halve the per-arena	maximum	number of dirty	unused pages
	     that are allowed to accumulate before informing the kernel	about
	     at	least half of those pages via madvise(2).  This	provides the
	     kernel with sufficient information	to recycle dirty pages if
	     physical memory becomes scarce and	the pages remain unused.  The
	     default is	512 pages per arena; MALLOC_OPTIONS=10f	will prevent
	     any dirty unused pages from accumulating.

     G	     When there	are multiple threads, use thread-specific caching for
	     objects that are smaller than one page.  This option is enabled
	     by	default.  Thread-specific caching allows many allocations to
	     be	satisfied without performing any thread	synchronization, at
	     the cost of increased memory use.	See the	"R" option for related
	     tuning information.  This option is not available for some	con-
	     figurations (non-PIC).

	     This is intended for debugging and	will impact performance	nega-

	     size is the maximum of 1 MB and the largest page size that	is
	     less than or equal	to 4 MB.

	     enabled by	default.  If both the "D" and "M" options are enabled,
	     the allocator prefers anonymous mappings over the DSS, but	allo-
	     cation only fails if memory cannot	be acquired via	either method.
	     If	neither	option is enabled, then	the "M"	option is implicitly
	     enabled in	order to assure	that there is a	method for acquiring


	     threaded process that exits while one or more threads are execut-
	     ing in the	memory allocation functions.  Therefore, this option
	     formance tuning aid during	application development.

	     default value is 128 bytes.

     R	     Double/halve magazine size, which approximately doubles/halves
	     the number	of rounds in each magazine.  Magazines are used	by the
	     thread-specific caching machinery to acquire and release objects
	     in	bulk.  Increasing the magazine size decreases locking over-
	     head, at the expense of increased memory usage.  This option is
	     not available for some configurations (non-PIC).

     U	     Generate "utrace" entries for ktrace(1), for all operations.
	     Consult the source	for details on this option.

     V	     Attempting	to allocate zero bytes will return a NULL pointer in-
	     stead of a	valid pointer.	(The default behavior is to make a
	     provided for System V compatibility.  This	option is incompatible
	     with the "X" option.

     X	     Rather than return	failure	for any	allocation function, display a
	     diagnostic	message	on stderr and cause the	program	to drop	core
	     including the following in	the source code:

		   _malloc_options = "X";

	     calls do not zero memory that was previously allocated.  This is
	     intended for debugging and	will impact performance	negatively.

     The "J" and "Z" options are intended for testing and debugging.  An ap-
     plication which changes its behavior when these options are used is

     suboptimal	for several reasons, including race conditions,	increased
     fragmentation, and	artificial limitations on maximum usable memory.  This
     not a primary concern, the	preferred configuration	is MALLOC_OPTIONS=dM
     has little	practical effect for typical applications; use

     This allocator uses multiple arenas in order to reduce lock contention
     pletely independently of each other, which	means a	small fixed increase
     in	overall	memory fragmentation.  These overheads are not generally an
     use of the	allocation functions.

     In	addition to multiple arenas, this allocator supports thread-specific
     caching for small objects (smaller	than one page),	in order to make it
     possible to completely avoid synchronization for most small allocation
     but it increases memory usage and fragmentation, since a bounded number
     of	objects	can remain allocated in	each thread cache.

     possible to find metadata for user	objects	very quickly.

     large, and	huge.  Small objects are smaller than one page.	 Large objects
     are smaller than the chunk	size.  Huge objects are	a multiple of the
     chunk size.  Small	and large objects are managed by arenas; huge objects
     are managed separately in a single	data structure that is shared by all
     threads.  Huge objects are	used by	applications infrequently enough that
     this single data structure	is not a scalability issue.

     contiguous	pages (unused, backing a set of	small objects, or backing one
     large object).  The combination of	chunk alignment	and chunk page maps
     makes it possible to determine all	metadata regarding small and large al-
     locations in constant time.

     bitmap that tracks	which regions are in use.  Allocation requests that
     are no more than half the quantum (8 or 16, depending on architecture)
     are rounded up to the nearest power of two.  Allocation requests that are
     more than half the	quantum, but no	more than the minimum cacheline-multi-
     ple size class (see the "Q" option) are rounded up	to the nearest multi-
     ple of the	quantum.  Allocation requests that are more than the minumum
     cacheline-multiple	size class, but	no more	than the minimum subpage-mul-
     tiple size	class (see the "C" option) are rounded up to the nearest mul-
     tiple of the cacheline size (64).	Allocation requests that are more than
     the minimum subpage-multiple size class are rounded up to the nearest
     multiple of the subpage size (256).  Allocation requests that are more
     than one page, but	small enough to	fit in an arena-managed	chunk (see the
     "K" option), are rounded up to the	nearest	run size.  Allocation requests
     that are too large	to fit in an arena-managed chunk are rounded up	to the
     nearest multiple of the chunk size.

     threaded applications.  If	you need to assure that	allocations do not
     suffer from cacheline sharing, round your allocation requests up to the
     nearest multiple of the cacheline size.

     The first thing to	do is to set the "A" option.  This option forces a
     mal policy	of trying to continue if at all	possible.

     options and symbols for debugger support.

     If	the program starts to give unusual results, coredump or	generally be-
     have differently without emitting any of the messages mentioned in	the
     with zero bytes.  Try running it with the "Z" option set; if that im-
     proves the	situation, this	diagnosis has been confirmed.  If the program
     still misbehaves, the likely problem is accessing memory outside the al-
     located area.

     Alternatively, if the symptoms are	not easy to reproduce, setting the "J"
     option may	help provoke the problem.

     In	truly difficult	cases, the "U" option, if supported by the kernel, can
     provide a detailed	trace of all calls made	to these functions.

     Unfortunately this	implementation does not	provide	much detail about the
     problems it detects; the performance impact for storing such information
     would be prohibitive.  There are a	number of allocator implementations
     available on the Internet which focus on detecting	and pinpointing	prob-
     lems by trading performance for extra sanity checks and detailed diagnos-

     warning condition,	a message will be printed to file descriptor
     "A" option	is set,	all warnings are treated as errors.

     The _malloc_message variable allows the programmer	to override the	func-
     note that doing anything which tries to allocate memory in	this function
     is	likely to result in a crash or deadlock.

     All messages are prefixed by "<progname>: (malloc)".

     set to ENOMEM.

     pointer is	returned, and errno is set to ENOMEM if	the error was the re-
     sult of an	allocation failure.  The realloc() function always leaves the
     original buffer intact when an error occurs, whereas reallocf() deallo-
     cates it in this case.

     The free()	function returns no value.

     tion pointed to by	ptr.

     The following environment variables affect	the execution of the alloca-
     tion functions:

     MALLOC_OPTIONS  If	the environment	variable MALLOC_OPTIONS	is set,	the
		     characters	it contains will be interpreted	as flags to
		     the allocation functions.

     To	dump core whenever a problem occurs:

	   ln -s 'A' /etc/malloc.conf

     calls to these functions:

	   _malloc_options = "X";


     9899:1990 ("ISO C90").

     The reallocf() function first appeared in FreeBSD 3.0.

     The malloc_usable_size() function first appeared in FreeBSD 7.0.

BSD			      September	26, 2009			   BSD


Want to link to this manual page? Use this URL:

home | help