I'm developing a program which needs to be memory efficient. I have used std::vector
in my program to store a large number of elements. But, I noticed that the program's memory size grows exponentially when the number of elements are chosen large.
For example I wrote the following code:
#include <iostream>
#include <vector>
using namespace std;
int main(){
int vecSize;
cin >> vecSize;
vector<double> a(vecSize);
return 0;
}
Then I monitored the memory consumption using the gnu time command like this:
/usr/bin/time -f "Mem: %M" a.out
This is the memory results I have got for different vector sizes:
VecSize MemUsage
10: 4720 KB
100: 4720 KB
1000: 4736 KB
10000: 5024 KB
100000: 7744 KB
1000000: 35872 KB
10000000: 317120 KB
Does anybody know why the memory usage is growing so much fast when the number of elements are chosen more than 100000?