[ Bottom of Page | Previous Page | Next Page | Contents | Index | Library Home | Legal | Search ]

General Programming Concepts:
Writing and Debugging Programs

Malloc Log

Malloc Log is an optional extension of the malloc subsystem, enabling the user to obtain information showing the number of active allocations of a given size and stack traceback made by the malloc subsystem. This data can then be used in problem determination and performance analysis.

Malloc Log is not enabled by default, but can be enabled and configured prior to process startup by setting the MALLOCDEBUG environment variable.

Data Recorded in the Malloc Log

Malloc Log records the following data for each malloc or realloc subroutine invocation:

The data is stored into the following global structure:

struct malloc_log * malloc_log_table;

#ifndef MALLOC_LOG_STACKDEPTH
#define MALLOC_LOG_STACKDEPTH 4
#endif
struct malloc_log {
   size_t size;
   size_t cnt;
   uintptr_t callers[MALLOC_LOG_STACKDEPTH];
}

size_t malloc_log_size;

The size of the malloc_log structure can change. If the default call-stack depth is greater than 4, the structure will have a larger size. The current size of the malloc_log structure is stored in the globally exported malloc_log_size variable . A user can define the MALLOC_LOG_STACKDEPTH macro to the stack depth that was configured at process start time.

The malloc_log_table can be accessed in the following ways:

To clear all existing data from the malloc log tables, use the reset_malloc_log API as follows:

#include malloc.h
void reset_malloc_log(void *addr);

Enabling Malloc Log

Malloc Log is not enabled by default. To enable Malloc Log with the default settings, set the MALLOCDEBUG environment variable as follows:

MALLOCDEBUG=log

To enable Malloc Log with user-specified configuration options, set the MALLOCDEBUG environment variable as follows:

MALLOCDEBUG=log:records_per_heap:stack_depth
Note
The records_per_heap and stack_depth parameters must be specified in order. Leaving a value blank will result in setting that parameter to the default value.

The predefined MALLOCDEBUG configuration options include the following:

records_per_heap
Used to specify the number of Malloc Log records that are stored for each heap. This parameter affects the amount of memory that is used by Malloc Log. The default value is 4096, the maximum value is 65535.
stack_depth
Used to specify the depth of the function-call stack that is recorded for each allocation. This parameter affects the amount of memory used by Malloc Log, as well as the size of the malloc_log structure. The default value is 4, the maximum is 32.

Limitations

The performance of all programs can degrade when Malloc Log is enabled, due to the cost of storing data to memory. Memory usage will also increase.

[ Top of Page | Previous Page | Next Page | Contents | Index | Library Home | Legal | Search ]