Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

Probably for the same reason that it takes a little more memory to create an identical array the second time than it did the first:

C:\test>p1 [0] Perl> sub mem { my $mem = `tasklist /nh /fi "PID eq $$"`; $mem =~ tr[ \t\n][ ]s; return "$mem : $_[0]"; };; print mem 1;; perl.exe 291940 Console 1 11,292 K : 1 $a[ $_ ] = $_ for 1 .. 1e6;; print mem 2;; perl.exe 291940 Console 1 43,680 K : 2 undef @a;; print mem 3;; perl.exe 291940 Console 1 35,464 K : 3 $a[ $_ ] = $_ for 1 .. 1e6;; print mem 4;; perl.exe 291940 Console 1 44,848 K : 4

When building an array piecemeal this way, the base AV starts with 8 elements, therefore requires 64 bytes contiguous memory be allocated. When you add the 9th element to it, the AV has to be reallocate to accommodate that new element and so a new chunk of memory double the size (128 bytes) is allocated; the existing 8 elements are copied across and the new 9th element is added (leaving space for 7 more new elements). Then and the first AV is freed back to the memory pool.

Then when you go to add the 17th element; the size is doubled again (256 byte AV is allocated) and the 128 are freed back to the pool. This process of doubling continues, until the size of the size of the AV required gets bigger than the pool limit, and then rather than allocating the newly doubled AV from the pool; it is allocated from the OS.

During the process of creating the array the first time, the pool is expanded -- by requesting more memory from the OS -- to accommodate the AVs when they are less than the size where they get allocated directly from the OS. And when they are done with, those allocations are returned to the pool and will be reused for other allocations including the many 24-bytes SVs. by the time the first array is freed, the once contiguous chunks have been re-allocated to smaller subdivisions.

Hence, when the array is built the second time, the continuous chunks originally allocated from the OS to accommodate the intermediate AVs are no longer available as contiguous chunks, so as the array is built the second time, it is necessary to go back to the OS for new contiguous allocations, despite that the final array will be the same size as the first.

Hashes go through similar processes as they expand.

As XML::Simple builds nested structures of hashes and arrays, it has similar requirements for contiguous chunks of memory, that may need to be reallocated when re-building the same data-structure a second time.

(Note for the pedants: The above may not be a totally accurate description of the process; but it is sufficient to explain the point I am trying to convey.)


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

RIP Neil Armstrong


In reply to Re^3: How can I free the memory of a XML::Simple-Object by BrowserUk
in thread How can I free the memory of a XML::Simple-Object by Bauldric

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others exploiting the Monastery: (4)
    As of 2014-08-31 04:37 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best computer themed movie is:











      Results (294 votes), past polls