Red Hat Training

A Red Hat training course is available for Red Hat Enterprise Linux

5.3. Using Valgrind to Profile Memory Usage

Valgrind is a framework that provides instrumentation to user-space binaries. It ships with a number of tools that can be used to profile and analyze program performance. The tools outlined in this section provide analysis that can aid in the detection of memory errors such as the use of uninitialized memory and improper allocation or deallocation of memory. All are included in the valgrind package, and can be run with the following command:
valgrind --tool=toolname program
Replace toolname with the name of the tool you wish to use (for memory profiling, memcheck, massif, or cachegrind), and program with the program you wish to profile with Valgrind. Be aware that Valgrind's instrumentation will cause your program to run more slowly than it would normally.
An overview of Valgrind's capabilities is provided in Section 3.7.3, “Valgrind”. Further details, including information about available plugins for Eclipse, are included in the Developer Guide, available from Accompanying documentation can be viewed with the man valgrind command when the valgrind package is installed, or found in the following locations:
  • /usr/share/doc/valgrind-version/valgrind_manual.pdf, and
  • /usr/share/doc/valgrind-version/html/index.html.

5.3.1. Profiling Memory Usage with Memcheck

Memcheck is the default Valgrind tool, and can be run with valgrind program, without specifying --tool=memcheck. It detects and reports on a number of memory errors that can be difficult to detect and diagnose, such as memory access that should not occur, the use of undefined or uninitialized values, incorrectly freed heap memory, overlapping pointers, and memory leaks. Programs run ten to thirty times more slowly with Memcheck than when run normally.
Memcheck returns specific errors depending on the type of issue it detects. These errors are outlined in detail in the Valgrind documentation included at /usr/share/doc/valgrind-version/valgrind_manual.pdf.
Note that Memcheck can only report these errors — it cannot prevent them from occurring. If your program accesses memory in a way that would normally result in a segmentation fault, the segmentation fault still occurs. However, Memcheck will log an error message immediately prior to the fault.
Memcheck provides command line options that can be used to focus the checking process. Some of the options available are:
When enabled, Memcheck searches for memory leaks when the client program finishes. The default value is summary, which outputs the number of leaks found. Other possible values are yes and full, both of which give details of each individual leak, and no, which disables memory leak checking.
When enabled (set to yes), Memcheck reports errors when undefined values are used. When disabled (set to no), undefined value errors are not reported. This is enabled by default. Disabling it speeds up Memcheck slightly.
Allows the user to specify one or more ranges that Memcheck should ignore when checking for addressability. Multiple ranges are delimited by commas, for example, --ignore-ranges=0xPP-0xQQ,0xRR-0xSS.
For a full list of options, refer to the documentation included at /usr/share/doc/valgrind-version/valgrind_manual.pdf.