为什么会提高 plackup(或 starman)内存使用量?

发布于 2024-11-09 08:24:46 字数 2242 浏览 0 评论 0原文

我有这个简单的 PSGI 应用程序 (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 ]];
};

我被请求了以上 1000 次,并且内存使用量增加了。根据服务器的启动方式,得到:

  • plackup - 内存使用量在前 3 个请求时增加,并在接下来的 997 个请求中保持不变

  • plackup -r - 内存使用量随机增加(并非每次请求时)4k。

  • starman - 与上面一样,内存使用量随机增加 4k,但速度较慢

问题是:

  • 为什么提高内存使用量?泄漏在哪里,以及如何实现恒定的内存使用(尤其是在 starman 上),因为我不希望长期运行内存不足。 (好的,可以定义例如 --max-requests 100),但这不是内存使用情况的答案。
  • - 我的示例中有什么问题?

如果有人也想测试这个 - 这是我的提取脚本:

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;
}

我的结果:

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

那么,

  • 为什么 plackup 在前 3 个请求中提出?
  • plackup -r - 4k 增加(见最后几行) - 一开始更多
  • starman - 也增加,但默认 5 个工人的速度较慢(3280->3912 )

版本:

# 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

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 requests

  • plackup -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 more
  • starman - 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

素染倾城色 2024-11-16 08:24:47

根据 miyagava 的评论,答案是:

“plackup - 内存使用量正在增加
前 3 个请求并保持不变
对于接下来的 997 个请求”这意味着
一些模块是延迟加载的
最初的几个请求。之后有
没有泄漏。 – 宫川 14 小时前

Starman 默认启用 keep-alive
和 HTTP 管道,这意味着如果您
短时间内发送1000个请求
总有一天你会拥有这些联系
连接,除非你明确
断开它们。我可以确认这一点
使用 ApacheBench - 内存
暂时增加,但当它们
断开连接/超时,内存得到
下降到原来的位置。 – 宫川 14
小时前

谢谢。

Based on miyagava's comments the answer is:

"plackup - memory usage is raising at
first 3 requests and remain constant
for the next 997 requests" That means
some modules are lazy-loaded in the
first few requests. After that there's
no leak. – miyagawa 14 hours ago

Starman by default enables keep-alive
and HTTP pipelining, meaning if you
send 1000 requests in the short period
of time you'll have these connections
connected, unless you explicitly
disconnect them. I can confirm this
using ApacheBench - the memory
temporarily increases, but when they
disconnect/timeout, the memory gets
down to where it was. – miyagawa 14
hours ago

thanx.

金橙橙 2024-11-16 08:24:47

你用的是最新版本吗?我无法重现你的输出。

使用“plackup”:

sidburn@sid:~/perl/plack$ ./memory.pl 
req#    mem
1   5340
2   5380

使用“plackup -r”:

sidburn@sid:~/perl/plack$ ./memory.pl 
req#    mem
1   4860
2   5060

使用“starman”:

sidburn@sid:~/perl/plack$ ./memory.pl 
req#    mem
1   5176
5   5224
6   5176
7   5224

版本:
Perl:5.12.1 和5.12.3
普拉克:0.9979
星人:0.2010

Did you use the latest version? I cannot reproduce your output.

With "plackup":

sidburn@sid:~/perl/plack$ ./memory.pl 
req#    mem
1   5340
2   5380

With "plackup -r":

sidburn@sid:~/perl/plack$ ./memory.pl 
req#    mem
1   4860
2   5060

With "starman":

sidburn@sid:~/perl/plack$ ./memory.pl 
req#    mem
1   5176
5   5224
6   5176
7   5224

Versions:
Perl: 5.12.1 & 5.12.3
Plack: 0.9979
Starman: 0.2010

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文