$a=[@$a] can be used to shrink the array. (Well, create a new one the minimal size.)

$ perl -MDevel::Peek -e'push @$a, $_ for qw( a b c d e ); Dump($a,1); +$a=[@$a]; Dump($a,1);' SV = IV(0x817bc44) at 0x817bc48 REFCNT = 1 FLAGS = (ROK) RV = 0x816c158 SV = PVAV(0x816d1c8) at 0x816c158 REFCNT = 1 FLAGS = () ARRAY = 0x8177928 FILL = 4 <----- 5 visible elements MAX = 11 <----- 12 elements allocated ARYLEN = 0x0 FLAGS = (REAL) SV = IV(0x817bc44) at 0x817bc48 REFCNT = 1 FLAGS = (ROK) RV = 0x816cea8 SV = PVAV(0x816d1dc) at 0x816cea8 REFCNT = 1 FLAGS = () ARRAY = 0x8183cb8 FILL = 4 <----- 5 visible elements MAX = 4 <----- 5 elements allocated ARYLEN = 0x0 FLAGS = (REAL)

But you might only end up fragmenting your memory, leaving less usable free memory behind.


In reply to Re: array overhead by ikegami
in thread array overhead 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.