【发布时间】:2014-07-17 11:28:20
【问题描述】:
我正在运行一个 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 左右得到了同样的限制。
【问题讨论】:
-
您的意思是 GB(字节)而不是 Gb(位),对吗?您能告诉我们这个脚本使用的 8 GB 的大部分分配是什么吗?是很多小块还是几个大块?知道它在用完时试图分配多少内存吗?你试过在调试器中运行吗?
-
尝试编辑您的问题并粘贴到
perl -V的输出中 -
@JohnZwinck 是的千兆字节。我认为是小块。我将编辑问题以放置给出错误的代码部分。
-
@MarkSetchell 我添加了 perl -V 的输出
-
愚蠢的问题:您确定运行脚本的用户对内存使用没有任何限制吗?这可以通过
ulimit -a进行检查。