我的代码运行在64位Linux下(openSUSE 13.1 x86_64),编译器是gcc (SUSE ) 4.8.1。在执行我的程序时,我会得到一个std::bad_alloc异常,它来自于std::push_back调用。如gdb所示:
(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 (
this=this@entry=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 (this=this@entry=0x7ffffffe2f30,
aktuelle_knoten=std::vector of length 12803276, capacity 16777216 = {...}, ebene=ebene@entry=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个MBytes,但这失败了。
另一方面,我有足够的内存(总共132个GBytes ),并且有足够的内存可用(在调试器中程序中断时拍摄的快照):
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个MByte仍然有效,但在256个MByte中失败。
和:
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
transit@m2883:~/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个GBytes,我将其读为64 GBytes +8 GBytes交换空间。我已经联系了服务器主机,并抱怨了这种情况。
发布于 2015-03-31 09:18:09
如果没有关于代码和系统配置的信息,就很难确定。
记忆碎片可以解释这种症状。向量使用的内存是连续的,即使可用内存总量足够,如果没有所需大小的连续块可供分配,则分配也将失败。
重复分配、重新分配和重新分配很容易导致内存碎片。
超过某些配额也可以解释这种行为。
https://stackoverflow.com/questions/29364737
复制相似问题