ArticleslgStudy

computer science

Mtrace

Mtrace is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Mtrace rather than just read about it. In short: mtrace is the memory debugger included in the GNU C Library. Use mtrace tool works only with single threaded applications.

Key takeaways

  • Mtrace belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Mtrace to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Mtrace from memory before moving on to harder problems.

Reference excerpt

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

Use mtrace tool works only with single threaded applications. In a multithreaded application a problem exists that one thread could temporarily remove the hook while another thread could malloc memory leading to missed allocations. The function mtrace installs handlers for malloc, realloc and free; the function muntrace disables these handlers. Their prototypes, defined in the header file <mcheck.h>, are

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.

Usage example

Bad source code The following is an example of bad source code. The problem with the program is that it allocates memory, but doesn't free the memory before exiting.

MTrace usage Set the environment variable MALLOC_TRACE to the pathname of the desired output file. Setting environment variables is slightly different in each shell. In Bourne Shell-compatible shells, like Bash, the command is as follows:

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:

Call the function mtrace() before allocating memory. It is usually easiest to call mtrace() at the very beginning of the main() function:

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:

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:

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:

For example:

mtrace can be used with parallel computing but one process at a time, using a condition on the rank like:

MTrace output If the mtrace command reports “No Memory Leaks”, then all memory that was allocated in the last execution of that program was also released, which is the way it should be. If, on the other hand, mtrace gives output such as that below, it means the programmer still has some work to do.

Memory not freed: ----------------- Address Size Caller 0x08049910 0x4 at /home/sureshsathiah/tips/leak.c:9

Good source code The following is an example of good source code. It releases memory after it is allocated, and it uses mtrace to notify the programmer if there are memory leaks.

External links GNU C Library manual: Allocation debugging Manual page for mtrace Manual page for the perl script mtrace Source code for the perl script Linux Journal: Memory Leak Detection in Embedded Systems

Worked examples

Example 1 — a first encounter with Mtrace

Start with the simplest possible case. Write down what Mtrace claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Mtrace before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Mtrace ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Mtrace

In research
Mtrace appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Mtrace in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Mtrace is common in secondary-school and first-year university syllabi. It links to neighbouring topics Debugging, Free memory management software, so understanding it makes those chapters shorter.
In everyday life
Look for Mtrace outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Mtrace” →

How to study Mtrace in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Mtrace means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Mtrace out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Mtrace in simple terms?

mtrace is the memory debugger included in the GNU C Library. Use mtrace tool works only with single threaded applications.

Why does Mtrace matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Mtrace?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Mtrace.

Tags

  • Debugging
  • Free memory management software

Keep exploring