Thorsten Schöning
Thorsten Schöning

Reputation: 3727

Perl: How to free memory allocated for a scalar without access to the Perl variable?

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...

Upvotes: 8

Views: 1640

Answers (3)

ikegami
ikegami

Reputation: 385897

is it possible to debug things like allocated memory outside the scope of variables

There really isn't any such memory. Any memory allocated outside of variables is surely needed. As you yourself point out, it's the memory allocated for variables that make up most "wasted" space.

but no hint yet how one could call something like the undef function on some low level Perl struct in Perl.

It's because there are no such structs.

Just like calling undef on a scalar, but without the scalar, something more low level, like on those "things" output of Devel::Peek.

Devel::Peek's only function, Dump, outputs things in variables. Like you've said, undef is what you'd want to clear these.


From the above, it's obvious you want to know how to free the memory associated with the variables in subs.

You also overlooked the fact that many operators have an associated variable (called "target") in which they return their result.

Approach 1

A simple way to clear all those variables would be to selectively clear the symbol table (%::). This would effectively "unload" every module. Be sure not clear core components (perl -E'say for sort keys %::'). And don't forget to clear %INC so the modules can be reloaded.

If clearing the symbol table is the approach you want to take, it might be less risky and time-consuming to take a snapshot of %:: early on, and restore that snapshot when it's time to clear the symbol.

Approach 2

If you didn't want to reload the modules, you could take attempt to locate every sub, and undef their vars, then undef the vars of their ops.

A sub's vars exists within its pads. Conveniently, so do opcode targets. There's a pad for each level of recursion the sub has experienced.

Given a reference to a sub, you can find the variables in a sub's pads. You can refer to PadWalker for an example of how to do this. You can't actually use PadWalker since it only returns one variable per variable name, even if there are more than one (due to more than one variable being declared with the same name, or due to recursion).

Captured variables and our variables should be left untouched. It's possible to detect whether a pad entry is one of these. (Again, refer to PadWalker.)

(Obviously, you could also looking into freeing the sub's extra pads!)

How do you find all the subs? Well, navigating the symbol table will give you most of them. Finding anon ones will be trickier.

Approach 3

The most efficient approach is to simply terminate the mod_perl thread/process. A new clean one will automatically be spawned. It's also the simplest to implement, as it's simply a configuration change (setting MaxRequestsPerChild to 1).


Another form of wasted memory is a memory leak. That's another large question, so I'm not touching it.

Upvotes: 4

Eugen Konkov
Eugen Konkov

Reputation: 25153

To debug memory allocation you should recompile perl with -Accflags=-DPERL_MEM_LOG DOC

(see related question about how to recompile perl)

You maybe will be interested in MEMORY DEBUGGERS

To free perl scalar, just like when it leave her scope:

{ # Scope enter
     my $x; # Memory allocation
} # << HERE $x is freed

You should just reduce its variable REFCNT to zero by SvREFCNT_dec macro DOC

To free an SV that you've created, call SvREFCNT_dec(SV*) . Normally this call is not necessary (see Reference Counts and Mortality).

Here is pseudo code:

{ 
    $x;
    call_xs_sub( $x ); # << HERE $x is freed        
}

XS pseudo code:

call_xs_sub( SV *sv ) {
    ...
    SvREFCNT_dec( sv ); # <<HERE scalar is freed
    ...
}

To spy every memory allocation you should walk perl arenas.

At compile time you may view every place where variable is declared and accessed with help of B::Xref module

Or run perl with -Dm option (The perl should be compiled with corresponding options. See this topic):

perl -Dm script.pl

Upvotes: -1

Jiri Klouda
Jiri Klouda

Reputation: 1370

I think you are looking for this answer to a similar question. Everything you really need to know you can find in the internals to the Devel::MAT::* submodules. Namely the Devel::MAT::Dumper.xs, which has the structure of the heap for perl interpreter. The module is designed to dump the heap at signal and analyze it later, but I think you can turn it into a runtime check. If you need help on reading the XS, look here.

Upvotes: 1

Related Questions