如何找出内存泄露的程序

使用vmstat 3 每3秒观察一下内存变化可以发现,内存一直在减少,而buff 和cache 没变化,说明内存一直在被消耗。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
[email protected]:~# vmstat 3
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
0 0 0 7047444 10788 872936 0 0 264 95 108 98 0 0 99 1 0
0 0 0 7042976 10788 876956 0 0 0 0 1307 1836 3 1 97 0 0
0 0 0 7039280 10796 880596 0 0 0 1941 1536 2961 1 1 97 1 0
……
0 0 0 6940324 14836 970724 0 0 0 3 157 359 0 0 100 0 0
0 0 0 6940452 14844 970724 0 0 0 235 233 348 0 0 98 2 0
2 0 0 6940452 14844 970724 0 0 0 0 147 335 0 0 100 0 0
0 0 0 6940292 14852 970724 0 0 0 5 142 330 0 0 100 0 0
0 0 0 6940292 14860 970724 0 0 0 4 155 343 0 0 100 0 0
0 0 0 6940292 14860 970724 0 0 0 3 136 317 0 0 100 0 0
0 0 0 6940292 14868 970724 0 0 0 12 144 327 0 0 100 0 0
0 0 0 6940292 14868 970724 0 0 0 15 146 319 0 0 100 0 0
0 0 0 6940324 14876 970728 0 0 0 4 148 335 0 0 100 0 0
0 0 0 6940324 14884 970728 0 0 0 7 169 354 0 0 100 0 0
0 0 0 6940324 14884 970728 0 0 0 0 143 357 0 0 100 0 0
1 0 0 6940200 14892 970728 0 0 0 4 155 339 0 0 100 0 0
0 0 0 6940200 14892 970728 0 0 0 3 157 359 0 0 100 0 0
0 0 0 6940232 14900 970728 0 0 0 7 162 353 0 0 100 0 0
0 0 0 6940232 14908 970728 0 0 0 8 227 456 0 0 99 0 0
0 0 0 6940232 14908 970732 0 0 0 149 254 400 0 0 98 1 0
0 0 0 6940232 14916 970732 0 0 0 9 163 344 0 0 100 0 0
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
1 0 0 6940108 14916 970732 0 0 0 0 140 299 0 0 100 0 0
0 0 0 6940108 14924 970732 0 0 0 8 154 342 0 0 100 0 0
0 0 0 6940016 14932 970732 0 0 0 8 165 354 0 0 99 0 0
0 0 0 6940016 14932 970732 0 0 0 8 138 304 0 0 100 0 0
0 0 0 6940016 14940 970736 0 0 0 4 145 323 0 0 100 0 0
0 0 0 6940016 14940 970736 0 0 0 3 135 328 0 0 100 0 0
0 0 0 6939892 14948 970736 0 0 0 7 151 319 0 0 100 0 0
0 0 0 6939924 14956 970736 0 0 0 7 149 324 0 0 99 0 0
0 0 0 6939924 14956 970736 0 0 0 3 129 302 0 0 100 0 0
0 0 0 6939924 14964 970736 0 0 0 9 150 365 0 0 100 0 0
0 0 0 6939924 14964 970736 0 0 0 89 169 310 0 0 99 1 0
0 0 0 6939800 14972 970736 0 0 0 5 141 329 0 0 99 0 0
0 0 0 6939832 14980 970736 0 0 0 7 141 306 0 0 100

那到底是什么进程在消耗内存资源呢?通过memleak 查看进程的内存分配请求以及地址,可以看到该进程不停的在分配进程。并且这些被分配的进程没有被回收。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
[email protected]:~# memleak -a -p $(pidof app)
Attaching to pid 11477, Ctrl+C to quit.
[15:22:44] Top 10 stacks with outstanding allocations:
addr = 7f70e43127b0 size = 8192
addr = 7f70e430e790 size = 8192
addr = 7f70e430c780 size = 8192
addr = 7f70e43107a0 size = 8192
32768 bytes in 4 allocations from stack
fibonacci+0x1f [app]
child+0x4f [app]
start_thread+0xdb [libpthread-2.27.so]
[15:22:49] Top 10 stacks with outstanding allocations:
addr = 7f70e431a7f0 size = 8192
addr = 7f70e43147c0 size = 8192
addr = 7f70e431c800 size = 8192
addr = 7f70e43127b0 size = 8192
addr = 7f70e43187e0 size = 8192
addr = 7f70e430e790 size = 8192
addr = 7f70e43167d0 size = 8192
addr = 7f70e430c780 size = 8192
addr = 7f70e43107a0 size = 8192
73728 bytes in 9 allocations from stack
fibonacci+0x1f [app]
child+0x4f [app]
start_thread+0xdb [libpthread-2.27.so]

并且从上图可以看到是 fibonacci() 函数分配内存没有被释放。

阿文 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
坚持原创技术分享,您的支持将鼓励我继续创作!