valgrind-3.8.1的安裝與使用研究

來源:互聯網
上載者:User

目前只知道這個工具可以測試linux下的記憶體流失問題,很好用,下載地址是http://www.valgrind.org/downloads/valgrind-3.8.1.tar.bz2

安裝:


[root@localhost ~]# cd valgrind-3.8.1
[root@localhost valgrind-3.8.1]# ./configure && make && makeinstall  


完成後 

進行測試看是否安裝成功。

[ubuntu@root ~]#valgrind


看結果是否為:


[ubuntu@root ~]#valgrind
valgrind: no program specified
valgrind: Use --help for more information.


然後就可以對程式就行了測試了:


測試:


3、樣本(測試一個C)

#include <stdio.h>                                                                  

#include <stdlib.h>

int main()

{

       struct mm{

                int a;

                int b;

       }kk,*p;

       kk.a = 10;

       printf("kk.a : %d \n",kk.a);

       p = (struct mm *)malloc(sizeof(struct mm));

       p->a = 9;

       printf("p->a : %d \n",p->a);

 

return 0;

}

操作如下:

[root@localhost ~]# vim hello.c

[root@localhost ~]# gcc -g -o hello hello.c

[root@localhost ~]# ./hello


結果如下:

然後用valgrind命令:

[root@localhost ~]# valgrind --tool=memcheck--leak-check=yes --show-reachable=yes ./hello

可以看出上面提示“malloc/free:1 allocs, 0 frees, 8 bytes allocated.”,“definitely lost:8 bytes in 1 blocks.”。即丟失了8個位元組。


例子引用了其他人的。。。。。

這個工具不錯,對於開發arm上這種比較需要注意資源使用的程式,很有協助

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.