2.15 Memory Tracing

Tracing of memory allocation and deallocation is provided by the -mtrace option. Control is provided over whether the address, size, and line number of each allocation is displayed, or the tracing output can be suppressed entirely. A “paranoia” mode is provided where the memory allocator protects its data structures against inadvertent modification by the user program.

Runtime environment variables may be used to override the tracing options a program was built with, and to specify where to write the tracing output. These are only operative if the program was built with some tracing option; -mtrace=off will build a program with the tracing-capable memory allocator.

If -mtrace=off is not specified, use of any -mtrace option will implicitly do a -mtrace=on.

Basic tracing produces a message to the memory tracing file (normally standard error) for each allocation and deallocation, including those for automatic variables, i/o buffers and compiler-generated temporaries. Each allocation is numbered sequentially; the first three items are the i/o buffers for units 0, 5 and 6 (standard error, standard input and standard output).

All -mtrace= suboptions may be overridden at run time by the NAGFORTRAN_MTRACE_OPTIONS environment variable, which should be set to the required trace_opt_list (e.g. ‘on,size’). The memory tracing file may be specified at run time by the NAGFORTRAN_MTRACE_FILE environment variable.

The NAGFORTRAN_MTRACE_OPTIONS variable can also contain an option to limit the total amount of memory that may be allocated. The ‘limit=N’ option limits the maximum memory allocated to N MiB (mebibytes), but only if the program was built with a tracing option (minimally, -mtrace=off). Exceeding the memory limit will result in a normal “out of memory” condition, which if it occurs in an ALLOCATE statement, can be captured by a STAT= clause. Note that the memory limit applies to the overall memory usage including automatic variables and compiler-generated array temporaries.

The -mtrace option must be specified when linking, and is incompatible with -gc. Additionally, line number information is only available for those files compiled with -mtrace=line.

The nagfmcheck program can be used to check the output from the -mtrace option. It is designed to be used as a filter. Any lines that do not look like memory tracing output are ignored. It reports to standard output any errors it detects such as deallocating something twice, deallocating something that was never allocated, or deallocating something with a size different from that with which it was allocated. It also reports any apparent memory leaks, though this is less useful if the program terminated prematurely.