When running on a Sun (Unix) workstation, the following code takes about 500MB of RAM!
foreach $x (0..4000) { foreach $y (0..4000) { $matrix[$x][$y] = 1.0; } }
This seems like an insanely large amount. :)

Here's why I think that ... A 4001x4001 array has approximately 16,000,000 elements. If each element is a float (which is the case here) then if one assumes a 4 byte float (which is the case on my machine), you would need 64,000,000 bytes to hold them in contiguous memory. That's 64MB (right? I'm not making some silly math mistake am I??). So even if somehow each float takes 8 bytes, that's still just 128MB.

Perl uses 500MB for this. Now, I can accept the idea that there is some overhead involved, but not hundreds of MB.

Why does this take up so much memory? Equally important, how can I do this and use less memory?

Thanks!

In reply to why does this array take up so much memory? by Anonymous Monk

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.