free very very slow - seems like a bug

  • Thread starter Thread starter Alan9837
  • Start date Start date
A

Alan9837

Guest
/*

The following simple console application takes a long time (>7 seconds) to de-allocate memory using free. If m and n are both changed to 2000 then the de-allocations is in the milliseconds.

Problem below uses approximately 3 Gbytes of memory and has been tested on Visual Studio 2017 Professional on two computers both with 32 Gbytes of memory.

*/

#include <malloc.h>

int main(int argc, char *argv[])
{
int m = 3000; // when changed to 2000 then de-allocation is instant
int n = 3000; // when changed to 2000 then de-allocation is instant
int kn = 30;
double * * * a = (double***) malloc(kn * sizeof(double**));
for (int k = 0; k < kn; k++) {
a[k] = (double**) malloc(m * sizeof(double*));
for (int i = 0; i < m; i++) {
a[k] = (double *) malloc(n * sizeof(double));
}
}
for (int k = 0; k < kn; k++) {
// The following loop takes ~0.25 seconds to execute
for (int i = 0; i < m; i++) {
free(a[k]);
}
free(a[k]);
}
free(a);
}

Continue reading...
 
Back
Top