硬盘和RAM上文件大小的差异
Difference in file size on hard disk and RAM
我有36MB的数据文件(文件中的每个值都是双类型)驻留在硬盘上。我的问题是,当我通过c++在RAM中读取这个文件时,将内容放在矩阵中(由boost库提供),它是否只占用36MB的RAM或不同?我的内存用完了吗?
原因是我在64位的ubuntu平台上有8 GB的RAM,我得到了糟糕的分配错误。同样的文件读取程序适用于小数据文件。
下面是加载(data real-sim)[https://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/binary.html]的代码片段。x和y分别是升压矩阵和矢量,在。h文件中声明为extern
。
void load_data(const char* filename)
{
ifstream in(filename);
string line;
int line_num = 0;
if (in.is_open()) {
while (in.good()) {
getline(in, line);
if (line.empty()) continue;
int cat = 0;
if (!parse_line(line, cat, line_num)) {
cout << "parse line: " << line << ", failed.." << endl;
continue;
}
y(line_num) = cat;
line_num += 1;
}
in.close();
}
}
bool debug = false;
using namespace boost::numeric::ublas;
vector<double> y(no_records);
matrix<double> x(no_records,no_features);
using namespace std;
template < class T>
void convert_from_string(T& value, const string& s)
{
stringstream ss(s);
ss >> value;
}
int get_cat(const string& data) {
int c;
convert_from_string(c, data);
return c;
}
bool get_features(const string& data, int& index, double& value) {
int pos = data.find(":");
if (pos == -1) return false;
convert_from_string(index, data.substr(0, pos));
convert_from_string(value, data.substr(pos + 1));
return true;
}
bool parse_line(const string& line, int& cat, const int line_num) {
if (line.empty()) return false;
size_t start_pos = 0;
char space = ' ';
while (true) {
size_t pos = line.find(space, start_pos);
if ((int)pos != -1) {
string data = line.substr(start_pos, pos - start_pos);
if (!data.empty()) {
if (start_pos == 0) {
cat = get_cat(data);
}
else {
int index = -1;
double v = 0;
get_features(data, index, v);
if (debug)
cout << "index: " << index << "," << "value: " << v << endl;
if (index != -1) {
index -= 1; // index from 0
x(line_num, index) = v;
}
}
}
start_pos = pos + 1;
}
else {
string data = line.substr(start_pos, pos - start_pos);
if (!data.empty()) {
cout << "read data: " << data << endl;
int index = -1;
double v = 0;
get_features(data, index, v);
if (debug)
cout << "index: " << index << "," << "value: " << v << endl;
if (index != -1) {
index -= 1; // index from 0
x(line_num, index) = v;
}
}
break;
}
}
return true;
}
我找到了罪魁祸首。bad allocation
错误的原因是我内存不足。事情是,我使用dense matrix
表示(由boost库提供)。因此,将大小为20000x40000
的矩阵存储为升压矩阵表示的密集矩阵将需要大小为6.4GB
的RAM。现在,如果RAM中没有那么多空间,就会出现错误的分配。
相关文章:
- 读取某些文件时出现分段错误,似乎与文件大小无关
- 构建挂起,即使是适度的文件大小
- 如何在Windows内核中获取文件大小
- 为什么 QFileSystemWatcher 会发出多个信号?而 QFileInfo 首次写入零文件大小
- 使用 C++ iOS::ate 获取错误的文件大小
- 相同的源代码,不同的可执行文件大小?
- 为什么未关闭的文件大小为 4 字节
- 读取二进制文件大小的短自上周以来停止工作
- 如何减小C++标准库libstdc++.so文件大小
- 文件大小和缓冲区过冲
- 如何从范围 v3 的istream_range中检索文件大小?
- 如何使用直接IO编写一个带有扩展文件大小的小文件
- 如何使用文件系统库获取"true"文件大小?(C++17)
- 如何限制文件大小,以便我的程序在变大之后创建一个新文件?并编辑新创建的文件的名称
- 如何使用C 17获取文件大小
- HttpQueryInfo 获取文件大小
- 二进制文件大小大于预期的 c++
- 如何计算预期的核心文件大小
- 硬盘和RAM上文件大小的差异
- 如何在windows7有限内存的电脑上读取大文件?文件大小为25GB,但RAM容量只有16GB