Out of memory kill

问题描述 投票:0回答:1

我在创建一个太大的矩阵时遇到了问题,该矩阵具有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:内存不足

c matrix out-of-memory slurm
1个回答
1
投票
  1. 将矩阵的声明更改为双指针(也许是错字):
double  **matrix;
  1. 您应该验证malloc函数的返回值,尤其是对于too big matrix

  2. 请勿转换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
    }
}

© www.soinside.com 2019 - 2024. All rights reserved.