You can get around the memory allocation in the second loop by printing the lines just before the end of the outside loop, instead of storing them in memory, as

foreach $_ (@total) { my @line_elems; for($i = 0; $i < $sampleSize; $i++){ ... push @line_elems, $temp; } print (join "\t", @line_elems) ."\n"; }
instead of adding them to the ever-growing %RandHapl hash.

The first loop, however,still allocates allocates enough memory to hold all the tokens in the file in a hash. This will grow as the file size does, leading to out-of-memory errors.You could fix this by moving the contents of the second loop inside the 'elsif' block, since both loops iterate on $currentPop, and storing the tokens in a temporary array instead of in %haplotypes.

BTW, I notice that you declare the @haplotypes array at the top, but actually use the %haplotypes hash. Please consider using 'use strict;' and 'my' declarations, as they would prevent this kind of error.

Ron Steinke rsteinke@w-link.net

In reply to Re: memory leak by rsteinke
in thread memory leak by Juba

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.