c++ - vector hold memory even swap with an another empty vector -


i make tiny experiment, code following :

    vector<char> *p = new vector<char>[1024];     (size_t = 0; < 1024; i++)      {         (*p++).resize(1024 * 1024);//alloc 1 g memory      }      sleep(5);     cout << "start clear" << endl;     (size_t = 0; < 1024; i++)     {         vector<char> tmp;         tmp.swap(*p++);     }     delete [] p;      cout << "clear over!" << endl;     sleep (5); 

//here, memory still 1g, why ? thank much.

in implementations, memory isn't returned os immediately, rather put "free list", acquiring memory os way more expensive walking such free list. that's why still see 1gig of memory, wherever check that.

also, in code, don't see reset p after reserving vectors, swap empty vector uninitialized memory doesn't belong you.


Comments

Popular posts from this blog

c++ - Is it possible to compile a VST on linux? -

c# - SharpSVN - How to get the previous revision? -

php cli reading files and how to fix it? -