nwboy74 has asked for the wisdom of the Perl Monks concerning the following question:
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/
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Memory Leak Caused by Forking?
by BrowserUk (Patriarch) on Oct 21, 2010 at 01:50 UTC | |
by morgon (Priest) on Oct 21, 2010 at 03:49 UTC | |
by BrowserUk (Patriarch) on Oct 21, 2010 at 04:27 UTC | |
by nwboy74 (Novice) on Oct 21, 2010 at 15:25 UTC | |
by BrowserUk (Patriarch) on Oct 21, 2010 at 20:08 UTC | |
|
Re: Memory Leak Caused by Forking?
by aquarium (Curate) on Oct 20, 2010 at 23:29 UTC | |
| |
|
Re: Memory Leak Caused by Forking?
by locked_user sundialsvc4 (Abbot) on Oct 21, 2010 at 14:53 UTC |