[완료]pcap 함수 메모리 누수현상?? (코드 10줄도 안됩니다)
글쓴이: ecstasy5001 / 작성시간: 월, 2007/10/08 - 4:34오후
#include pcap.h
int main()
{
pcap_t *dev;
char errbuf[1000];
while(1)
{
dev = pcap_open_live("eth0", BUFSIZ,1, 1000, errbuf);
pcap_close(dev);
}
return 0;코드는 위와 같습니다.
실행을 하고 확인해본결과 메모리 누수현상이 있습니다.
왜 위 코드가 메모리 누수현상이 나는지 조언좀 부탁드리겠습니다.
Forums:


[bushi@rose net]$ uname
[bushi@rose net]$ uname -a Linux rose.mizi.com 2.6.22.9-91.fc7 #1 SMP Thu Sep 27 23:10:59 EDT 2007 i686 i686 i386 GNU/Linux [bushi@rose net]$ [bushi@rose net]$ rpm -q libpcap libpcap-0.9.7-1.fc7 [bushi@rose net]$ [bushi@rose net]$ cat pcap.c #include <stdio.h> #include <pcap.h> int main() { pcap_t *dev; char errbuf[1000]; int i = 0; do { i++; dev = pcap_open_live("eth0", BUFSIZ,1, 1000, errbuf); if (!dev) { perror("eth0"); continue; } pcap_close(dev); } while (i<100); return 0; } [bushi@rose net]$ [bushi@rose net]$ gcc -s -o p pcap.c -Wall -lpcap [bushi@rose net]$ [bushi@rose net]$ sudo valgrind ./p ==26139== Memcheck, a memory error detector. ==26139== Copyright (C) 2002-2007, and GNU GPL'd, by Julian Seward et al. ==26139== Using LibVEX rev 1732, a library for dynamic binary translation. ==26139== Copyright (C) 2004-2007, and GNU GPL'd, by OpenWorks LLP. ==26139== Using valgrind-3.2.3, a dynamic binary instrumentation framework. ==26139== Copyright (C) 2000-2007, and GNU GPL'd, by Julian Seward et al. ==26139== For more details, rerun with: -v ==26139== ==26139== ==26139== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 13 from 1) ==26139== malloc/free: in use at exit: 0 bytes in 0 blocks. ==26139== malloc/free: 400 allocs, 400 frees, 869,100 bytes allocated. ==26139== For counts of detected errors, rerun with: -v ==26139== All heap blocks were freed -- no leaks are possible. [bushi@rose net]$깨끗한 것 같은데요.
OTL
테스트 결과입니다.
[root@localhost CodeTest]# uname -a Linux localhost 2.6.15-UP #9 PREEMPT Tue Oct 17 10:01:52 KST 2006 i686 i686 i386 [root@localhost CodeTest]# [root@localhost CodeTest]# rpm -q libpcap libpcap-0.7.2-7.1 [root@localhost CodeTest]# [root@localhost CodeTest]# cat opendev.c #include <pcap.h> int main() { pcap_t *dev; char errbuf[1000]; while(1) { dev = pcap_open_live("eth0", BUFSIZ ,1,1000, errbuf); pcap_close(dev); } return 0; } [root@localhost CodeTest]# while [ 1 ] ; do ps -aux | grep opendev | grep -v gre USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 8790 0.0 0.0 1704 376 pts/1 R 18:06 0:00 ./opendev root 8790 0.0 0.0 1704 380 pts/1 D 18:06 0:00 ./opendev root 8790 0.0 0.0 1704 384 pts/1 R 18:06 0:00 ./opendev root 8790 0.0 0.0 1704 384 pts/1 R 18:06 0:00 ./opendev root 8790 0.0 0.0 1704 388 pts/1 R 18:06 0:00 ./opendev root 8790 0.0 0.0 1704 392 pts/1 D 18:06 0:00 ./opendev root 8790 0.0 0.0 1704 396 pts/1 R 18:06 0:00 ./opendev root 8790 0.0 0.0 1704 396 pts/1 R 18:06 0:00 ./opendev root 8790 0.0 0.0 1704 400 pts/1 R 18:06 0:00 ./opendev root 8790 0.0 0.0 1704 404 pts/1 D 18:06 0:00 ./opendev root 8790 0.0 0.0 1704 404 pts/1 R 18:05 0:00 ./opendev root 8790 0.0 0.0 1704 408 pts/1 R 18:05 0:00 ./opendev root 8790 0.0 0.0 1704 412 pts/1 R 18:05 0:00 ./opendev root 8790 0.0 0.0 1704 416 pts/1 D 18:06 0:00 ./opendev root 8790 0.0 0.0 1704 416 pts/1 R 18:06 0:00 ./opendev root 8790 0.0 0.0 1704 420 pts/1 R 18:06 0:00 ./opendev root 8790 0.0 0.0 1704 424 pts/1 R 18:06 0:00 ./opendev root 8790 0.0 0.0 1704 428 pts/1 D 18:06 0:00 ./opendev root 8790 0.0 0.0 1704 428 pts/1 R 18:06 0:00 ./opendev root 8790 0.0 0.0 1704 432 pts/1 R 18:06 0:00 ./opendev root 8790 0.0 0.0 1704 436 pts/1 R 18:06 0:00 ./opendev root 8790 0.0 0.0 1704 440 pts/1 D 18:06 0:00 ./opendev root 8790 0.0 0.0 1704 440 pts/1 R 18:06 0:00 ./opendev root 8790 0.0 0.0 1704 444 pts/1 R 18:06 0:00 ./opendev root 8790 0.0 0.0 1704 448 pts/1 R 18:06 0:00 ./opendev root 8790 0.0 0.0 1704 452 pts/1 D 18:06 0:00 ./opendev root 8790 0.0 0.0 1704 452 pts/1 R 18:06 0:00 ./opendev저도 valgrind 를 이용해서 확인 해봤는데 깨끗했습니다.
그런데 위와 같은 명령을 실행하면 보시다시피 RSS(프로세스가 사용중인 실제 메모리 크기)
와 위에서는 안나왔지만 VSZ(가상 메모리양) 도 계속 증가됩니다..
왜 그런지 조언좀 부탁드리겠습니다..
http://www.zeroc.com/faq/iceM
http://www.zeroc.com/faq/iceMemoryUsage.html
제가 본 글 중에 가장 친절한 글인 것 같습니다.
http://virtualthreads.blogspot.com/2006/02/understanding-memory-usage-on-linux.html
도 읽어보시고요.
OTL
해결했습니다. 문제는 버전이였던거 같습니다
libpcap 버전을 0.9.8로 변경한 후에
실행하니 RSS와 VSZ가 늘어나지 않는군요.
위에 답변 주신분 감사합니다.
댓글 달기