If you let the program below run undefinitely, it eventually uses up all RAM and the OS starts swapping (it took ~5 minutes to take up 64GB in my workstation). If it is true what has been answered here: When does the heap memory actually get released?, then why does the OS go swap instead of reclaiming supposedly unused RAM?
As you can see in the code below, the large vector is supposedly to be freed by the end of each loop (goes out of scope). So, I expected the program to occupy the same ammount of RAM all the time.
OBS: If you have less memory, you can set the nIter
number to a lower figure.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int nIter = 700000000;
while(true){
std::vector< std::vector<double> > data;
data.reserve(nIter);
std::vector<double> dataLine( 7, 0.0 );
for( int i = 0; i < nIter; ++i ){
data.push_back( std::vector<double>() );
data.back() = dataLine;
}
} //End of scope.
return 0;
}