in reply to Re: Memory Leak Caused by Forking?
in thread Memory Leak Caused by Forking?

One more point:

When you are forking a process that then exits the parent process should to a wait or waitpid to remove the entry fom the process table.

I am not really sure about Windows but I assume the layer that emulates fork would also emulate this, so even without a memory leak your code would waste ressources.

Replies are listed 'Best First'.
Re^3: Memory Leak Caused by Forking?
by BrowserUk (Patriarch) on Oct 21, 2010 at 04:27 UTC
    I am not really sure about Windows but I assume the layer that emulates fork would also emulate this

    Indeed it does. This version takes care of that, but it doesn't stop the memory leak. It might slow it a little, it is hard to tell, but there are also other problems with the fork emulation that manifest themselves.

    For example, for some reason, using the DATA handle starts returning blank lines interspersed with the actual lines when forking is in use. Which doesn't happen when the fork is skipped. Just another indication that no one is using (or even properly testing) the fork emulation on windows,

    He should also be checking the file opens which probably aren't doing what he thinks they are. He probably shouldn't be using bare-words file handles for this. etc. etc.

    But until he explains why he feels the need to use fork here at all? There just doesn't seem any logic at all in starting a new "process" to fetch a page; pipe that content back to the parent just for it to write it to disk.

    #! perl -slw use strict; use LWP::UserAgent; use HTTP::Request; use HTTP::Response; use HTTP::Message; my $iteration = 1; my $some_directory = 'C:/test/junk'; #open(URLS, $some_directory.'urls.txt'); while (<DATA>) { my $url = $_; chomp $url; get_url($url, $iteration); print "($iteration) $url\n"; #sleep 3; $iteration++; } #close URLS; sub get_url { my ($url, $iteration) = @_; pipe(my $READER, my $WRITER); if (my $pid = fork) { close $WRITER; open(my $FH, '>'. "$some_directory/$iteration" ) or die $!; while (<$READER>) { print $FH $_; } close $READER; close $FH; waitpid $pid, 0; print "$pid returned: ", $? >> 8; } elsif (defined $pid) { close $READER; my $ua = LWP::UserAgent->new(); my $request = HTTP::Request->new("GET", $url); my $response = $ua->request($request); my $content = $response->content(); print $WRITER $content; close $WRITER; undef $request; undef $response; exit 123; } }

    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.