saahbs has asked for the wisdom of the Perl Monks concerning the following question:

The code below appears to leak memory.

The leak rate is high when connections are refused. (tested using perl v5.8.8 as shipped in GNU/Debian Linux Etch with 2.6.22 kernel) Thanks, Mike

#!/usr/bin/perl use warnings; use strict; use Socket qw(:DEFAULT IPPROTO_TCP TCP_NODELAY); while ( 1 ){ socket(Client, PF_INET, SOCK_STREAM, getprotobyname("tcp")) or die " +ERROR: socket() failed $!\n"; my $remoteAddr = sockaddr_in(1220, inet_aton("localhost")) or die "E +RROR: sockaddr_in() failed $!\n"; connect(Client, $remoteAddr) or print "connect() status: $!\n"; close(Client) or print "DEBUG: close() status: $!\n"; }

Replies are listed 'Best First'.
Re: socket leaks memory
by Joost (Canon) on Oct 02, 2007 at 18:47 UTC
    Tried with the bundled perl5.8.8 on my debian system ( perl v5.8.8 built for i486-linux-gnu-thread-multi / Compiled at Dec 6 2006 23:05:53, my system's a mix of stable, testing and unstable ) and custom perl5.8.8, perl5.8.6 and perl5.8.5 on the same system.

    None show any problems.

    perl5.00504 runs if I remove "use warnings" and the constants you're explicitly importing (but not using!). Also no memory problems.

    update: the libc used by my main perl is 2.6.1, kernel version 2.6.20

Re: socket leaks memory
by kyle (Abbot) on Oct 02, 2007 at 18:07 UTC

    I ran this on a Linux with Perl 5.8.8 for 90 seconds (connections refused just as fast as it could), and it was not growing.

    What tool are you using to detect the memory leak? I was watching it with "top". Sometimes it would gain a little bit and then drop back to where it was.

      I just observe resident memory usage with top or ps. I tried it on another machine with exactly the same 5.8.8 build and it was not leaking. I wonder if it is a kernel/libc6 issue (the leaker was libc-2.6.1 with 2.6.22 kernel, and the non-leaker was libc-2.3.6 with 2.6.18 kernel). Another leaker was libc-2.3.2 with 2.6.8 kernel and perl 5.8.4.
Re: socket leaks memory
by saahbs (Initiate) on Oct 02, 2007 at 17:40 UTC
    Can anybody confirm this behavior? I observed it with both 5.8.4 and 5.8.8 (haven't tried other releases). Thanks, Mike
Re: socket leaks memory
by saahbs (Initiate) on Oct 02, 2007 at 19:04 UTC
    I tried C version of the code on machine which was leaking using perl (2.6.1 libc, 2.6.22 kernel, 5.8.8 perl). The C version did not leak, so it is not directly kernel/libc leak.