This question is related to an answer to a former question about memory handling by Perl. I've learned that one can free memory in Perl by explicitly using the undef
function on an available scalar and using Devel::Peek
or Devel::Size
or such one can see how many memory is allocated for a scalar. In all those cases the scalars debugged are used within their scope.
But is it possible to debug things like allocated memory outside the scope of variables, just on the level of a Perl interpreter? Something like searching for all allocated memory for all "things" that are a scalar in the current interpreter and print their associated data, like current value or such?
And if that's the case, if one does already have that information, is one even able to free the known memory? Just like calling undef
on a scalar, but without the scalar, something more low level, like on those "things" output of Devel::Peek
.
What I'm thinking about is having a mod_perl cleanup handler executed after a request, scanning the current mod_perl interpreter for large chunks of data and freeing them manually. Simply because I decide that large blocks of allocated data are of no use anymore, even if Perl thinks otherwise:
Finally and perhaps the biggest win is memory re-use: as calls are made into Perl subroutines, memory allocations are made for variables when they are used for the first time. Subsequent use of variables may allocate more memory, e.g. if a scalar variable needs to hold a longer string than it did before, or an array has new elements added. As an optimization, Perl hangs onto these allocations, even though their values "go out of scope".
https://perl.apache.org/docs/2.0/user/intro/overview.html#Threads_Support
I could find a lot of monitoring and debugging packages around low level memory access, but no hint yet how one could call something like the undef
function on some low level Perl struct in Perl. Might simply not be possible without any XS or such...
undef
is indeed the right tool. The variables themselves are found in a function's pad. You can use PadWalker'speek_sub
to get references to the vars of a sub from a sub reference, and you can get references to the named subs from the namespace (%::
). – Appurtenantsplice(@a, 5)
from warning if@a
has fewer than 5 elements, a couple of other things I can't remember, inclding some doc fixes.) – Appurtenantcaller
can help? – Appurtenant