【发布时间】:2014-11-24 10:35:37
【问题描述】:
请参阅下面的代码:
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;
}
我使用 while(1) 循环检查 htop 工具中的内存使用情况,r 可能使用 5GB RES,但在打印 load done 之后,RES 仍然需要 5GB。有什么问题?
【问题讨论】:
-
vector 将内存返回给分配器,但分配器可能不会将其返回给操作系统,它不是必需的。
-
请不要在您的问题上附加随机标签。 [C] 与此无关;
std::vector<>根本不是 C 类型。
标签: c++ memory stl heap-memory htop