Try it this way. Create a set number of sessions, then dispatch events (using snmp_dispatch_once()) until you get a reply. Then create another and dispatch events until you get another reply, and so on. Adjust the value of $MAXCONCURRENT to control your memory usage.

(Note:Untested code):

#!/usr/bin/perl -w use warnings; use strict; use Net::IP; use Net::SNMP qw( snmp_dispatch_once oid_lex_sort ); #use Smart::Comments '###'; my $startip = $ARGV[0] || die "Missing Starting IP"; my $endip = $ARGV[1] || die "Missing Ending IP"; my $community = $ARGV[2] || die "Missing community string"; my $ips = Net::IP->new( "$ARGV[ 0 ] - $ARGV[ 1 ]" ); my $MAXCONCURRENT = 50; my $running = 0; ### Updated do{} while (taken from docs) to while(){} per reply. while( ++$ips ) { my( $session, $error ) = Net::SNMP->session( -hostname => $ips->ip, -version => 'snmpv2c', -nonblocking => 1, -community => "$community", -timeout => 3, -retries => 1, ); if( defined( $session ) ) { my $serialno = '.1.3.6.1.3.83.1.1.4.0'; my $mac = '.1.3.6.1.2.1.2.2.1.6.2'; my @msoids = ( $mac, $serialno ); my $result = $session->get_request( -varbindlist => \@msoids, -callback => [ \&getms, $session, $ips->ip ] ); $running++; ## Count the sessions started }else{ warn "Session not defined for %s: %s\n", $ips->ip, $session->e +rror_status; }; ## start another unless we have the max running next unless $running > $MAXCONCURRENT; ## Dispatch events until we get a reply from one snmp_dispatch_once() while $running > $MAXCONCURRENT; } exit; sub getms { my $obj = shift; my $session = shift; my $hfcip = shift; $running--; ## One more done if (!defined($obj->var_bind_list)) { warn "$hfcip SNMP Error.", $obj->error_status,"\n"; return; } ## print values for the oids $session->close; }

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

In reply to Re^4: How to improve memory usage in this script?? by BrowserUk
in thread How to improve memory usage in this script?? by Anonymous Monk

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.