Configuring the Malloc Environment Variables

The malloc library provides debugging features to help you track down memory smashing bugs, heap corruption, references to freed memory, and buffer overruns. 

You enable these debugging options through a set of environment variables. With the exception of MallocCheckHeapStart and MallocCheckHeapEach, the value for most of these environment variables is ignored.

To disable a variable from Terminal, use the unset command. 

Table 1 lists some of the key environment variables and describes their basic function. For a complete list of variables, see the malloc man page.



Table 1  Malloc environment variables
VariableDescription
MallocStackLoggingIf set, malloc remembers the function call stack at the time of each allocation.
MallocStackLoggingNoCompactThis option is similar to MallocStackLogging but makes sure that all allocations are logged, no matter how small or how short lived the buffer may be.
MallocScribbleIf set, free sets each byte of every released block to the value 0x55.
MallocPreScribbleIf set, malloc sets each byte of a newly allocated block to the value 0xAA. This increases the likelihood that a program making assumptions about freshly allocated memory fails.
MallocGuardEdgesIf set, malloc adds guard pages before and after large allocations.
MallocDoNotProtectPreludeFine-grain control over the behavior of MallocGuardEdges: If set, malloc does not place a guard page at the head of each large block allocation.
MallocDoNotProtectPostludeFine-grain control over the behavior of MallocGuardEdges: If set, malloc does not place a guard page at the tail of each large block allocation.
MallocCheckHeapStartSet this variable to the number of allocations before malloc will begin validating the heap. If not set, malloc does not validate the heap.
MallocCheckHeapEachSet this variable to the number of allocations before malloc should validate the heap. If not set, malloc does not validate the heap.

The following example enables stack logging and heap checking in the current shell before running an application. The value for MallocCheckHeapStart is set to 1 but is irrelevant and can be set to any value you want.

You could also set these variables from your shell’s startup file, although if you do be sure to export each variable.

% MallocStackLogging=1
% MallocCheckHeapStart=1000
% MallocCheckHeapEach=100
% ./my_tool

 If you want to run your program in gdb, you can set environment variables from the Xcode debugging console using the command set env, as shown in the following example:

% gdb
(gdb) set env MallocStackLogging 1
(gdb) run

Some of the performance tools require these options to be set in order to gather their data. For example, the malloc_history tool can identify the allocation site of specific blocks if the MallocStackLogging flag is set. This tool can also describe the blocks previously allocated at an address if the MallocStackLoggingNoCompact environment variable is set.

The leaks command line tool will name the allocation site of a leaked buffer if MallocStackLogging is set.