EvanCarroll has asked for the wisdom of the Perl Monks concerning the following question:

Anyone good at trouble shooting memory leaks? I have no idea how to do this or why this quick test fails. AFAIK it didn't fail under AS 5.8 but under Strawberry 5.10 its memory consumption just grows. Any assistance would be appreciated, should I file a bug report, if so with who?
#!/usr/bin/perl -l use LWP; use Parallel::ForkManager; use feature ':5.10'; my $pm = Parallel::ForkManager->new(10); my $lwp = LWP::UserAgent->new; foreach my $test ( 0 .. 500 ) { state $count = 0; $count++; $pm->start and next; my $resp = $lwp->get( "http://google.com/search?q=$test", ':conten +t_file' => $test ); $pm->finish; } $pm->wait_all_children;


Evan Carroll
I hack for the ladies.
www.EvanCarroll.com

Replies are listed 'Best First'.
Re: Strawberry perl memory leak
by BrowserUk (Patriarch) on May 15, 2008 at 05:21 UTC

    Suitably adjusted to run under 5.8.x, I see the same behaviour (continuous memory growth) under both AS811/5.8.6 and AS1002/5.10. The memory growth is much faster with 5.10. After 100 forks, 5.8.6 is consuming 120MB. At the same point 5.10 is consuming 260MB.

    I suspect that the root cause is that the old threads are not being joined until the loop is completed, so that the old thread carcasses (stack and heap allocations) are just accumulating in memory (much like *nix zombies), until their return values are either collected or discarded with join or detach, and that isn't done until the wait_for_all method is called.

    Speculation, but it fits the symptoms.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
        Any solution in all of that?

        Not one you'll like. I reached the conclusion a long time ago that using fork on Win32 is a pointless exercise.

        The emulation simply isn't accurate enough, nor complete enough to enable transparent cross-platform solutions. And if you need to put conditional code in to test and account for Win32 limitations and variations, then it's better to move to a full Win32 native solution.

        See this Re: Question: Fast way to validate 600K websites from a few days ago that does almost exactly what you are trying to do using Parallel::ForkManager. It works (on Win32 and anywhere where threads works), and the code is hardly more complex.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Strawberry perl memory leak
by runrig (Abbot) on May 14, 2008 at 22:47 UTC
    My guess is that it's due to how fork is implemented. Perhaps try Win32::Process::Create().

    Update: This leaks too (under AS perl 5.10):

    Another update: I take that back...it grows to some size and then remains around the same.

    #!/usr/bin/perl use strict; use warnings; my $i; while (1) { unless ( ++$i % 20 ) { print "$i: "; my $p = scalar(<STDIN>); while (1) { my $pid = wait; print "[$pid]\n"; last if ! $pid or $pid == -1; } } my $pid = fork(); die "No fork: $!" unless defined $pid; print "$pid\n" if $pid; next if $pid; exit; }
Re: Strawberry perl memory leak
by CountZero (Bishop) on May 14, 2008 at 22:59 UTC
    And how does it behave under AS Perl 5.10?

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: Strawberry perl memory leak
by ysth (Canon) on May 15, 2008 at 03:21 UTC
    You didn't have a "state $count = 0" in AS 5.8, so I question your ascribing this to Strawberry vs. AS rather than (different code under) 5.8 vs. 5.10.
      the state has no effect. more of a reference to running the lwp in pfm


      Evan Carroll
      I hack for the ladies.
      www.EvanCarroll.com