Subject: | Memory leak in Memoize.pm's unmemoize() |
There's a memory leak in Memoize.pm. If you frequently memoize then
unmemoize methods, and your script is long running, this will be a
problem. This may not be a common scenario, but it's the one I am
in. My exact scenario is:
* I have a set of database access libraries.
* I'm running under mod_perl.
* Some of the methods are like ->get_employee_info($id).
* For each page hit, I create a new instance of object.
* For each page hit, I don't care if employee_info changes
while that process is happening, so I want to memoize it,
because get_employee_info may be called frequently with
the same $id on each page load.
* However, after the one page is served, my objects go out
of scope, and I want any associated cache on the memoized
versions to be cleared completely.
In addition, how I'm setting up the memoizing is via AUTOLOAD, so
on every initialization, they're not all made. I also install them
to a different name (mem_$methodname). A DESTROY wrapper is installed
to unmemoize any that were memoized when the object gets destroyed.
Using the stock Memoize.pm, the unmemoize leaves behind entries in
the %memotable and %revmemotable. It's doing:
undef $memotable{$revmemotable{$cref}};
undef $revmemotable{$cref};
But it should (also) be doing:
delete $memotable{$revmemotable{$cref}};
delete $revmemotable{$cref};
Those above two lines are the entirity of the patch.
Included in the this test bundle are two copies of Memoize.pm:
orig/Memoize.pm
patched/Memoize.pm
Both have been modified from stock, adding the following methods:
_peek_at_memotable
_peek_at_revmemotable
Which simple return the internal %memotable and %revmemotable
respectively (so the test script can examine what's in there).
I've included a very simple test script that demonstrates the
problem. If _peek_at_memotable and _peek_at_revmemotable were
also added to the official Memoize.pm, then this could be
modified to be included as an actual test script included in
the distribution.
To see results using orig Memoize code:
PERL5LIB=./orig/ perl test.pl
To see results using patched Memoize code:
PERL5LIB=./patched/ perl test.pl
NOTE: the test script will not work with the stock Memoize.pm as
distributed from CPAN. The one in ./orig/Memoize.pm has two
additional functions (_peek_at_memotable and _peek_at_revmemotable)
to allow some additional introspection into the module. Without
modifying Memoize.pm with these (or similar), there's no good way
to demonstrate this bug.
Subject: | bugreport.tar.gz |
Message body not shown because it is not plain text.