encyclopegia.org

Mtrace

mtrace is the memory debugger included in the GNU C Library.

mtrace is the memory debugger included in the GNU C Library. void mtrace(void); void muntrace(void); The handlers log all memory allocations and frees to a file defined by the environment variable MALLOC_TRACE (if the variable is unset, describes an invalid filename, or describes a filename the user does not have permissions to, the handlers are not installed). A Perl script called mtrace , not to be confused with the function of the same name, is also distributed with the GNU C Library; the script parses through the output file and reports all allocations that were not freed. include int main(void) { int* a = (int*)malloc(sizeof(int)); // allocate memory and assign it to the pointer return 0; // Exit the program without freeing memory // we should have released the allocated memory with the statement “free(a)” } $ MALLOC_TRACE=/home/YourUserName/path/to/program/MallocTraceOutputFile.txt $ export MALLOC_TRACE Include mcheck.h in the source code. This is done, for example, by adding the following line to the top of a C or C++ file, as shown below: include Call the function mtrace() before allocating memory. It is usually easiest to call mtrace() at the very beginning of the main() function: mtrace(); To delineate the end of the code that should be traced, call the function muntrace() . This is usually done at the end of the main() function: muntrace(); Compile and run the program as usual. Note that one must compile with the -g flag to get useful output. In GCC on Linux, this can be done using the following commands for a C program: $ gcc yourProgram.c -g $ ./a.out Memory leak information will be reported in the file specified by the MALLOC_TRACE environment variable. The difficulty is, this file will be in a computer-readable format. Most Linux machines come with a console command called mtrace , that converts the computer readable format into human-readable text as shown below. There is a Perl script, of the same name, that can be downloaded to accomplish the same task, in the event that one does not have access to the command mtrace . The mtrace syntax is as follows: $ mtrace For example: $ mtrace a.out MallocTraceOutputFile.txt mtrace can be used with parallel computing but one process at a time, using a condition on the rank like: if (my_rank == 0) { mtrace(); } include include int main(void) { mtrace(); // Starts the recording of memory allocations and releases int* a = (int*)malloc(sizeof(int)); // allocate memory and assign it to the pointer if (!a) { return EXIT_FAILURE; // error } free(a); // free the allocated memory muntrace(); return EXIT_SUCCESS; // exit } Source code for the perl script Linux Journal: Memory Leak Detection in Embedded Systems

References

No reference rows published for this article.

Cite this article

MLA

"Mtrace." encyclopegia.org, 2026, https://encyclopegia.org/wiki/mtrace. Accessed 24 November 2026.

Chicago

"Mtrace." encyclopegia.org. Accessed November 24, 2026. https://encyclopegia.org/wiki/mtrace.

APA

encyclopegia.org. (2026). Mtrace. Retrieved November 24, 2026, from https://encyclopegia.org/wiki/mtrace

Accessed November 24, 2026 · CC BY-SA 4.0 · citation page · JSON

CC BY-SA 4.0 · share-alike · https://en.wikipedia.org/w/index.php?oldid=1378133385 · 0.5 · snapshot enwiki-001378133385

Cite · JSON · Search

Related services and records

Services

  • Estate — measured countries, lanes and facts
  • Intel — live corroborated events
  • Legal — statutes, cases and citations
  • Encyclopedia — articles with inline sources
  • Space — launches, satellites and orbits
  • Search — federated search and reports
  • Pass — passport account and SSO
  • Corpora — read-only corpus origins
  • e01 — self-hosted corpora and tools

Text and metadata only; each target carries its own sources. Missing data is published as no-data, never estimated.