Sure. I ran this with about 1000 devices and the threaded one goes to 1.4GB. Nonthreaded stays rock steady about 32MB.

######routeleaker2.pl########

#!/usr/bin/perl require Net::SNMP; #use Data::Dump "pp"; $| = 1; $howmany = $ARGV[0] or die; $ipCidrRouteProto = "1.3.6.1.2.1.4.24.4.1.7"; $ipCidrRouteIfIndex = "1.3.6.1.2.1.4.24.4.1.5"; $ipCidrRouteType = "1.3.6.1.2.1.4.24.4.1.6"; $ipRouteMask = "1.3.6.1.2.1.4.21.1.11"; $ipRouteIfIndex = "1.3.6.1.2.1.4.21.1.2"; $ipRouteType = "1.3.6.1.2.1.4.21.1.8"; $ipRouteNextHop = "1.3.6.1.2.1.4.21.1.7"; $ipRouteProto = "1.3.6.1.2.1.4.21.1.9"; open IPFILE, "<allips.txt" or die; for $i (1 .. $howmany) { my ($host, $commstr) = split / /, <IPFILE>; chomp $commstr; print "$host, $commstr "; last unless $commstr; &poll($host, $commstr); if ($i % 5 == 0) { my $memusage = (`ps -p $$ -o vsz `)[1]; chomp $memusage; print " __${memusage}__"; } print "\n"; } print "\n"; sub poll { my ($devarg, $commstr) = @_ or die; my ($sessionO, $error) = Net::SNMP->session( -hostname => $devarg, -community => $commstr, -port => 161, -t +ranslate => 1, -version => SNMPv1, -timeout => 30, -retries => 2, -domain => "udp", -debug => +0x00 ); if (!defined($sessionO)) { print STDERR "Error making object($devarg $commstr): ", $error +, " \n"; print "Error making object($devarg $commstr): ", $error, " \n" +; die; } $sessionO->translate([-timeticks => 0]); my $session = \$sessionO; my $result6; $result6 = $$session->get_entries(-columns => [$ipCidrRouteIfIndex +, $ipCidrRouteProto, $ipCidrRouteType ]); $result6 = $$session->get_entries(-columns => [$ipRouteNextHop, $i +pRouteIfIndex, $ipRouteMask, $ipRouteProto, $ipRouteType]) unless %$r +esult6; if (!defined($result6)) { printf(" %s", $$session->error); } print "'", scalar %$result6, "' ", length join( "", %$result6); # print "\npre\n", pp($session), "\n", pp($sessionO), "\n"; $$session->close; # print "\npost\n", pp($session), "\n", pp($sessionO), "\n"; }
################routeleaker3.pl##############

#!/usr/bin/perl use threads ('stack_size' => 131072); use threads::shared; use Thread::Queue; use Thread::Semaphore; require Net::SNMP; #use Data::Dump "pp"; $| = 1; $howmany = $ARGV[0] or die; $ipCidrRouteProto = "1.3.6.1.2.1.4.24.4.1.7"; $ipCidrRouteIfIndex = "1.3.6.1.2.1.4.24.4.1.5"; $ipCidrRouteType = "1.3.6.1.2.1.4.24.4.1.6"; $ipRouteMask = "1.3.6.1.2.1.4.21.1.11"; $ipRouteIfIndex = "1.3.6.1.2.1.4.21.1.2"; $ipRouteType = "1.3.6.1.2.1.4.21.1.8"; $ipRouteNextHop = "1.3.6.1.2.1.4.21.1.7"; $ipRouteProto = "1.3.6.1.2.1.4.21.1.9"; open IPFILE, "<allips.txt" or die; @ips = <IPFILE>; close IPFILE; $howmany = $#ips if $howmany > $#ips; $queue = new Thread::Queue; $queue->enqueue(@ips[0 .. $howmany]); undef @ips; share($on); share($count); share($printlock); $semaphore = new Thread::Semaphore(0); $count = 0; for $i (0 .. 99) { $threads[$i] = threads->create("entrypoint"); } print "\n"; $semaphore->up(100); while ($on) { if ($slept % 60 == 0) { my $memusage = (`ps -p $$ -o vsz `)[1]; chomp $memusage; lock $count; lock $printlock; print "\ncount: $count, ${memusage} KB \n\n"; } sleep 1; $slept++; } print "\n"; foreach $i (@threads) { $i->join; } print "done\n"; exit; sub entrypoint { {lock $on; $on++;} { lock $printlock; print "+"; } local $mytid = threads->self->tid(); $semaphore->down; my $string; while ($string = $queue->dequeue_nb) { my ($host, $commstr) = split / /, $string; chomp $commstr; last unless $commstr; &poll($host, $commstr); { lock $count; $count++; } } {lock $on; $on--;} } sub poll { my ($devarg, $commstr) = @_ or die; my ($sessionO, $error) = Net::SNMP->session( -hostname => $devarg, -community => $commstr, -port => 161, -t +ranslate => 1, -version => SNMPv1, -timeout => 30, -retries => 2, -domain => "udp", -debug => +0x00 ); if (!defined($sessionO)) { print "Error making object($devarg $commstr): ", $error, " \n" +; die; } $sessionO->translate([-timeticks => 0]); my $session = \$sessionO; my $result6; $result6 = $$session->get_entries(-columns => [$ipCidrRouteIfIndex +, $ipCidrRouteProto, $ipCidrRouteType ]); $result6 = $$session->get_entries(-columns => [$ipRouteNextHop, $i +pRouteIfIndex, $ipRouteMask, $ipRouteProto, $ipRouteType]) unless %$r +esult6; if (!defined($result6)) { lock $printlock; print "t$mytid $devarg 'no routing table' ", "\n"; $$session->close; return; } { lock $printlock; print "t$mytid $devarg '", scalar %$result6, "' ", length join( "" +, %$result6), "\n"; } # print "\npre\n", pp($session), "\n", pp($sessionO), "\n"; $$session->close; # print "\npost\n", pp($session), "\n", pp($sessionO), "\n"; }

In reply to Re^6: Massive Perl Memory Leak by wagnerc
in thread Massive Perl Memory Leak by wagnerc

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.