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.
RIP an inspiration; A true Folk's Guy

In reply to Re^3: Memory Leak Caused by Forking? by BrowserUk
in thread Memory Leak Caused by Forking? by nwboy74

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.