Esteemed monks,

I always get stuck with memory leak problems when I try to use Perl from C (my very first post here was about this), but this time I can't believe that the problem is due to a bug in perl!

Here's the test code I'm using:
/* Call this file arrays.c, and compile with: gcc -o arrays arrays.c `perl -MExtUtils::Embed -e ccopts -e ldopts` In my system: perl -MExtUtils::Embed -e ccopts -fno-strict-aliasing -pipe -I/usr/local/include -D_LARGEFILE_SOU +RCE -D_FILE_OFFSET_BITS=64 -I/opt/perl/lib/5.8.8/i686-linux/CORE perl -MExtUtils::Embed -e ldopts -Wl,-E -L/usr/local/lib /opt/perl/lib/5.8.8/i686-linux/auto/Dyn +aLoader/DynaLoader.a -L/opt/perl/lib/5.8.8/i686-linux/CORE -lperl -ln +sl -ldl -lm -lcrypt -lutil -lc */ #include <EXTERN.h> #include <perl.h> #include <stdio.h> #include <sys/types.h> #include <unistd.h> #define ELEMENTS 100000 #define ROUNDS 1000 #define MILESTONE 100 static PerlInterpreter *my_perl; AV* create_av () { AV *av = newAV(); int i; for (i = 0; i < ELEMENTS; ++i) { av_push(av, newSViv(i)); } return av; } void print_memory (int milestone) { FILE *fh; int nread; const int bufsize = 4096; char buffer[bufsize]; printf("\n\n**** %d ****\n", milestone); sprintf(buffer, "/proc/%d/status", getpid()); fh = fopen(buffer, "r"); while ((nread = fread(buffer, 1, bufsize, fh)) > 0) { fwrite(buffer, 1, nread, stdout); } } void cycles () { int i; for (i = 1; i <= ROUNDS; i++) { AV *av = create_av(); if (i % MILESTONE == 0) { print_memory(i); } av_undef(av); } } int main (int argc, char *argv[], char *env[]) { static char *dummy_argv[] = {"","-e","0"}; static int dummy_argc = 3; PERL_SYS_INIT3(&dummy_argc, &dummy_argv, &env); my_perl = perl_alloc(); perl_construct(my_perl); PL_exit_flags |= PERL_EXIT_DESTRUCT_END; perl_parse(my_perl, NULL, dummy_argc, dummy_argv, (char **) NULL); cycles(); perl_destruct(my_perl); perl_free(my_perl); PERL_SYS_TERM(); return 0; }
I'm using this with perl 5.8.8 in Linux. The main() function initialises the Perl stuff. The main objective is doing some cycles(), in which I repeatedly create an array and populate it (create_av()) and then dispose it. At some given intervals I print the memory status (print_memory()). Here's what I got (after filtering out most lines):
**** 100 **** VmSize: 5528 kB **** 200 **** VmSize: 5536 kB **** 300 **** VmSize: 5544 kB **** 400 **** VmSize: 5816 kB **** 500 **** VmSize: 5824 kB **** 600 **** VmSize: 5832 kB **** 700 **** VmSize: 5840 kB **** 800 **** VmSize: 5848 kB **** 900 **** VmSize: 5860 kB **** 1000 **** VmSize: 5868 kB
I tried different values for the constants (ELEMENTS, ROUNDS and MILESTONE), but the trend remains: the more the cycles, the bigger the memory.

Where am I leaking it?

Update: it turned out that I had two leaks: I had to use SvREFCNT_dec(av) instead of av_undef(av) and.. to close the filehandle I used to read the memory usage. Thanks to dave_the_m for spotting them both (what are you waiting? Go vote him!).

Flavio
perl -ple'$_=reverse' <<<ti.xittelop@oivalf

Don't fool yourself.

In reply to Memory leak dealing with AVs by polettix

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.