I didn't got time to test it in another platform to see for memory
leaks, but anyway, I guess that if we can solve it that way, it's
better to do it that way, because the code will be more portable.
Also, from the "Programming Perl (3rd Ed.)" from Larry Wall:
"The existence of any reference prevents the referent from being
reclaimed by Perl, even though we want cache data to disappear as
soon as it's no longer needed" [...]
The problem, as far as I could test, is that with the first pass of
the loop, we create a new variable, let's say it $x, thus
allocating memory for it. Then, in the second step of the loop, we
create a new lexically-scoped variable, also called $x, giving
memory to it.
Note that we allocate a new variable, we don't reuse the same!!! I
think that it's true for the standard perl port on unix, also (not
100% sure, but almost).
When the variable $x goes out of scope, it's memory is deallocated,
but since perl sees a reference to a slice of memory and cannot
determine which symbol holds this reference (because the symbol was
overwritten in the symbol table because of the "my $x" in the 2nd,
3rd,.... steps of the loop), Perl cannot free the allocated memory
because it can be still in use.
Note that althought Perl will deallocate correctly used memory in
this situation, and that my notes affect only to a specific Perl
port, what's really sure in any port of perl (as far as I used,
Win32 with Cygwin, ActiveState's Win32, FreeBSD, Red Hat and
Solaris), declaring variables outside of the loop will be more
efficient because we will only need space for 1 variable, not for
N variables (where N is the total loop steps) during the process.
Monday, December 30, 2002, 8:49:33 PM, you wrote:
TB> On Mon, Dec 30, 2002 at 05:15:28PM +0100, Josep Ruano Bou wrote:
>> I'm saying that those constructions leaks...
>> With the original modules, it was consuming an additional 100KB of RAM
>> every time a new document was inserted, at the beginning, and at
>> the end it was consuming near 300KB of additional RAM per document
>> After I moved all the declarations inside loops to outside of the
>> loops, it was consuming between 20KB and 60KB while indexing each
>> document, freeing memory (I guess because of the garbage collector)
>> sometimes and giving a medium usage of 40KB of RAM per document
>> indexed at the end. Also, the usage of RAM per document was
>> constant during all the execution.
TB> This sounds like a bug your version of perl. Have you tried it with a
TB> different version?