c++ - HDF5 H5Dread 内存使用过多

标签 c++ memory-management hdf5

我们曾遇到过因使用过多内存而导致程序崩溃的情况。这是 HDF5 1.8.9。

大多数时候,我们没有遇到问题,但有时对于较大的文件会发生以下情况:

在这个例子中,我有一个 325MB HDF5 文件,这导致 2GB 内存被用来读取它的一些值( HDF5 文件中的数据点,只有 400,001 个 double 值)。问题似乎是我们对 H5Dread 方法的使用引起的。知道我们在这里做错了什么吗?

导致问题的方法如下所示:

std::vector<double> Hdf5DataReader::GetUnlimitedDimensionValues() 
{ 
    // Define hyperslab in the dataset 
    hid_t time_dataspace = H5Dget_space(mUnlimitedDatasetId); 

    // Get the dataset/dataspace dimensions
    hsize_t num_timesteps;
    H5Sget_simple_extent_dims(time_dataspace, &num_timesteps, NULL);

    // Data buffer to return 
    std::cout << "Number of timesteps we are reserving memory for = " << num_timesteps << "\n"; 
    std::vector<double> ret(num_timesteps);         

    PrintMemoryUsage("made memory space"); 

    // Read data from hyperslab in the file into the hyperslab in memory 
    H5Dread(mUnlimitedDatasetId, 
            H5T_NATIVE_DOUBLE, 
            H5S_ALL, 
            H5S_ALL, 
            H5P_DEFAULT, 
            &ret[0]); 

    PrintMemoryUsage("read into memory space"); 

    H5Sclose(time_dataspace); 

    return ret; 
} 

输出是

Number of timesteps we are reserving memory for = 400001
made memory space: memory use = 43.5898 MB.
read into memory space: memory use = 2182.4 MB.

(使用这段代码来诊断分配给程序的内存量 - 这看起来合理吗?:

#include <unistd.h>
#include <sys/resource.h>

void PrintMemoryUsage(const std::string& rPrefix)
{
    struct rusage rusage;
    getrusage( RUSAGE_SELF, &rusage );

    double max_res = (double)(rusage.ru_maxrss)/(1024);// Convert KB to MB
    std::cout << rPrefix << ": memory use = " << max_res <<  " MB.\n";
}

)

最佳答案

Yossarian 的评论包含线索 - 虽然我们检查了主数据集的分块,但内存爆炸实际上来自仅包含时间值的相关一维数据集。后一个数据集的 block 大小为 1。增加 block 大小解决了问题。

关于c++ - HDF5 H5Dread 内存使用过多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18466691/

相关文章:

访问 Xarray 数据集时出现 HDF5 警告

c++ - C++ 中的 if 语句行为真的很奇怪

c++ - 可能的 ComputeCPP SYCL 错误读取嵌套缓冲区

c++ - MyClass::metaobject VS2005 的 Qt 链接问题

java - 我如何找出 Tomcat 使用这么多内存的原因并停止它?

python-2.7 - 将 HDF 文件加载到 Python Dask DataFrame 列表中

arrays - 如何在 NumPy 中将 HDF5 二维数组转换为一维数组?

c++ - ld : 4 duplicate symbols for architecture x86_64

linux - 高内存架构

c - 分配的内存块的簿记是多余的吗?