2015-03-31 295 views
0

我的代码运行在64位Linux(openSUSE 13.1 x86_64)下,编译器是gcc(SUSE Linux)4.8.1。我在执行程序时得到了一个std :: bad_alloc异常,该异常源自std :: vector push_back调用。如所看到的在gdb:std :: bad_alloc异常尽管有足够的空闲内存

(gdb) bt 
#0 0x00007ffff6053849 in raise() from /lib64/libc.so.6 
#1 0x00007ffff6054cd8 in abort() from /lib64/libc.so.6 
#2 0x00007ffff694c655 in __gnu_cxx::__verbose_terminate_handler()() from /usr/lib64/libstdc++.so.6 
#3 0x00007ffff694a7c6 in ??() from /usr/lib64/libstdc++.so.6 
#4 0x00007ffff694a7f3 in std::terminate()() from /usr/lib64/libstdc++.so.6 
#5 0x00007ffff694aa1e in __cxa_throw() from /usr/lib64/libstdc++.so.6 
#6 0x00007ffff694af1d in operator new(unsigned long)() from /usr/lib64/libstdc++.so.6 
#7 0x0000000000457ca6 in allocate (__n=8388608, this=0x7ffffffe1f80) 
    at /usr/include/c++/4.8/ext/new_allocator.h:104 
#8 _M_allocate (__n=8388608, this=0x7ffffffe1f80) at /usr/include/c++/4.8/bits/stl_vector.h:168 
#9 std::vector<std::pair<long, long>, std::allocator<std::pair<long, long> > >::_M_insert_aux (
    [email protected]=0x7ffffffe1f80, __position=..., __x=...) at /usr/include/c++/4.8/bits/vector.tcc:345 
#10 0x000000000045335c in push_back (__x=..., this=0x7ffffffe1f80) at /usr/include/c++/4.8/bits/stl_vector.h:913 
#11 c_RoutingNetzwerk::LoescheAktuelleKnoten ([email protected]=0x7ffffffe2f30, 
    aktuelle_knoten=std::vector of length 12803276, capacity 16777216 = {...}, [email protected]=0, 
    aktueller_kantengrad=std::vector of length 17266677, capacity 17266677 = {...}, algo=..., 
    neue_abgehende_kanten=std::vector of length 4194304, capacity 4194304 = {...}, 
    neue_eingehende_kanten=std::vector of length 4194304, capacity 4194304 = {...}) 
    at RoutingAlgorithmus/RoutingNetzwerk.cpp:3275 

的调用neue_abgehende_kanten.push_back(...)加倍向量的大小,所以我试图分配4194304 * 2 * 16个字节= 128兆字节,这将失败。

在另一方面,我有足够多的内存更多(132个GB的一切的一切),和足够的内存是免费的(虽然我的程序在调试器中断拍摄快照):

m2883:~ # free -m 
      total  used  free  shared buffers  cached 
Mem:  129151  128582  568   0   59  56334 
-/+ buffers/cache:  72189  56962 
Swap:   8195   5  8190 

任何想法,为什么分配失败了吗?它看起来好像系统没有释放它的缓存供我的程序使用?!


我只是尝试做一些实验,并与

#include <cstdlib> 
#include <stdio.h> 

int main(int argc, char** argv) 
{ 
    void* p = calloc(1, 256 * 1024 * 1024); 
    if (!p) 
     printf("failed\n"); 
    else 
     printf("all done\n"); 
} 

这仍然适用于128兆字节来了,但没有为256兆字节。

和:

m2883:~ # free -mh 
      total  used  free  shared buffers  cached 
Mem:   126G  125G  593M   0B  60M  54G 
-/+ buffers/cache:  70G  55G 
Swap:   8.0G  5.4M  8.0G 

[email protected]:~/test> ulimit -a 
core file size   (blocks, -c) 0 
data seg size   (kbytes, -d) unlimited 
scheduling priority    (-e) 0 
file size    (blocks, -f) unlimited 
pending signals     (-i) 1033140 
max locked memory  (kbytes, -l) 64 
max memory size   (kbytes, -m) unlimited 
open files      (-n) 1024 
pipe size   (512 bytes, -p) 8 
POSIX message queues  (bytes, -q) 819200 
real-time priority    (-r) 0 
stack size    (kbytes, -s) 8192 
cpu time    (seconds, -t) unlimited 
max user processes    (-u) 1033140 
virtual memory   (kbytes, -v) unlimited 
file locks      (-x) unlimited 

看来,服务器在某种程度上是错误配置。即使在刚刚启动的系统上,只有少数系统服务没有运行,我也无法分配超过70 GB的字节,我读取的内容是64 GB + 8 GB交换空间。我已联系服务器主机,并抱怨这种情况。

+0

可能是系统中每个进程的内存限制可用? – CiaPan 2015-03-31 09:18:52

+0

您可以使用'free -mh'吗?单位不清楚。你的堆可能被损坏。 – chmike 2015-03-31 09:28:37

+1

这不仅仅是可用内存的数量。这是*连续*内存的数量是必需的。 – PaulMcKenzie 2015-03-31 09:30:53

回答

0

没有关于您的代码和系统配置的信息,很难确定。

内存碎片会解释症状。矢量使用的内存是连续的,即使可用的内存总量足够大,如果没有可供分配的所需大小的单个连续块,分配也将失败。

重复分配,取消分配和重新分配可能很容易导致内存碎片。

超过一些配额也会解释行为。

+0

缓存使用的内存不能被分割,可以吗?! – 2015-03-31 09:41:01

+0

任何内存都可能变成碎片。碎片是使用的结果(例如分配和释放模式)。 – Peter 2015-03-31 09:48:39