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