加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

perl – 为什么要提高plackup(或starman)的内存使用率?

发布时间:2020-12-16 06:25:54 所属栏目:大数据 来源:网络整理
导读:我有这个简单的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次并增加了内存使用量.根据启动服
我有这个简单的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#tmemn";
foreach my $i (1..1000) {
    my $res = $ua->request($req);
    (my $mem = $res->content) =~ s/D//g;
    next if( $mem == $old_mem );
    print "$it$memn";
    $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

所以,

>为什么在前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

解决方法

根据 miyagava的评论,答案是:

“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

感谢名单.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读