NERSCPowering Scientific Discovery Since 1974



The Valgrind tool suite provides a number of debugging and profiling tools that help you make your programs faster and more correct. The most popular of these tools is called Memcheck. It can detect many memory-related errors that are common in C and C++ programs and that can lead to crashes and unpredictable behaviour.

Using Valgrind on Hopper

Prepare Your Program

Compile your program with -g to include debugging information so that Memcheck's error messages include exact line numbers. Using -O0 is also a good idea, if you can tolerate the slowdown. With -O1 line numbers in error messages can be inaccurate, although generally speaking running Memcheck on code compiled at -O1 works fairly well, and the speed improvement compared to running -O0 is quite significant. Use of -O2 and above is not recommended as Memcheck occasionally reports uninitialised-value errors which don't really exist.

Load Module

 module load valgrind

Running Serial Programs

If you normally run your program like this:

myprog arg1 arg2

Use this command line:

valgrind --leak-check=yes myprog arg1 arg2

Memcheck is the default tool. The --leak-check option turns on the detailed memory leak detector.

Your program will run much slower (eg. 20 to 30 times) than normal, and use a lot more memory. Memcheck will issue messages about memory errors and leaks that it detects.

Running Parallel Programs Interactively

Get an interactive terminal (sample command below).

qsub -I -V -q interactive -l mppwidth=24 -l walltime=00:30:00

If you are running your program like this:

aprun -n 24 ./myprog arg1 arg2

Simply add valgrind to your command (after aprun).

module load valgrind
aprun -n 24 valgrind --leak-check=yes ./myprog arg1 arg2

Running Parallel Programs in Batch

In your batch script, simply 1. load the module; 2. add "valgrind" in front of your command. For example, your aprun line will be replace by the following:

module load valgrind
aprun -n 24 valgrind --leak-check=yes ./myprog arg1 arg2

Link to Outside Documentation

This page is based on the "Valgrind Quick Start Page". For more information about valgrind, please refer to

For questions on using Valgrind at NERSC contact the consultants at


PackagePlatformCategoryVersionModuleInstall DateDate Made Default
Valgrind carver applications/ debugging 3.9.0 valgrind/3.9.0 2014-01-06 2014-01-06
Valgrind edison applications/ debugging 3.9.0 valgrind/3.9.0 2014-01-06 2014-01-13
valgrind genepool applications/ programming 3.7.0 valgrind/3.7.0 2012-09-28 2012-09-28
valgrind hopper applications/ debugging 3.6.1 valgrind/3.6.1 2011-03-25 2013-05-03
Valgrind hopper applications/ debugging 3.8.1 valgrind/3.8.1 2013-05-03 2013-05-03
Valgrind hopper applications/ debugging 3.9.0 valgrind/3.9.0 2014-01-07
valgrind phoebe applications/ programming 3.7.0 valgrind/3.7.0 2012-07-03 2013-06-26