Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re: Shrunk array takes more memory than original

by BrowserUk (Patriarch)
on Nov 10, 2015 at 13:04 UTC ( #1147354=note: print w/replies, xml ) Need Help??

in reply to Shrunk array takes more memory than original

Using total_size() rather than size():

@a = 1 .. 10000;; print total_size \@a;; 320176 $#a = 5000;; print total_size \@a;; 200376

As you can see, the total size occupied by the array has shrunk by 30%.

When you initialise the array, you cause perl to allocate 10,000 * 8 (64-bit) pointers as a continuous piece of memory for the array, plus 10,000 * 24 bytes for the scalars = 320,000 bytes (plus a little extra for the array mechanics.

When you assign to $#array, the size of the base array does not get reallocated (hence the size() doesn't change), but half of the scalars are reclaimed. So now you have 10,000 * 8 plus 5,000 * 24 = 200,000 (plus the little extra).

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". I knew I was on the right track :)
In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1147354]
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (8)
As of 2023-12-11 22:20 GMT
Find Nodes?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?

    Results (41 votes). Check out past polls.