【发布时间】:2018-06-04 09:57:40
【问题描述】:
我正在通过以下方式读取文件。
std::vector<std::string> urlList;
std::ifstream infile("top1m.txt");
std::string line;
std::cout << "Loading urls from file" << std::endl;
for (int offset = 0; offset < _urlCount; offset++) { //_urlCount=1000000
std::getline(infile, line);
if (!line.empty()){
if (line.back() == '\r')
line.erase(line.length() - 1, std::string::npos);
}
urlList.push_back(std::string("http://").append(line));
}
inflie.close();
top1m.txt 为 17 mb。 在阅读文件之前,我的 .exe 文件只有 6 mb。 我做错了什么,如何减少程序占用的内存?
【问题讨论】:
-
你如何检查你的进程使用了多少内存?
-
我在任务管理器里看到了。 @Someprogrammerdude
-
这与您的问题无关,但是当您想删除
std::string的最后一个字符时,只需说line.pop_back();就简单多了 -
1.众所周知,任务管理器对内存使用不可靠。 2. 如果没有其他程序在争夺资源,Windows 可能不会立即回收释放的内存。
-
任务管理器的
Working set列可能会引起有趣的阅读。它在Details选项卡中可用(作为一个选项)。投票赞成(叹气)。这个问题有什么问题?
标签: c++ file memory memory-management memory-leaks