Why is Perl reallocating memory after this template?

Memory addresses for anonymous arrays are naturally reused perl. As this example shows, they cycle between two addresses for empty arrays:

$ perl -E "say [] for (1..6)"
ARRAY(0x37b23c)
ARRAY(0x37b28c)
ARRAY(0x37b23c)
ARRAY(0x37b28c)
ARRAY(0x37b23c)
ARRAY(0x37b28c)

I came up with some theories about why he couldn't reallocate memory right away when I discovered that the cycle is not always long. Cycles of the following examples: 3 and 4.

$ perl -E "say [0] for (1..6)"
ARRAY(0x39b23c)
ARRAY(0x39b2ac)
ARRAY(0x39b28c)
ARRAY(0x39b23c)
ARRAY(0x39b2ac)
ARRAY(0x39b28c)

$ perl -E "say [0,0] for (1..6)"
ARRAY(0x64b23c)
ARRAY(0x64b2cc)
ARRAY(0x64b2ac)
ARRAY(0x64b28c)
ARRAY(0x64b23c)
ARRAY(0x64b2cc)

What causes this memory management feature?

+5
source share
2 answers

When SVs are freed, they are actually placed in the "free" pool. Perhaps the order in which they enter the pool affects the order in which they exit.

+2
source

", ". " ". , SVs , , , (, , ).

, - " + 2". , SV , arrayref $_?

+1

All Articles