我正在运行一个因内存不足错误而崩溃的 perl 脚本。 该脚本在具有 128 Gb 内存的机器上的 Ubuntu Linux 下运行。在提交时,大部分内存可用,但脚本会因为其使用的内存超过略高于 8Gb 的值而终止。机器(和操作系统)是 64 位的。
我一直在网上搜索 perl 中内存分配的限制,但我发现唯一的限制是机器内存,在这种情况下,即使不考虑交换分区也有很多限制。
这是我第二次遇到这个问题。我第一次使用不同的脚本时发生了同样的事情。有人有解释吗?我看到的唯一可能性是 perl 对内存分配有一些限制,但我在网上搜索的所有结果似乎都与这种可能性相矛盾。
提前致谢
编辑 1: 操作系统是 Fedora Linux,不是 Ubuntu Linux。对不起,我弄糊涂了。
编辑 2: 以下是导致错误的代码部分:
open( $psFullInput, "<", "fullPsIn.dat" );
$counter = <$psFullInput>; # First element is counter of spectra
while ($line = <$psFullInput>) {
@elems = split(" ",$line);
$xx = shift(@elems);
$yy = shift(@elems);
$freq = shift(@elems);
$psStored[$xx][$yy] = [];
push( @{$psStored[$xx][$yy]}, @elems );
}
close( $psFullInput );
之前的脚本做了类似的事情,除了数组元素不是从文件中读取而是一些计算的结果。
编辑 3: perl -V 的结果:
Summary of my perl5 (revision 5 version 16 subversion 3) configuration:
Platform:
osname=linux, osvers=3.10.9-200.fc19.x86_64, archname=x86_64-linux-thread-multi
uname='linux buildvm-01.phx2.fedoraproject.org 3.10.9-200.fc19.x86_64 #1 smp wed aug 21 19:27:58 utc 2013 x86_64 x86_64 x86_64 gnulinux '
config_args='-des -Doptimize=-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -Dccdlflags=-Wl,--enable-new-dtags -Dlddlflags=-shared -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -Wl,-z,relro -DDEBUGGING=-g -Dversion=5.16.3 -Dmyhostname=localhost -Dperladmin=root@localhost -Dcc=gcc -Dcf_by=Red Hat, Inc. -Dprefix=/usr -Dvendorprefix=/usr -Dsiteprefix=/usr/local -Dsitelib=/usr/local/share/perl5 -Dsitearch=/usr/local/lib64/perl5 -Dprivlib=/usr/share/perl5 -Dvendorlib=/usr/share/perl5/vendor_perl -Darchlib=/usr/lib64/perl5 -Dvendorarch=/usr/lib64/perl5/vendor_perl -Darchname=x86_64-linux-thread-multi -Dlibpth=/usr/local/lib64 /lib64 /usr/lib64 -Duseshrplib -Dusethreads -Duseithreads -Dusedtrace=/usr/bin/dtrace -Duselargefiles -Dd_semctl_semun -Di_db -Ui_ndbm -Di_gdbm -Di_shadow -Di_syslog -Dman3ext=3pm -Duseperlio -Dinstallusrbinperl=n -Ubincompat5005 -Uversiononly -Dpager=/usr/bin/less -isr -Dd_gethostent_r_proto -Ud_endhostent_r_proto -Ud_sethostent_r_proto -Ud_endprotoent_r_proto -Ud_setprotoent_r_proto -Ud_endservent_r_proto -Ud_setservent_r_proto -Dscriptdir=/usr/bin -Dusesitecustomize'
hint=recommended, useposix=true, d_sigaction=define
useithreads=define, usemultiplicity=define
useperlio=define, d_sfio=undef, uselargefiles=define, usesocks=undef
use64bitint=define, use64bitall=define, uselongdouble=undef
usemymalloc=n, bincompat5005=undef
Compiler:
cc='gcc', ccflags ='-D_REENTRANT -D_GNU_SOURCE -fno-strict-aliasing -pipe -fstack-protector -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64',
optimize='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic',
cppflags='-D_REENTRANT -D_GNU_SOURCE -fno-strict-aliasing -pipe -fstack-protector -I/usr/local/include'
ccversion='', gccversion='4.8.2 20131017 (Red Hat 4.8.2-1)', gccosandvers=''
intsize=4, longsize=8, ptrsize=8, doublesize=8, byteorder=12345678
d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=16
ivtype='long', ivsize=8, nvtype='double', nvsize=8, Off_t='off_t', lseeksize=8
alignbytes=8, prototype=define
Linker and Libraries:
ld='gcc', ldflags =' -fstack-protector'
libpth=/usr/local/lib64 /lib64 /usr/lib64
libs=-lresolv -lnsl -lgdbm -ldb -ldl -lm -lcrypt -lutil -lpthread -lc -lgdbm_compat
perllibs=-lresolv -lnsl -ldl -lm -lcrypt -lutil -lpthread -lc
libc=, so=so, useshrplib=true, libperl=libperl.so
gnulibc_version='2.17'
Dynamic Linking:
dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-Wl,--enable-new-dtags -Wl,-rpath,/usr/lib64/perl5/CORE'
cccdlflags='-fPIC', lddlflags='-shared -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -Wl,-z,relro '
Characteristics of this binary (from libperl):
Compile-time options: HAS_TIMES MULTIPLICITY PERLIO_LAYERS
PERL_DONT_CREATE_GVSV PERL_IMPLICIT_CONTEXT
PERL_MALLOC_WRAP PERL_PRESERVE_IVUV USE_64_BIT_ALL
USE_64_BIT_INT USE_ITHREADS USE_LARGE_FILES
USE_LOCALE USE_LOCALE_COLLATE USE_LOCALE_CTYPE
USE_LOCALE_NUMERIC USE_PERLIO USE_PERL_ATOF
USE_REENTRANT_API USE_SITECUSTOMIZE
Built under linux
Compiled at Nov 11 2013 12:36:47
%ENV:
PERL5LIB="/home/parisia/lib/perl5/lib64/perl5"
@INC:
/home/parisia/lib/perl5/lib64/perl5
/usr/local/lib64/perl5
/usr/local/share/perl5
/usr/lib64/perl5/vendor_perl
/usr/share/perl5/vendor_perl
/usr/lib64/perl5
/usr/share/perl5
.
编辑 4: 这是重现问题的简短代码。 如您所见,我只是在填充内存。
use strict;
use warnings;
my ($xx,$yy); # Coordinate variables
my (@elems); # Array of elements to be stored on each matrix position
# Generate an array of 3000 floating point values.
# The list will be added to each array element. In the true script
# of course, each element has a list of completely different values.
# Here I use the same list of values for simplicity.
for ($xx = 1; $xx < 3000; $xx++) {
push( @elems, 1+$xx/10000 );
}
# Fill in each matrix element with the generated array
my @psStored;
for ($xx = 0; $xx < 300; $xx++) {
print "Row [$xx]\n";
for ($yy = 0; $yy < 300; $yy++) {
push( @{$psStored[$xx][$yy]}, @elems );
}
}
这是崩溃前“free”的结果(机器上有一些并行进程在运行):
[parisia@gloria01 ~]$ free
total used free shared buffers cached
Mem: 132015788 46395504 85620284 0 217192 16979772
-/+ buffers/cache: 29198540 102817248
Swap: 268435452 0 268435452
更新 1
我进一步调查。按照建议,我使用了 Devel::Size 并检查了 $psStored 的大小。输出的最后一行(在 $yy 中打印每个循环结束时的大小)是:
Row [226]
8772773032
Row [227]
8811419600
Row [228]
Out of memory!
脚本报错时进程的大小为: VIRT:8943960,RES:8.406g
但是,我尝试了以下脚本,它按照命令行指定的方式分配给定大小(以千兆字节为单位)的字符串:
use strict;
use warnings;
my $size = $ARGV[0];
print "$size GB ";
$size = int($size * 1000000000);
print "($size bytes)...\n";
my $var = "x" x $size;
print "Allocated\n";
这个脚本没有大分配的问题。例如,我可以请求 30 GB,并且在脚本完成之前从“top”获得以下输出: VIRT:56.004g,RES:0.054t
因此,在处理数组时,有些东西会干扰分配,但我不明白发生了什么。我也尝试过使用哈希,但我在 8-9GB 左右遇到了同样的限制。
最佳答案
我怀疑 $xx
和 $yy
的值相当大且稀疏(即它们之间存在很大差距)。这意味着 Perl 必须为所有中间值创建数组元素,即使其中没有数据。
数据结构的设计主要取决于构建后你想用它做什么,以及你需要如何访问它。最节省空间的方法是使用散列,这样如果你有
$xx = 1024
$yy = 2048
然后不是将频率存储在 $ps_stored[1024][2048]
中(创建 $ps_stored[0]
到 $ps_stored[1023]
和 $ps_stored[1024][0]
到 $ps_stored[1024][2047]
并将它们留空)你可以将它存储在 hash $ps_stored{'1024,2048'}
完全不浪费空间。
由于您没有说明您将如何使用这些数据,所以我无法判断它是否可以像这样工作,但这里有一些代码可以替换您以这种方式构建哈希的代码。
use strict;
use warnings;
use autodie;
my %ps_stored;
open my $ps_full_input, '<', 'fullPsIn.dat';
my $counter = <$ps_full_input>; # First element is counter of spectra
while (<$ps_full_input>) {
my @elems = split;
my ($xx, $yy, $freq) = @elems;
push @{ $ps_stored{"$xx,$yy"} }, \@elems;
}
close $ps_full_input;
注意以下几点
您必须始终检查
open
调用是否成功。忽略失败的open
将使您的程序无缘无故地产生无意义的东西。你可以写一个明确的open my $ps_full_input, '<', 'fullPsIn.dat' or die $!;
或者您可以在程序的顶部
使用 autodie
,如果您的代码中有多个open
调用,这将很有用您必须始终在每个 Perl 程序的顶部
使用严格
和使用警告
,并将所有变量声明为关闭尽可能在他们第一次使用my
时使用。因为你的代码是一个示例,所以不清楚你是否有strict
和warnings
,但是根本没有声明所以有问题习惯使用 Perl 的人会感谢您在本地标识符中只使用小写字母、数字和下划线。大写字母保留用于包和模块名称,如
Data::Dumper
在您
push
之前,无需将标量值预设为对空数组的引用。只要标量仍为undef
,数组将在您第一次使用push
时自动生成。例如my $aref; push @{ $aref }, 1, 2, 3;
效果与
相同my $aref; $aref = []; push @{ $aref }, 1, 2, 3;
我已将对
@elems
数组的引用 推送到ps_stored
列表中。我无法判断$xx
和$yy
的特定值是否可以多次出现,但如果它们出现,那么您的方式只会推送所有不同的@elems
集合到一个列表中,这可能很难拆分成单独的集合。如果您改为推送引用,那么它们将保持独立
希望对你有帮助
关于linux - Perl 内存不足但有足够的可用内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24802302/