C++ std::虽然有足够的可用内存,但存在错误的\u alloc异常

C++ std::虽然有足够的可用内存,但存在错误的\u alloc异常,c++,bad-alloc,C++,Bad Alloc,我的代码在64位Linux(openSUSE 13.1 x86_64)下运行,编译器是gcc(SUSE Linux)4.8.1。在程序执行的某个时刻,我得到一个std::bad_alloc异常,它源于std::vector push_-back调用。如gdb所示: (gdb) bt #0 0x00007ffff6053849 in raise () from /lib64/libc.so.6 #1 0x00007ffff6054cd8 in abort () from /lib64/libc

我的代码在64位Linux(openSUSE 13.1 x86_64)下运行,编译器是gcc(SUSE Linux)4.8.1。在程序执行的某个时刻,我得到一个std::bad_alloc异常,它源于std::vector 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
你知道为什么分配失败了吗?在我看来,系统好像没有释放缓存供我的程序使用


我只是尝试了一个小实验,然后想出了

#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" );
}


服务器似乎在某种程度上配置错误。即使是在一个刚启动的系统上,除了一些系统服务外,没有任何东西运行,我也无法分配超过70 GB的空间,我将其理解为64 GB+8 GB的交换空间。我已经联系了服务器主机,并抱怨了这种情况。

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

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

重复分配、取消分配和重新分配很容易导致内存碎片


超过某个配额也可以解释这种行为。

您的系统中可能存在每个进程的可用内存限制?您是否可以使用
free-mh
?单位不清楚。您的堆可能已损坏。这不仅仅是可用内存的数量。这是所需的连续内存量。测试程序对我来说工作得很好,16GB的Ubuntu 14.10系统。看看你的系统配置,而不是C++。我必须补充的是,还有两个程序同时运行,使用了近70g。我毫不怀疑,杀死它们将使我能够分配256M缓存使用的内存不能被碎片化,不是吗?!任何内存都可能变得支离破碎。碎片是使用的结果(例如分配和释放模式)。
#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" );
}
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