When I run the program below on a Windows XP machine and I watch the memory usage in the Task Manager, the Page File usage climbs steadily with each successive call to get_url. Can anyone point out why, for instance, I start with 887MB and end at 920MB; and as soon as the program completes, it drops back down? Imagine calling thousands of pages and how the memory would continue to expand until you run out.

use strict; use warnings; use LWP::UserAgent; use HTTP::Request; use HTTP::Response; use HTTP::Message; my $ua = LWP::UserAgent->new(); my $iteration = 1; my $some_directory = 'C:\\'; open(URLS, $some_directory.'urls.txt'); while (<URLS>) { my $url = $_; chomp $url; &get_url($url, $iteration); print "($iteration) $url\n"; sleep 15; $iteration++; } close URLS; sub get_url { my ($url, $iteration) = @_; pipe(READER, WRITER); if (my $pid = fork) { close WRITER; open(FH, '>'.$some_directory.$iteration); while (<READER>) { print FH; } close READER; close FH; } elsif (defined $pid) { close READER; 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; } }

Here's a random sample of urls I have in urls.txt:

http://www.google.com/ http://www.yahoo.com/ http://www.capweek.com/ http://www.autosource1.ca/index.htm http://www.keithbuickgmc.com/en/ http://www.pawtuckettimes.com/ http://www.stateportpilot.com/ http://www.memphisflyer.com/ http://www.thetelegram.com/ http://www.nugget.ca/ http://highcountrypress.com/ http://www.howstuffworks.com/ http://www.oregonlottery.org/ http://www.springfieldnewssun.com/ http://www.mountvernonnews.com/ http://www.jdnews.com/ http://www.aurorasentinel.com/ http://www.cariddiauto.com/ http://www.enterprise-journal.com/ http://www.naplesnews.com/

In reply to 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.