I have this simple PSGI application (app.psgi).
use strict;
use warnings;
my $app = sub {
my $mem = `ps -o rss= -p $$`;
$mem =~ s/^\s*|\s*$//gs;
return [ 200, [ 'Content-Type' => 'text/text' ], [ $mem ]];
};
I was requested the above 1000 times and got increased memory usage. Depending on how was started the server, got:
plackup
- memory usage is raising at first 3 requests and remain constant for the next 997 requestsplackup -r
- memory usage is randomly raising (not at every request) by 4k.starman
- like above, the memory usage is randomly raising by 4k, but with slower rate
The question is:
- WHY is raising the memory usage ? Where is the leak, and how achieve constant memory usage (especially on starman), because i don't want run out of memory at long run. (OK, it is possible define for example --max-requests 100), but it is not an answer for the memory usage.
- or - what is wrong in my example?
If anyone want test this too - here is my script for the fetching:
use strict;
use warnings;
use LWP::UserAgent;
my $ua = LWP::UserAgent->new;
my $req = HTTP::Request->new(GET => 'http://localhost:5000');
my $old_mem = 0;
print "req#\tmem\n";
foreach my $i (1..1000) {
my $res = $ua->request($req);
(my $mem = $res->content) =~ s/\D//g;
next if( $mem == $old_mem );
print "$i\t$mem\n";
$old_mem = $mem;
}
My results:
plackup plackup -r starman
req# mem req# mem req# mem
1 7780 1 3924 1 3280
2 7800 2 4296 5 3728
3 7804 3 4304 8 3280
... ...
... deleted ... deleted
... ...
839 4596 994 3912
866 4600 998 3908
962 4604 1000 3912
So,
- why
plackup
raising in first 3 requests? plackup -r
- 4k increase (see last lines) - at the beginning much morestarman
- raising too, but with default 5 workers in slower rate (3280->3912)
Versions:
# cpanm Plack Starman
Plack is up to date. (0.9979)
Starman is up to date. (0.2010)
# perl -v
This is perl 5, version 12, subversion 3 (v5.12.3) built for darwin-thread-multi-2level
-r
option) - I'll see if there's any place that is leaking. Because -r is usually used during the development I wouldn't consider it a huge deal - but it's always nice to have no leaks :) – Gelhar