$ perl -MDevel::Size=total_size -E'
my $s = "x" x 100_000;
my $x = \$s;
my $y = \$s;
say total_size($x);
say total_size($y);
'
100048
100048
Does that mean that the size of $x
and $y
combined is 200KB? No. Same idea here.
It's not the size of the sub, but the size of the references, and everything it references, directly and indirectly.
$ perl -MDevel::Size=total_size -E'
sub f { } say total_size(\&f);
${"xxx"}=1; say total_size(\&f);
${"yyy"}=1; say total_size(\&f);
'
5847
5908
5969
As you can see, this is measuring more than just the sub. There appears to be a pointer to the sub's namespace.