Hello,

I have included the guts of a script I have written that basically counts the frequency of alphabetic characters in a series of strings. It runs fine except, it has a memory leak, gradually using up memory until the machine expires. I have tried undef-ing everything in sight but still the problem occurs. Either I have missed something, in which case I would be grateful if a fresh pair of eyes could point it out, or perhaps there is a leak in the DBI module? Thanks for your help.

open (OUTPUT, ">>./output.txt") or die "Cannot open the output file: $ +!"; foreach my $id (@ids) { my $sth = $dbh->prepare("select long_string from my_table where id +=?"); $sth->execute($id); my $long_string = $sth->fetchrow_array(); $sth->finish(); undef($sth); my %count; my @letters = split("", $long_string); foreach my $letter (@letters) { $count{$letter}++; } undef(@letters); undef($long_string); # Print out the counts for each string print OUTPUT "$id\t" . ($count{A} || 0) . "\t" . ($count{B} || 0) . "\t" . ($count{C} || 0) . "\t" . ($count{D} || 0) . "\t" . ($count{E} || 0) . "\t" . ($count{F} || 0) . "\t" . ($count{G} || 0) . "\t" . ($count{H} || 0) . "\t" . ($count{I} || 0) . "\t" . ($count{J} || 0) . "\t" . ($count{K} || 0) . "\t" . ($count{L} || 0) . "\t" . ($count{M} || 0) . "\t" . ($count{N} || 0) . "\t" . ($count{O} || 0) . "\t" . ($count{P} || 0) . "\t" . ($count{Q} || 0) . "\t" . ($count{R} || 0) . "\t" . ($count{S} || 0) . "\t" . ($count{T} || 0) . "\t" . ($count{U} || 0) . "\t" . ($count{V} || 0) . "\t" . ($count{W} || 0) . "\t" . ($count{X} || 0) . "\t" . ($count{Y} || 0) . "\t" . ($count{Z} || 0) . "\n"; undef(%count); } close (OUTPUT) or die "Cannot close the output file: $!";

In reply to 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.