Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux C++ program crashes with St9bad_alloc after map gets very huge

I am running a C++ program that involving building inverted index on red hat linux 64 bits. My invert index is defined as map<unsigned long long int, map<int,int> > invertID; and I got this error where it crashes randomly, with what(): St9bad_alloc.Each time of the crash is different. Sometimes, I got 100,000,000 keys and it's still running a while more. Sometimes, about 80,000,000 keys and it already yell out the error.

Googling around, I found that this error may come from new, but taking a look at my code, I am not using any new keyword, yet, I have such memory allocation with map. I keep on inserting in the key/value pair in each iteration. So I decided some experiment with try catch statement.

In fact, here is the critical part of the code and output:

    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;
        }
    }

Output:

...
CAUGHT 2: invertID[21959247897][3856
CAUGHT 2: invertID[38022506156][3856
CAUGHT 2: invertID[29062506144][3856
terminate called after throwing an instance of 'std::bad_alloc'
  what():  St9bad_alloc

I see that when I tried to insert new key, the error is thrown. However, I got a bit more surprise that St9bad_alloc is still being thrown after I cover the key insertion part with try catch block. I did a little backtrace and here is the result:

(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)

At #14, InvertIndexGen.cpp:174, in my code above, this is where it crashed:

map<int,int> M = mainMapIt->second; // THIS IS LINE 174.

It seems that when I call ->second, a copy of the respective map has to be created. This should be the reason of St9bad_alloc as well.

But in this case, is there anything I can do here? After all, invertID.max_size() return 18446744073709551615, and I am using about 100 million keys only. I also see it from top, that my program uses only 10% of memory. (we got 128GB RAM)

What are some of the measures I should use against this error? I see some of my senior colleagues are doing this as well, and they report that when their invert index starts to grow more than 70-80% of memory in top, the program starts to go haywire. But my program uses only 10%, so what's going on here? What are some of the things we can do to prevent this error?

EDIT: some comments suggest me to check with ulimit, so here it is:

-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
like image 372
Karl Avatar asked Feb 21 '26 07:02

Karl


1 Answers

map<int,int> M = mainMapIt->second; // THIS IS LINE 174.

does a copy of your second.

map<int,int>& M = mainMapIt->second; // THIS IS LINE 174.

would at least help to avoid this copy.

like image 141
Totonga Avatar answered Feb 23 '26 19:02

Totonga