EDITED
The code was compiled with Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86 in VS2010 as 32bit and runs on Win7 64bit machine.
Program with empty main body takes 1KB of memory.
The sizeof(Bag)=32. (=16 bytes of internal buffer for short strings + 4 byte of pointer to memory if string bigger than 16 bytes is added + other internal stuff of string).
After reserving 200,000 elements the vector v takes 6400KB of memory. Thus, total memory until now is 7400KB.
What I don't understand is why after inserting 100,000 elements (which is smaller than reserved capacity of v) into v the memory usage increases to 14,800KB. If I replace string with int so the total usage memory will be as it supposed to be 1,800KB (= 1000KB + 200*4B)
1. struct Bag
2. {
3. string s;
4. };
5. vector<Bag> v;
6. v.reserve(200000);
7. for(int i = 0; i < 100000; ++i)
8. {
9. v.push_back(Bag());
10. }
Task Manager. I know that maybe this is not the most accurate tool, but it show very deterministic memory behavior on various machines (with same architecture of course).push_back()calls with a singleresize(100000)?