2015-03-31 169 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

相關問題