in reply to Memory Leak Caused by Forking?
There are (at least) three problems with your program:
This: my $content = $response->content(); fetches the entire web page as a single lump and returns it to you in $content.
Which you then write to your pipe as a single lump: print WRITER $content;.
In your fork, you then read that back, line by line from the pipe and write it out to your file. That's nonsensical.
Why not skip the fork and write directly to the file? This works:
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:/test/junk'; #open(URLS, $some_directory.'urls.txt'); while (<DATA>) { my $url = $_; chomp $url; &get_url($url, $iteration); print "($iteration) $url\n"; $iteration++; } close URLS; sub get_url { my ($url, $iteration) = @_; open(FH, '>'.$some_directory.$iteration); my $request = HTTP::Request->new("GET", $url); my $response = $ua->request($request); my $content = $response->content(); print FH $content; close FH; return; } __DATA__ ...
And it allowed me to remove that dumbly arbitrary 15 seconds delay that wastes 13 seconds when the pages only take 2 seconds to download.
(Or doing a disservice to those of whom you are asking questions, by concealing the real requirements of your code. Simplifying the problem too much gets answers to the wrong questions).
This also works (without leaks), and is far simpler:
use strict; use warnings; use LWP::Simple; my $iteration = 1; my $some_directory = 'C:/test/junk'; #open(URLS, $some_directory.'urls.txt'); while (<DATA>) { my $url = $_; chomp $url; getstore( $url, $some_directory . $iteration ); print "($iteration) $url\n"; $iteration++; } __DATA__ ...
This is rarely used and barely tested. It is quite likely that it leaks over time; that those leaks are a internal and would require the bug report and the release of a new version to fix.
If there is a need to do the downloads asynchronously (and on the basis of what you've presented here, you don't), then threads are a far simpler and better tested option.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Memory Leak Caused by Forking?
by morgon (Priest) on Oct 21, 2010 at 03:49 UTC | |
by BrowserUk (Patriarch) on Oct 21, 2010 at 04:27 UTC | |
|
Re^2: Memory Leak Caused by Forking?
by nwboy74 (Novice) on Oct 21, 2010 at 15:25 UTC | |
by BrowserUk (Patriarch) on Oct 21, 2010 at 20:08 UTC |