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).
|
|---|