See my code below:
void foo() {
std::ifstream f("data");
string line;
vector<string> r;
while(getline(f, line)) {
r.push_back(line);
}
f.close();
r.resize(0);
}
int main(int argc, char *argv[])
{
foo();
cout << "load done" << endl;
while(1) {;}
return 0;
}
I use while(1) loop to check out the memory usage in htop tool, r may use 5GB RES, but after load done printed, RES still take 5GB. What's the problem?
Resize doesn't guarantee that the underlying memory will be freed.
You should try with shrink_to_fit, which will reduce the capacity of the contained to fit its size.