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

hi all,
I am new to Perl and I wrote some code that bascally reads a file that contains information about some host, its ip and a port to which it should connect. A telnet object is first created. the program loops through the file and for each entry tries to connect to the port on a specific machine and expcects specific output. It does so by calling the telnet member function "open".
If the output received is not the one expected, an email message is generated to specific people. At first, I used to call Net::Telnet->new for each entry in the table. That made the program consume about 110M of RAM after running for 4 or 5 hours. I then made a change by creating the telnet object outside of the for loop and only call the "open" function inside.
The program still consumes about 26M of RAM and it's increasing as time passes by. Can anybody enlighten me on how Perl deals with memory management and if there is a better way to implement the task.
Thank you for all the help.
Saad

Replies are listed 'Best First'.
Re: Memory leaks
by Fastolfe (Vicar) on Nov 09, 2000 at 23:32 UTC
    Perl frees up variables when they fall out of scope or are undefined. Be certain you are using strict and the perl -w flag to catch warnings, and that all of your variables are scoped correctly where you're using them. If you're still having problems, consider undef'ing large variables/objects when you're done with them, assuming they won't fall out of scope shortly thereafter and be destroyed automatically.
Re: Memory leaks
by elwarren (Priest) on Nov 10, 2000 at 01:16 UTC
    I have a script that opens and closes a telnet session to three different hosts every five minutes and logs the output. I've run this script on my NT workstation for days on end without problems. Could you please post your code or maybe the relevant snippet so we could further help?
Re: Memory leaks
by saritto (Initiate) on Nov 14, 2000 at 01:12 UTC
    Here is a snippet of the code that I am using:
    $t = new Net::Telnet( Timeout => $conn_timeout, Errmode =>"return"); $firsttime = 0; while (1) { chomp( @target_list = <TARGET_FILE> ); close TARGET_FILE; foreach $target ( @target_list ) { ( $target_ip, $target_name, $target_port ) = ( split /\s+/, $t +arget ); my ($sec,$min,$hour,$mday,$mon,$year) = gmtime(time()); $mon += 1; $year += 1900; $time = ".$mon.$mday.$year.$hour:$min:$sec"; $filename = ($dumpfilename .$target_name).$time; $t->dump_log($filename); $ok = $t->open( Host => $target_ip, Port => $target_port); if($ok) { if(!($t->waitfor(String=>$wait_string, Timeout => $prompt_timeout))) { if (($var=( $actualtime = time) - $firsttime) > $al +arm_interval) { sendalarm( "agent process $target_name is no +t running on $target_port", "Agent-Process $target_name"); $alarm_flag=1; } # close tcp connection $t->close(); } else { # we have found a valid prompt $t->print("bye"); $t->close(); # remove dumpfile unlink($filename); } } else { if (($var = ( $actualtime = time) - $firsttime) > $ +alarm_interval) { sendalarm( "agent server $target_name +is not running on $target_port", "Agent-Server $target_name"); $alarm_flag=1; } unlink($filename); } }
    thank you for all the help
      Is there a Profiling Tool available that can identify where in the PERL program memory leaks occur. I have used tools like that with Java programs .. but do not know of any such tool for PERL applications. Please let me know.