Run out of ram C++

Let's see if I have this right.

You are producing:

  • 1 vector that holds:
  • 256 vectors that each hold
  • 256 vectors that each hold (65,536 in total)
  • 256 vectors that each hold (16,777,216 in total)
  • 256 shorts (4,294,967,296 in total, or 8,589,934,592 Bytes as you indicated)

I don't know the entire size of each vector itself, but probably well under 1k, so you're using less than 10 gig of memory.

However, that's a LOT going on. Is it really hanging, or is it just taking a very, very long time.

Some debug output periodically would help answer that.


Some tips (from the comments):

  1. Run an optimized build (-O3), this should speed up processing.

  2. Instead of push_back() of an empty vector in a loop, use resize(). This will prevent costly reallocation.

    So for example, replace

     while(matriz.size() < width)   //width es el tamaño de N
     {
         vector<vector<vector<short>>> aux;
         matriz.push_back(aux);
     }
    

    With

     matriz.resize(width);
    

    If you do still need to use push_back() in a loop, at least reserve() the capacity beforehand. This can again prevent costly reallocations. Reallocating a vector can briefly double the amount of memory that it would normally use.

  3. Use tools like top to watch memory and swap usage on the machine in real time. If you notice swap space used increasing, that means the machine is running out of memory.

Tags:

C++

Ram