所以我这里有这个循环:
/* loop */
omp_set_dynamic(0);
#pragma omp parallel num_threads(1)
#pragma omp for ordered private(iterationPoints, normalizedPoints, grid)
for(iterations = maxIter; iterations >= minIter; iterations -= stepSizeIter){
/* calculate iterationPoints */
iterationPoints = (double **) malloc(iterations *sizeof(double *));
int i;
for(i = 0; i < iterations; i++){
iterationPoints[i] = (double *) malloc(DIMS *sizeof(double));
}
calcuTraj(iterationPoints,a,b,c,x,y,z,h,iterations, bounds, (iterations == maxIter));
/* normalize Data */
normalizedPoints = (double **) malloc(iterations *sizeof(double *));
for(i = 0; i < iterations; i++){
normalizedPoints[i] = (double *) malloc(DIMS * sizeof(double));
}
#pragma omp ordered
printf("%d %f %f %f %f %f %f\n",iterations,bounds[0][0] ,bounds[0][1], bounds[1][0], bounds[1][1], bounds[2][0], bounds[2][1]);
normalize(iterationPoints, normalizedPoints, bounds, iterations);
/* creating 3D Array for the grid of boxes in space*/
/* setting minimum for sidelength of the grid */
double minGridSideLength = 1;
/* calculating array size */
int boxesPerDim = ceil(minGridSideLength/epsion) +1;
//printf("boxesPerDim: %d \n", boxesPerDim);
/* create grid array */
grid = (bool ***) malloc(boxesPerDim *sizeof(bool **));
int j_X, j_Y;
for(j_X = 0; j_X < boxesPerDim; j_X++){
grid[j_X] = (bool **) malloc(boxesPerDim *sizeof(bool *));
for(j_Y = 0; j_Y < boxesPerDim; j_Y++){
grid[j_X][j_Y] = (bool *) calloc(boxesPerDim,sizeof(bool *));
}
}
/* count hitted boxes */
int boxesHit = boxCount(normalizedPoints, grid, iterations, epsion, boxesPerDim);
#pragma omp ordered
fprintf(file,"%d %d\n", iterations, boxesHit);
//printf("%d \n",boxesHit);
/* free storage */
free(iterationPoints);
free(grid);
free(normalizedPoints);
}
我将线程数设置为 1,因此不存在会导致高迭代时内存爆炸的多线程。因此,在此配置中,循环将一步一步运行。函数“calculatedTraj(..)”计算“迭代点”。如果我从 minIter = 20*10^6 到 maxIter = 50*10^6 迭代,步长 = 10*10^6,我的内存就会爆炸,进程就会被杀死。但是如果我运行 minIter = maxIter = 50*10^6 的程序,它就可以工作并且内存也很好。 那么为什么不能用 for 循环来做到这一点,因为在每个循环部分结束时我都会释放分配的内存,那么为什么 ram 会爆炸呢?
最佳答案
grid[j_X] = (bool **) malloc(boxesPerDim *sizeof(bool *));
grid[j_X][j_Y] = (bool *) calloc(boxesPerDim,sizeof(bool *));
您还没有释放这些。
类似于拥有一些结构:
struct whatever {
int something
char *stringly
};
struct whatever *whtvr = malloc(sizeof(struct whatever));
whtvr->stringly = malloc(strlen("whatever") + 1);
...
在放弃结构体的内存之前,您必须释放字符串的内存。
free(whtvr->stringly);
free(whtvr);
关于c - 由于循环,RAM 正在爆炸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48136930/