還可以這樣查內(nèi)存泄漏
一、前言
對(duì)于C或C++程序員來(lái)說(shuō),面對(duì)的bug很大部分是內(nèi)存操作問(wèn)題,這其中比較令人頭疼的就是內(nèi)存泄漏了,雖然我們有valgrind 和AScan等內(nèi)存問(wèn)題的檢測(cè)工具,但是valgrind每次輸出一大堆,AScan有時(shí)候看輸出結(jié)果看的是云里霧里的。再說(shuō),誰(shuí)會(huì)嫌棄工具箱里面多個(gè)工具那。
二、 內(nèi)存泄漏的一般檢查
2.1 基本準(zhǔn)備
內(nèi)存泄漏問(wèn)題的檢查步驟,對(duì)于做過(guò)c或c++同學(xué)都比較熟悉:
首先通過(guò)top或vmstat 、或smem(本次介紹)等工具查看內(nèi)存情況,看看是否出現(xiàn)了內(nèi)存泄漏。
其次用pidstat 或top指定進(jìn)程的方式,觀察可以進(jìn)程內(nèi)存占用情況。
用memleak或gdb工具查看內(nèi)存泄漏。
先上測(cè)試代碼:
#include <stdio.h>#include <stdlib.h>#include <pthread.h>#include <unistd.h>#define MALLOC_SIZE 256000int *fibo(int *n0, int *n1){ int *v = (int *) malloc(MALLOC_SIZE*sizeof(int)); memset(v, 0, MALLOC_SIZE*sizeof(int)); *v = *n0 + *n1; return v;}void do_test(){ int n0 = 0; int n1 = 1; int *v = NULL; int n = 2; for (n = 2; n > 0; n++) { v = fibo(&n0, &n1); n0 = n1; n1 = *v; printf("%dth => %lld\n", n, *v); //free(v) sleep(1); }}int main(void){ printf("pid=%d\n", getpid()); do_test(); return 0;}
程序比較簡(jiǎn)單,編譯運(yùn)行起來(lái):
gcc memtest.c ; ./a.out
2.2 smem工具
這次用下新工具smem,這是一個(gè)python寫(xiě)的小工具,可以統(tǒng)計(jì)系統(tǒng)中所有進(jìn)程占用的物理內(nèi)存RSS、以及去掉共享內(nèi)存的PSS、以及程序本身的獨(dú)占內(nèi)存USS的情況。
安裝:
# centos 下yum install epel-releaseyum install smem python-matplotlib python-tk# ubuntu 下apt-get install smem
常用命令:
-k 帶單位顯示內(nèi)存
root@ubuntu-lab:/home/miao# smem -k PID User Command Swap USS PSS RSS 1009 root /usr/sbin/cron -f -P 0 304.0K 399.0K 2.9M 1137 root nginx: master process /usr/ 0 196.0K 435.0K 2.1M 931 root /usr/sbin/irqbalance --fore 0 492.0K 655.0K 4.0M ....
-u -k 帶單位顯示每個(gè)用戶的內(nèi)存占用:
root@ubuntu-lab:/home/miao# smem -u -kUser Count Swap USS PSS RSS systemd-timesync 1 0 764.0K 1.1M 6.7M messagebus 1 0 924.0K 1.2M 4.9M systemd-network 1 0 1.7M 2.1M 7.4M syslog 1 0 3.0M 3.1M 6.2M www-data 4 0 2.0M 4.2M 22.4M systemd-resolve 1 0 4.8M 5.8M 12.7M miao 8 0 11.0M 16.9M 49.1M postgres 7 0 9.2M 22.0M 74.5M mysql 1 0 74.0M 74.7M 80.7M root 30 0 260.7M 284.1M 429.5M
-w -k 顯示系統(tǒng)整體內(nèi)存情況類似free
root@ubuntu-lab:/home/miao# smem -w -kArea Used Cache Noncache firmware/hardware 0 0 0 kernel image 0 0 0 kernel dynamic memory 1.5G 1.3G 268.5M userspace memory 414.0M 191.5M 222.5M free memory 2.8G 2.8G 0
-k -s uss -r 按照uss的占用從大到小排序的方式展示內(nèi)存的占用情況 非常實(shí)用
root@ubuntu-lab:/home/miao# smem -k -s uss -r PID User Command Swap USS PSS RSS 1298 root /usr/bin/dockerd -H 0 74.3M 74.5M 77.9M 1068 mysql /usr/sbin/mariadbd 0 74.0M 74.8M 80.7M 939 root /usr/lib/snapd/snapd 0 44.9M 45.0M 46.7M ....
好了基本命令介紹完畢,那我們來(lái)看看如何查看內(nèi)存是否泄漏吧,因?yàn)閮?nèi)存泄漏的程序占用的內(nèi)存是一直再增加的(這不是廢話嘛),這樣我們就可以用上面的排序命令只觀察上面幾個(gè)進(jìn)程了。
watch smem -k -s uss -r
小技巧,watch加在命令前面,5s執(zhí)行一次命令,會(huì)高亮顯示改變的部分。
2.3 memleak檢查
在ubuntu下安裝memleak竟然很難安裝,我用的是最新的服務(wù)器版本,后面在centos下安裝后測(cè)試的:
[root@xxx]# python2 /usr/share/bcc/tools/memleak -p 160399Attaching to pid 160399, Ctrl+C to quit.[17:27:25] Top 10 stacks with outstanding allocations: 5120000 bytes in 5 allocations from stack fibo+0x1a [a.out] do_test+0x41 [a.out] main+0x24 [a.out] __libc_start_main+0xf5 [libc-2.17.so][17:27:30] Top 10 stacks with outstanding allocations: 10240000 bytes in 10 allocations from stack fibo+0x1a [a.out] do_test+0x41 [a.out] main+0x24 [a.out] __libc_start_main+0xf5 [libc-2.17.so][17:27:35] Top 10 stacks with outstanding allocations: 15360000 bytes in 15 allocations from stack fibo+0x1a [a.out] do_test+0x41 [a.out] main+0x24 [a.out] __libc_start_main+0xf5 [libc-2.17.so][17:27:40] Top 10 stacks with outstanding allocations: 19456000 bytes in 19 allocations from stack
fibo 函數(shù)出現(xiàn)內(nèi)存泄漏,把泄漏的字節(jié)數(shù)都打印了出來(lái),我們改了下代碼把free的注釋去掉,再用memleak查看等了一會(huì)還是沒(méi)有泄漏信息,說(shuō)明已經(jīng)修復(fù)了,如下:
[root@xxx]# python2 /usr/share/bcc/tools/memleak -p 165349Attaching to pid 165349, Ctrl+C to quit.[17:35:21] Top 10 stacks with outstanding allocations:[17:35:26] Top 10 stacks with outstanding allocations:[17:35:31] Top 10 stacks with outstanding allocations:[17:35:36] Top 10 stacks with outstanding allocations:
三、gdb 查看內(nèi)存泄漏
也許你對(duì)memleak已經(jīng)很熟悉了,那來(lái)看看gdb查看函數(shù)的內(nèi)存泄漏方法吧,這個(gè)方法只是查看具體的一個(gè)函數(shù)是否存在內(nèi)存泄漏,一定的場(chǎng)景下還是蠻實(shí)用的。 把代碼中的 for (n = 2; n > 0; n++) 改成 for (n = 2; n > 0&& n <10; n++)
(gdb) b mainBreakpoint 1 at 0x400739: file memleaktest.c, line 34.(gdb) rStarting program: /home/miaohq/testcode/./a.out Breakpoint 1, main () at memleaktest.c:3434 printf("pid=%d\n", getpid());Missing separate debuginfos, use: debuginfo-install glibc-2.17-325.el7_9.x86_64(gdb) call malloc_stats()Arena 0:system bytes = 0in use bytes = 0Total (incl. mmap):system bytes = 0in use bytes = 0max mmap regions = 0max mmap bytes = 0$1 = -136490560(gdb) npid=18197735 do_test();(gdb) call malloc_stats()Arena 0:system bytes = 0in use bytes = 0Total (incl. mmap):system bytes = 0in use bytes = 0max mmap regions = 0max mmap bytes = 0$2 = -136490560(gdb) n2th => 13th => 24th => 35th => 56th => 87th => 138th => 219th => 3436 return 0;(gdb) call malloc_stats()Arena 0:system bytes = 0in use bytes = 0Total (incl. mmap):system bytes = 8224768in use bytes = 8224768max mmap regions = 8max mmap bytes = 8224768$3 = -136490560(gdb) p 256000*4*8$4 = 8192000(gdb)
Total (incl. mmap):即本程序占用的總內(nèi)存,看到明顯的增加部分即為未釋放的內(nèi)存,程序使用的內(nèi)存增加:8224768 稍大于 256000*4*8 分配的內(nèi)存,內(nèi)存分配需要存儲(chǔ)鏈表還有一些對(duì)齊原因所以會(huì)多分配些。
free之后的場(chǎng)景:
(gdb) call malloc_stats()Arena 0:system bytes = 0in use bytes = 0Total (incl. mmap):system bytes = 0in use bytes = 0max mmap regions = 0max mmap bytes = 0$1 = -136490560(gdb) npid=18340635 do_test();(gdb) n2th => 13th => 24th => 35th => 56th => 87th => 138th => 219th => 3436 return 0;(gdb) call malloc_stats()Arena 0:system bytes = 1159168in use bytes = 0Total (incl. mmap):system bytes = 1159168in use bytes = 0max mmap regions = 1max mmap bytes = 1028096$2 = -136490560(gdb)
in use bytes 為0了。?