Well - memory leaks are are often pesky little beasties to catch!

I would guess that the most likely candidate is the DBH - but you dont show exactly how you are opening the DBH. Make sure that PrintError and RaiseError are on. If necessary, put your DBH into trace mode.

Also, you say that it runs fine, but gradually consumes memory until the machine expires - yet your example does not really show any sort of long-running loop? Perhaps the issue is actually in the loop construction?

Self referencing and circular object references can also cause issues - but OOh - your example does not indicate that sort of style.

In my experience, using undef to force vars out of scope is usually a mistake. There are some malloc cleanup bugs in Perls built with libc2.2.5 that can cause garbage collection to run for long periods.

and because I couldn't resist, here is Another Way - there are no doubt even More Ways to remove intermediate vars. :)

#!/usr/local/bin/perl -w use strict; use DBI; my @ids = @ARGV; die "no ids specified" unless scalar @ids; open (OUTPUT, ">>./output.txt") or die "Cannot open the output file: $ +!"; my $dbh; my $sql = "select long_string from mytable where id=?"; { my ( $dsn, $user, $password ) = ( "mydsn", "myuser", "mypassword", ); $dbh = DBI->connect( $dsn, $user, $password, {RaiseError => 1, AutoCommit => 0, PrintError => 1} ); die "DB Connect failed: DSN='$dsn' User='$user' Error='$DBI::errstr' +" unless $dbh; } my $sth = $dbh->prepare($sql); my %count; my @zeros = (0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0); foreach my $id (@ids) { my $long_string = uc($dbh->selectrow_array($sth,{},$id)); @count{'A'..'Z'} = @zeros; for my $i ( 0 .. length($long_string)-1 ) { $count{substr($long_string,$i,1)}++; } print OUTPUT join("\t", ($id, @count{'A'..'Z'})), "\n"; } close (OUTPUT) or die "Cannot close the output file: $!"; $dbh->disconnect();

Oh - and did I mention perl -w and strict? Essentials! As an aside, your example only counts uppercase letters, and no other chars - I guess that you are counting on the quality of the input data.

Regards

Jeff


In reply to Re: Where are those memory leaks? by jaa
in thread Where are those memory leaks? by ezekiel

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.