Assigning a number to a scalar doesn't free the string buffer, it just marks it as unused. You "solution" can actually increase the memory used.
$ perl -MDevel::Size=total_size -E'
$a[5]="5678";
say total_size($a[5]);
$x=int($a[5]);
say total_size($a[5]);
'
40
44
undef $s (but not $s = undef;) will free the string buffer.
$ perl -MDevel::Size=total_size -E'
$a[5]="5678";
say total_size($a[5]);
$t=int($a[5]);
undef $a[5];
$a[5]=$t;
say total_size($a[5]);
'
40
36
It doesn't downgrade the scalar from PVIV to IV, though. You'd need to start with a new scalar to do that.
$ perl -MDevel::Peek -MDevel::Size=total_size -E'
$a[5]="5678";
say total_size($a[5]);
$t=int($a[5]);
delete $a[5];
$a[5]=$t;
say total_size($a[5]);
'
40
16
|