If you want to build Valgrind for 64 bit Windows, download and install a 64 bit tool chain, either: (recommended) TDM-GCC - download tdm64-gcc-4.6.1 installer from SourceForge, e.g. Tdm64-gcc-4.6.1.exe (gcc 4.6.1). Still way better than 1.75GB that stock valgrind allows. Fri Aug 12 2005 Jakub Jelinek 3.0.0-1 - upgrade to 3.0.0 - x8664 support - temporarily obsolete valgrind-callgrind, as it has not been ported yet. Tue Jul 12 2005 Jakub Jelinek 2.4.0-3 - build some insn tests with -mmmx, -msse or -msse2 (#161572. Valgrind is an instrumentation framework for building dynamic analysis tools. There are Valgrind tools that can automatically detect many memory management and threading bugs, and profile your programs in detail. You can also use Valgrind to build new tools.
When developing programs under Linux, especially server-side programs, it is always in a loop. If it is found that there is no problem when the program first runs, but after a period of time, the program stops. The high probability of this situation is a memory leak.
Step 1:
We can use a simple command “free -m” to check whether it is really a memory leak. As shown below: If the memory usage is increasing and the free is decreasing as the program runs, it can be preliminarily identified as a memory leak.
Step 2:
In order to prevent memory leaks caused by other running programs in the system, we can use another tool to know more precisely whether our program has a memory leak.
We can use “pmap -x <pid>” command to see the result. The pid can be obtained through the “top” command. In this way, we can always look at the program we are running and this tool can also see the stack and heap allocation.
Suppose, we have a piece of code like this:
We use “pmap -x 20521” command to see the result. 20521 is the pid.
The RSS (Resident Set Size) in the result is closer to the size of the actual memory used by the computer. [anon] in mapping column indicates that it is allocated on the heap, which is also consistent with our code. As our program runs, we will find that more and more heaps are allocated, which means that our program has a memory leak.
This program was specially written by me, so it can be seen at a glance that the memory leak is because I have been malloc, but did not call free to release them. In many cases, it is easy to find the memory leak caused by without releasing malloc.
But sometimes, when we call other functions, such as the regcomp() function, there is a implicit memory allocatation process in this function.but because I did not call regfree to release, it caused a memory leak, which is more difficult to find.
Next, I will introduce how to download and install this tool.
Download and Install Valgrind
Method one:
- wget http://valgrind.org/downloads/valgrind-3.4.1.tar.bz2
- tar xvf [FileName].tar.bz2
- cd [Unzipped directory]
- ./configure
- make
- sudo make install
Method two:
- sudo apt-get install valgrind
It is said that this method is very slow. So I used method one.
But there is a problem with method one that is some dependencies, it does not follow the installation, so there will be an error when using it, the error message prompts you to say that xxx is missing. Then, at this time, just running “sudo apt-get install xxx” command is OK.
Then I can start to introduce this tool and how to use it.
Valgrind Usage
- Valgrind is a toolkit that helps us locate errors faster, make development faster, and make the application more correct. One of the most popular tools is MemCheck.It can detect many of the memory-related errors common in C/C++ that can cause a program to crash or have unpredictable results. This is the Memcheck tool that we’re going to use.
- In order for MemCheck to extract some debug information, we add -g when compiling the program. For example, if it is normally “gcc xxx.c“, then we are now “gcc -g xxx.c“.
- If we run a program normally is “./myprog arg1 arg2“. Now we are going to use “Valgrind –leak-check=yes./myprog arg1 arg2” command to run this program.
Let me give you an example, an example from the official webiste.
The result is:
Download Valgrind
Again, this tool is really powerful. And as you can see from the results, it’s very straightforward and precise, it finds the errors that are illegal, and it tells you in which function and in which line. It gets more and more precise from the bottom up, so let’s say we start with main, and then we start with f function. The following memory is not free is also directly pointed out. Such a handy tool is really worth spreading the word.
Download Valgrind Executable
Download Valgrind Linux
Have a good day! Any feedback is welcome.
Reference:
Download Valgrind Source Code
For more information, you can visit the website: http://valgrind.org/docs/manual/QuickStart.html