in reply to Re^3: XML::Twig loves to eat my memory
in thread XML::Twig loves to eat my memory

However it still does not release memory when I try to $t->dispose().

How did you ascertain that?

Replies are listed 'Best First'.
Re^5: XML::Twig loves to eat my memory
by carcus88 (Acolyte) on Jul 23, 2010 at 04:45 UTC
    By watching the size of the process. It does not decrease after dispose is called. Unless I misunderstand the purpose of dispose I would expect memory to be released and the size of the process to go down.

      Unless I misunderstand the purpose of dispose I would expect memory to be released...

      Yes. (Actually, it sounds like it's doesn't do anything on a system will a properly installed Scalar::Util because it's done automatically then.)

      ...and the size of the process to go down.

      That does not follow. Freeing memory does not necessarily make the process size go down. It's only guaranteed to go back to Perl's free memory pool. In some circumstances on some OSes, the process size can go down, but there's no guarantee of it.

      For example, you could observe the following:

      my $x; $x .= "a" for 1..1024; # Process size goes up undef $x; # Process size doesn't go down $x .= "a" for 1..1024; # Process size doesn't go up

      undef $x; freed the string's buffer — observable with Devel::Peek — but it might not release it to the OS. However, Perl will now be able to reuse that memory to rebuild $x.

      To see if the memory is freed, what you need to do is use some memory and see if the process goes up. Do as almut suggested and call process twice in a row, checking the process size after each call.

        How can I tell if Scalar::Util is properly installed. Here is the perl I'm using and a check for the Scalar::Util module.
        C:\Users\mmitchell>perl -v
        
        This is perl, v5.10.0 built for MSWin32-x86-multi-thread
        (with 9 registered patches, see perl -V for more detail)
        
        Copyright 1987-2007, Larry Wall
        
        Binary build 1005 290470 provided by ActiveState http://www.ActiveState.com
        Built May 24 2009 12:17:36
        
        Perl may be copied only under the terms of either the Artistic License or the
        GNU General Public License, which may be found in the Perl 5 source kit.
        
        Complete documentation for Perl, including FAQ lists, should be found on
        this system using "man perl" or "perldoc perl".  If you have access to the
        Internet, point your browser at http://www.perl.org/, the Perl Home Page.
        
        
        C:\Users\mmitchell>perl -e "use Scalar::Util"
        
        C:\Users\mmitchell>
        
        I did as almut suggested and ran the process() function twice.
        Size after first process() 
        167,472 MB
        Size after second process()
        316,212 MB
        
        It seems memory is not "freed" or its just not available for re-use for some reason.