“内存不足";使用简单的Win32 :: Unicode :: File readline循环和Strawberry Perl [英] "Out of memory" with simple Win32::Unicode::File readline loop and Strawberry Perl

查看:98
本文介绍了“内存不足";使用简单的Win32 :: Unicode :: File readline循环和Strawberry Perl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题可以通过在Windows XP上的Strawberry perl 5.12.3.0中运行以下代码来找到.

The issue I have can be found by running the following code in Strawberry perl 5.12.3.0 on Windows XP.

    #!/usr/bin/perl -w

    use strict;
    use warnings;
    use Win32::Unicode::File;
    use Encode;

    my $fname = shift @ARGV;

    my $fh = Win32::Unicode::File->new;
    if ($fh->open('<', $fname)){
      while (my $line = $fh->readline()){}
      close $fh;
    }else{
      print "Couldn't open file: $!\n";
    }

这里发生的唯一一件事情是,我执行了一条readline,这一直在消耗内存,直到我从Strawberry Perl中收到内存不足"错误为止.我正在使用一个非常大的文件,但是由于此代码基于流,因此无关紧要.我在这里错过了什么吗,还是草莓perl的某个地方漏了水?我在ActivePerl中测试了完全相同的代码,并且在那里工作正常,即,它不占用内存.

The only thing that is happening here is that I perform a readline and this keeps eating memory until I get an Out of memory error from Strawberry perl. I am using a really big file but since this code is stream based it shouldn't matter. Am I missing something here or is there a leak somewhere in Strawberry perl? I tested the exactly same code in ActivePerl and there it works fine, i.e., it doesn't eat memory.

更新:用普通的菱形运算符替换Win32 :: Unicode :: File似乎至少对我的发行版有效.请参阅以下代码.

Update: Replacing Win32::Unicode::File with the normal diamond operator seems to work on my distribution at least. See the following code.

    use strict;
    use warnings;

    my $fname = shift @ARGV;

    if (open(my $fh, '<', $fname)){
      while (my $line = <$fh>){}
      close $fh;
    }else{ print "Couldn't open file: $!\n";}

那么这表明问题出在Win32 :: Unicode模块上吗?

So that would suggest the problem lies with Win32::Unicode module right?

推荐答案

我猜这有点不合常规,但是我要回答我自己的问题.我用Path :: Class :: Unicode包代替了Win32 :: Unicode :: File包,而不是读取unicode文件.这可以正常工作(即不消耗内存),因此问题似乎出在Win32 :: Unicode :: File软件包中,并且很可能是一个错误.我已经联系了包裹的作者,他正在调查中.如果您要我提供代码,请告诉我.非常简单.

A little unorthodox I guess, but I'm going to answer my own question. I have replaced the Win32::Unicode::File package with the Path::Class::Unicode package instead for reading the unicode file. This works fine (i.e. no memory eating) so it seems like the problem is in the Win32::Unicode::File package and is most likely a bug. I have contacted the author of the package and he's looking into it. Please let me know if you want me to supply the code. It's pretty straightforward.

这篇关于“内存不足";使用简单的Win32 :: Unicode :: File readline循环和Strawberry Perl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆