Perl: How to free memory allocated for a scalar without access to the Perl variable?
<p>This question is related to an <a href="https://stackoverflow.com/a/39514537/2055163">answer</a> to a former <a href="https://stackoverflow.com/questions/39513307/perl-when-is-unneeded-memory-of-a-scalar-freed-without-going-out-of-scope">question about memory handling</a> by Perl. I've learned that one can free memory in Perl by explicitly using the <code>undef</code> function on an available scalar and using <code>Devel::Peek</code> or <code>Devel::Size</code> 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.</p>

<p>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?</p>

<p>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 <code>undef</code> on a scalar, but without the scalar, something more low level, like on those "things" output of <code>Devel::Peek</code>.</p>

<p>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:</p>

<blockquote>
  <p>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".</p>
</blockquote>

<p><a href="https://perl.apache.org/docs/2.0/user/intro/overview.html#Threads_Support" rel="nofollow noreferrer">https://perl.apache.org/docs/2.0/user/intro/overview.html#Threads_Support</a></p>

<p>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 <code>undef</code> function on some low level Perl struct in Perl. Might simply not be possible without any XS or such...</p>