Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^2: Shrunk array takes more memory than original

by kroach (Pilgrim)
on Nov 10, 2015 at 12:45 UTC ( [id://1147353]=note: print w/replies, xml ) Need Help??


in reply to Re: Shrunk array takes more memory than original
in thread Shrunk array takes more memory than original

I do not mean to shrink the memory, I would like it to stay the same instead of growing.

I'm not adding anything new to the array and I don't get why it gets larger from having half of its elements removed.

  • Comment on Re^2: Shrunk array takes more memory than original

Replies are listed 'Best First'.
Re^3: Shrunk array takes more memory than original
by BrowserUk (Patriarch) on Nov 10, 2015 at 13:06 UTC
    why it gets larger from having half of its elements removed.

    Because using $#array causes some magic to be attached to the array.

    You don't even have to assign to $#array for this to happen, only reference it:

    @b = 1 .. 10; print size \@b;; 256 print $#b; print size \@b;; 9 440

    And yes, 184 bytes does seem excessive. You'd think that the magic could be stored once and simply referenced for each array using an 4/8-byte pointer.


    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.
      Because using $#array causes some magic to be attached to the array.
      Technically speaking, a temporary magic object is created that points to the array. When the object is assigned to, it truncates the array. This object is freed (well, returned to the pool anyway) at the end of the statement. The array itself never gains any magic.

      Dave.

        This object is freed (well, returned to the pool anyway) at the end of the statement.

        Thanks for the correction. But it does raise the question of what is in those 184 bytes that remain?


        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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2024-04-18 16:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found