The only thing I can think of is that routines will never be freed even if $first and $second go out of scope. $first code refers to $second , $second code refers to $first . This is a circular data structure, and memory allocation in Perl cannot free it.
$ perl -wlE 'for (1..10_000) { my($first, $second); $first = sub {}; $second = sub {} } say "Done"; sleep 1000' $ perl -wlE 'for (1..10_000) { my($first, $second); $first = sub { $second->() }; $second = sub { $first->() } } say "Done"; sleep 1000'
The first Perl process uses 1912K after the loop, the second uses 10320K. The first will not grow no matter how many CVs are created, the second will be.
To get around this, you must break the circle by specifying $first or $second . This third one calls undef $first inside the loop; its memory does not grow.
$ perl -wlE 'for (1..100_000) { my($first, $second); $first = sub { $second->() }; $second = sub { $first->() }; undef $first; } say "Done"; sleep 1000'
source share