ISAI student has asked for the
wisdom of the Perl Monks concerning the following question:

I have a general question, I will pose the scenario, and then the question:
If I create an array X, I take up memory. I am to erase it (by setting it's $# to -1), and then I will create a new array, named Y, which is slightly smaller than X. Now the question: Will Perl use the memory freed from array X in order to store array Y, or allocate s different portion of memory for array Y?.
basically what I am asking if there is any reason, from memory usage point of view, to use $#=-1?
Thanks,
-ISAI student

$#a=-1 will free up all the elements of @a, allowing their memory to be reused. In addition, undef @a frees up the array's AvARRAY structure, which uses typically (4 x maximum array index ever used) bytes. Neither will free up the actual array itself; you need to ensure the variable goes out of scope for that.

Your snippet doesn't prove anything. It doesn't say anything about the elements of @x and @y, just @x and @y themselves. Perl arrays are objects of sorts with pointers to structures containing the actual elements.

afaik, the memory that you'll free if a var is going out of scope is never released back to the OS, so perl keeps the memory allocated for future use within the script.

In other words, the memory is free to reuse by the same perl process, but not for other processes on your system.

I've tested this using these 3 simple scripts, and you can see doing a ps -o vsz,command | grep perl (or similar type of command, adapt to your OS, in my case it's FreeBSD), that in the first 2 cases the memory usage by the relevant perl processes is the same: