I'm running your script and watching the memory usage (top), and it's minimal and also not growing, I don't think that memory is your problem. Instead, I think it's just that the code you've implemented is relatively slow, so you probably want to look into optimizing it. A couple of recommendations there: you could, for example

Alternatively, for the task you're doing I can recommend Math::Prime::Util, it has lots of really helpful mathematical routines and it can deal with huge numbers (you should install Math::Prime::Util::GMP for speed). However, if this is homework, that might be considered cheating ;-)

use Math::Prime::Util qw/factor/; use List::Util qw/max/; print max(factor(20)), "\n"; print max(factor(13195)), "\n"; print max(factor("600851475143")), "\n"; __END__ 5 29 6857

As for your original question, note that once you put things on disk, you'll have disk I/O as a potential bottleneck. But if you still want to look into that, there are a few Perl modules that make storing things on disk fairly transparent, such as AnyDBM_File for tying a hash to a DBM file, or Tie::File for tying an array to a flat text file. Also, this thread might be an interesting read: Is there any cache mechanism for running perl script, where the replies show how to cache data structures using Storable, JSON, or Path::Class (the latter only for simple arrays).


In reply to Re: Avoid keeping larger lists in Memory by haukex
in thread Avoid keeping larger lists in Memory by pr33

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.