[완료]pcap 함수 메모리 누수현상?? (코드 10줄도 안됩니다)

ecstasy5001의 이미지

#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;

코드는 위와 같습니다.

실행을 하고 확인해본결과 메모리 누수현상이 있습니다.

왜 위 코드가 메모리 누수현상이 나는지 조언좀 부탁드리겠습니다.

bushi의 이미지

[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

ecstasy5001의 이미지

[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(가상 메모리양) 도 계속 증가됩니다..

왜 그런지 조언좀 부탁드리겠습니다..

bushi의 이미지

http://www.zeroc.com/faq/iceMemoryUsage.html
제가 본 글 중에 가장 친절한 글인 것 같습니다.

http://virtualthreads.blogspot.com/2006/02/understanding-memory-usage-on-linux.html
도 읽어보시고요.

OTL

ecstasy5001의 이미지

libpcap 버전을 0.9.8로 변경한 후에

실행하니 RSS와 VSZ가 늘어나지 않는군요.

위에 답변 주신분 감사합니다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.