- 论坛徽章:
- 0
|
本帖最后由 hbasrc 于 2018-07-01 21:19 编辑
先推荐一篇好文:circular-references-memory-leaks-and-mojo
找到此文的起因是在使用Mojo::UserAgent 过程中发现有内存泄漏问题。
如下例:
- use 5.010;
- use strict;
- use Data::Printer;
- use Mojo::UserAgent;
- use Mojo::IOLoop;
- my @urls = qw/127.0.0.1 127.0.0.1:3000 www.metacpan.org www.perl.org/;
- my $ua = new Mojo::UserAgent(max_redirects => 2);
- my $max_conn = 4;
- my $alive = 0;
- my $id = Mojo::IOLoop->recurring( 0.2 => sub {
- return if $alive >= $max_conn or @urls < 1;
- for (1..$max_conn-$alive) {
- my $url = shift @urls;
- say 'HTTP Get: ', $url;
- $alive++;
-
- $ua->get($url => sub {
- my (undef, $tx) = @_;
- if ($tx->success) {
- say $tx->res->dom->at('title')->text;
- } else {
- say $tx->error->{message}, ': ', $tx->error->{code};
- }
- --$alive;
- push @urls, $url;
- });
- }
- });
- Mojo::IOLoop->start;
- 1;
复制代码
在perl 5.16中上例运行后会有缓慢而又轻微的内存泄漏,
上面推荐的这篇文章很好的分析了在大量使用闭包的代码中产生内存泄漏的原因及避免的办法。
上例中如果换到perl 5.18+中问题就消失了。
但在perl 5.22中下面的代码又出现了严重内存泄漏:
- # Perl 5.22.4 + Mojolicious v7.48
- ./perl -MMojo::UserAgent -E "say Mojo::UserAgent->new->get('www.qq.com')->res->dom->at('title')->text while 1"
复制代码 问题可能出在DOM解析部分的模块,因为暂时用不到DOM解析,没去追踪内在问题。
|
评分
-
查看全部评分
|