Linux C++;地图变得非常巨大后,程序与St9bad_alloc崩溃 我正在运行一个C++程序,它涉及在红帽Linux 64位上建立倒排索引。我的反转索引定义为map invertIDsecond;//这是174号线。

Linux C++;地图变得非常巨大后,程序与St9bad_alloc崩溃 我正在运行一个C++程序,它涉及在红帽Linux 64位上建立倒排索引。我的反转索引定义为map invertIDsecond;//这是174号线。,c++,linux,memory-management,map,bad-alloc,C++,Linux,Memory Management,Map,Bad Alloc,在谷歌上搜索,我发现这个错误可能来自new,但看看我的代码,我没有使用任何new关键字,但是,我在map上有这样的内存分配。在每次迭代中,我都会不断地插入键/值对。所以我决定用trycatch语句做一些实验 事实上,以下是代码和输出的关键部分: map<unsigned long long int, map<int,int> >::iterator mainMapIt = invertID.find(ID); if (mainMapIt != invert

在谷歌上搜索,我发现这个错误可能来自
new
,但看看我的代码,我没有使用任何
new
关键字,但是,我在map上有这样的内存分配。在每次迭代中,我都会不断地插入键/值对。所以我决定用
try
catch
语句做一些实验

事实上,以下是代码和输出的关键部分:

    map<unsigned long long int, map<int,int> >::iterator mainMapIt = invertID.find(ID);
    if (mainMapIt != invertID.end()){
    //if this ImageID key exists in InvID sub-map
        map<int,int> M = mainMapIt->second; // THIS IS LINE 174.
        map<int,int>::iterator subMapIt = M.find(imageID);
        if (subMapIt != M.end()){
        //increment the number of this ImageID key
            ++invertID[ID][imageID];
        }
        else{
        //add ImageID key with value 1 into the InvertID
            try{
                invertID[ID][imageID] = 1;
                ++totalPushBack;
            }catch (bad_alloc ba){
                cout << "CAUGHT 1: invertID[" << ID << "][" << imageID << endl;
            }
        }
    }
    else{
    //create the first empty map with the key as image ID with value 1 and put it in implicitly to the invertID
        try{
            invertID[ID][imageID] = 1;
        }catch (bad_alloc ba){
            cout << "CAUGHT 2: invertID[" << ID << "][" << imageID << endl;
        }
    }
我看到当我尝试插入新密钥时,抛出了错误。然而,在我用
try
catch
块覆盖键插入部分后,仍然会抛出
St9bad\u alloc
。我做了一点回溯,结果如下:

(gdb) backtrace
#0  0x000000344ac30265 in raise () from /lib64/libc.so.6
#1  0x000000344ac31d10 in abort () from /lib64/libc.so.6
#2  0x00000034510becb4 in __gnu_cxx::__verbose_terminate_handler() () from /usr/lib64/libstdc++.so.6
#3  0x00000034510bcdb6 in ?? () from /usr/lib64/libstdc++.so.6
#4  0x00000034510bcde3 in std::terminate() () from /usr/lib64/libstdc++.so.6
#5  0x00000034510bceca in __cxa_throw () from /usr/lib64/libstdc++.so.6
#6  0x00000034510bd1d9 in operator new(unsigned long) () from /usr/lib64/libstdc++.so.6
#7  0x0000000000406544 in __gnu_cxx::new_allocator<std::_Rb_tree_node<std::pair<int const, int> > >::allocate (
    this=0x7fffffffdfc0, __n=1)
    at /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/ext/new_allocator.h:88
#8  0x0000000000406568 in std::_Rb_tree<int, std::pair<int const, int>, std::_Select1st<std::pair<int const, int> >, std::less<int>, std::allocator<std::pair<int const, int> > >::_M_get_node (this=0x7fffffffdfc0)
    at /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_tree.h:358
#9  0x0000000000406584 in std::_Rb_tree<int, std::pair<int const, int>, std::_Select1st<std::pair<int const, int> >, std::less<int>, std::allocator<std::pair<int const, int> > >::_M_create_node (this=0x7fffffffdfc0, __x=...)
    at /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_tree.h:367
#10 0x00000000004065e3 in std::_Rb_tree<int, std::pair<int const, int>, std::_Select1st<std::pair<int const, int> >, std::less<int>, std::allocator<std::pair<int const, int> > >::_M_clone_node (this=0x7fffffffdfc0, __x=0x21c082bd0)
    at /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_tree.h:381
#11 0x0000000000406634 in std::_Rb_tree<int, std::pair<int const, int>, std::_Select1st<std::pair<int const, int> >, std::less<int>, std::allocator<std::pair<int const, int> > >::_M_copy (this=0x7fffffffdfc0, __x=0x21c082bd0, __p=0x7fffffffdfc8)
    at /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_tree.h:1226
#12 0x00000000004067e9 in std::_Rb_tree<int, std::pair<int const, int>, std::_Select1st<std::pair<int const, int> >, std::less<int>, std::allocator<std::pair<int const, int> > >::_Rb_tree (this=0x7fffffffdfc0, __x=...)
    at /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_tree.h:570
#13 0x0000000000406885 in std::map<int, int, std::less<int>, std::allocator<std::pair<int const, int> > >::map (
    this=0x7fffffffdfc0, __x=...) at /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_map.h:175
#14 0x0000000000403039 in generateInvertID (pathToPF=0x6859a8 "/home/karl/c/000605.pf",
    pathToC=0x38c139ed8 "/home/karl/c/000605.c", imageID=3856)
    at InvertIndexGen.cpp:174
#15 0x0000000000403b46 in generateInvertIDForAllPFAndC () at InvertIndexGen.cpp:254
#16 0x0000000000403d0b in main (argc=1, argv=0x7fffffffe448) at InvertIndexGen.cpp:47
(gdb)
map M=mainMapIt->second;//这是174号线。
做一份你的第二份

map<int,int>& M = mainMapIt->second; // THIS IS LINE 174.
map&M=mainMapIt->second;//这是174号线。
至少有助于避免此副本。

map M=mainMapIt->second;//这是第174行。

此行将导致不必要的映射复制和内存分配。
更改参考将有所帮助。

map&M=mainMapIt->second;//这是第174行。

您应该检查流程限制(使用
ulimit
命令从全局限制开始)。内存可能会受到某种限制。最大大小纯粹是一个理论值,与实际内存量无关。你应该更深入地挖掘你的内存使用情况,比较rss、swap、vsize进程,调查可能的ulimit和新的实际值,当然查看抛出的源代码也不会有什么坏处。下面是我的ulimit:-bash-3.2$ulimit-a
核心文件大小
(blocks,-c)0
数据段大小
(kbytes,-d)无限<代码>调度优先级<代码>(-e)0<代码>文件大小(块,-f)无限<代码>挂起信号(-i)1056768<代码>最大锁定内存(kbytes,-l)32<代码>最大内存大小(kbytes,-m)无限的
打开文件
(-n)1024
管道大小
(512字节,-p)8
POSIX消息队列
(字节,-q)819200
实时优先级
(-r)0
堆栈大小
(kbytes,-s)10240
cpu时间
(秒,-t)无限<代码>最大用户进程数
(-u)1056768<代码>虚拟内存
(千字节,-v)无限<代码>文件锁
(-x)无限我已使用ulimit更新了问题
map<int,int> M = mainMapIt->second; // THIS IS LINE 174.
-bash-3.2$ 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) 1056768
max locked memory       (kbytes, -l) 32
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) 10240
cpu time               (seconds, -t) unlimited
max user processes              (-u) 1056768
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited
map<int,int> M = mainMapIt->second; // THIS IS LINE 174.
map<int,int>& M = mainMapIt->second; // THIS IS LINE 174.