c - 内存不足杀死

标签 c matrix out-of-memory slurm

我在使用 slurm 集群创建太大的矩阵时遇到问题(内存不足被杀死)。我该如何解决这个问题? 下面的代码是关于分配矩阵的部分代码:

double  **matrix;
int rows = 30000;
int cols = 39996;
matrix = (double**)malloc(sizeof(double*)*rows);
for (int i = 0; i < rows; i++)
    matrix[i] = (double*)malloc(sizeof(double)*cols);

for(int i=0; i<rows; i++)
    for(int j=0; j<cols; j++)
        matrix[i][j] = 1;

这个值(行,列)是一个例子,因为我也可以有更大的值 相反,以下代码是有关释放的代码部分:

for (int i = 0; i < 30000; i++)
    free(matrix[i]);
free(matrix);

这是我的输出: Slurmstepd:错误:在步骤 98584.0 cgroup 中检测到 1 个 oom-kill 事件。您的某些进程可能已被 cgroup 内存不足处理程序终止。 srun:错误:lab13p1:任务 1:内存不足

最佳答案

  1. 将矩阵的声明更改为双指针(可能是拼写错误):
double  **matrix;
  • 您应该验证 malloc 函数的返回值,尤其是在矩阵太大的情况下。

  • 不要强制转换 malloc 函数。 Do I cast the result of malloc?

  • matrix = malloc(sizeof(double*)*rows);
    if(!matrix) {
       // handle the error
    }
    
    for (int i = 0; i < rows; i++) {
        matrix[i] = malloc(sizeof(double)*cols);
        if(!matrix[i]) {
           // handle the error
        }
    }
    
    

    关于c - 内存不足杀死,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62048131/

    相关文章:

    c - 是什么导致错误 "Cannot convert ' unsigned char'"?

    c - 以错误的方式调用函数(序列的总和)

    matlab - 如何找到结构体数组中的最大值

    c++ - 访问每个矩阵点

    python - 如何使用权重矩阵的最小二乘法?

    c# - 使用文件流写入字节通过控制台输出进度时出现Out of Memory异常

    java - 使用 XLConnect : run out of Java memory 将 Excel 文件读取到 R

    c - 为什么位字段的类型会影响包含结构的大小?

    将二叉树转换为链表

    java - 如何修复 java.lang.OutOfMemoryError : Java heap space error?