硬盘和RAM上文件大小的差异

Difference in file size on hard disk and RAM

本文关键字:文件大小 RAM 硬盘      更新时间:2023-10-16

我有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中没有那么多空间,就会出现错误的分配。