免费非常慢 - 看起来像一个bug [英] free very very slow - seems like a bug

查看:73
本文介绍了免费非常慢 - 看起来像一个bug的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

/ *

以下简单的控制台应用程序需要很长时间(> 7秒)才能使用免费取消分配内存。 如果m和n都更改为2000,那么取消分配是以毫秒为单位。

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.

下面的问题使用大约3 GB的内存并且已经在两台计算机上的Visual Studio 2017 Professional上进行了测试两者都有32 GB的内存。

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>

#include <malloc.h>

int main( int argc,char * argv [])

{

     int m = 3000; //当更改为2000时,则会立即取消分配

     int n = 3000; //当更改为2000时,则会立即取消分配

     int kn = 30;

     double * * * a =(double ***)malloc(kn * sizeof(double **));

     for(int k = 0; k< kn; k ++){

   &NBSP;&NBSP;&NBSP;   a [k] =(double **)malloc(m * sizeof(double *));

   &NBSP;&NBSP;&NBSP;   for(int i = 0; i< m; i ++){

   &NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;   a [k] [i] =(double *)malloc(n * sizeof(double));

   &NBSP;&NBSP;&NBSP;  }

    }

     for(int k = 0; k< kn; k ++){

   &NBSP;&NBSP;&NBSP;   //以下循环需要约0.25秒才能执行

   &NBSP;&NBSP;&NBSP;   for(int i = 0; i< m; i ++){

   &NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP; &NBSP;自由(A [k]的[I]);

   &NBSP;&NBSP;&NBSP;  }

   &NBSP;&NBSP;&NBSP;  免费(a [k]);

    }

     free(a);

}

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][i] = (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][i]);
        }
        free(a[k]);
    }
    free(a);
}

推荐答案

我没有看到。

调试(非优化)构建总共需要1.2秒才能完成:

The debug (non optimised) build takes a total of 1.2s to complete:

发布(优化)版本需要不可记录的时间才能完成:

The release (optimised) build takes an unrecordable amount of time to complete:

这是添加了wprintf语句,因此内存分配有副作用,因此编译器赢了'优化整个主体。

This is with an added wprintf statement so that the memory allocation has a side effect so the compiler won't optimise the entire body of main away.

这是使用Visual Studio 2017 15.9.11,Visual Studio 2019 16.0.3和Visual Studio 2019 16.1预览2测试的。所有测试都是在Windows 10 1903。

This was tested with Visual Studio 2017 15.9.11, Visual Studio 2019 16.0.3 and Visual Studio 2019 16.1 preview 2. All tests were done on Windows 10 1903.


这篇关于免费非常慢 - 看起来像一个bug的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆