1. First you need to download it in http://www.linkdata.se/sourcecode/memwatch/
2. The memory tool is open source (I also like open source ^_^ ...), no need to install, just include its header file in your code. Like this #include "Memwatch"
3. The latest version currently used is memwatch-2.71.tar.gz
4. Unzip TAR-XVF memwatch-2.71.tar.gz
5. Copy memwatch.c and MEMWATCH.H to your program directory in the unzip file
6. The test file contains Testmem.c, MEMWATCH.C, memwatch.h
7. Test the code:
1#include <stdio.h>2#include <stdlib.h>3#include <errno.h>4#include"memwatch.h"5 intMain ()6 {7 Char*Hello;8Setenv"Malloc_trace","Output",1);9 mtrace ();Ten if(Hello = (Char*) malloc (sizeof(Char))) ==NULL) One { APerror ("cannot allocate memory"); - return-1; - } the return 0; -}
View Code
8. Test command:
Gcc-dmemwatch-dmw_stdio testmem.c Memwatch.c-o Testmem
9. Run./Testmem
10. Generate Memwatch.log and view the cat Memwatch.log
As to what kind of principle, we can read the source code and the Readme file
Linux C Programming Memory leak tool: Use of Memwatch