So I'm working with a kmer project and I needed to generate a 4**21 by 197 matrix to record the information I need for each kmer since I'm doing 21mer right now. However, it shows out of memory when I try to generate the matrix. Is there any way I can get around with it?

Here is the script I wrote:

#!/usr/bin/perl use strict; use warnings; #initialize matrix my @matrix; my $i; #column my $j; #roll my @sum; for ($i=0; $i<197; $i++){ for ($j=0; $j<4**21; $j++){ $matrix[$i][$j]=0; if($i==0) { $sum[$j]=0; } } } #inport file names my $fn = $ARGV[0]; chomp $fn; unless (open(FILE, $fn)){ print "Can't open file \"$fn\"\n\n"; exit; } chomp(my @line = <FILE>); my $n; for ($n=0; $n<scalar @line; $n++){ #open each kmer file unless (open(KMER, $line[$n])){ print "Can't open file \"$line[$n]\"\n\n"; exit; } my @kmers = <KMER>; my @all_kmer; my $kmer; foreach $kmer (@kmers){ my @split = split(/\s+/,$kmer); #obtain kmer reference + number $matrix[$n][$split[0]] = 1; #print OUT "$matrix[$n][$split[0]]\t"; $sum[$split[0]]=$sum[$split[0]]+1; } close $line[$n]; } close $fn; #creat outfile my $outfile = $ARGV[1]; unless(open OUT, '>' .$outfile){ die "\nUnable to create $outfile\n"; } #sum up for ($j=0; $j<4**21; $j++){ print OUT "$sum[$j]\n"; }

In reply to Out of Memory when generating large matrix by cathyyihao

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.