我在使用VS2012编译器时遇到了一个奇怪的问题,这个问题在GCC中似乎没有出现过。释放内存的过程需要花费数分钟而不是几秒钟。有人对此有什么想法吗?在调试过程中,发现在调用RtlpCollectFreeBlocks()时会明显卡顿。我在debug和release模式下都遇到了这个问题。我的操作系统是Windows 7 32位,但我在64位的7上也遇到了同样的问题。
#include "stdafx.h"
#include <iostream>
#include <stdint.h>
#include <cstdlib>
#define SIZE 500000
using namespace std;
typedef struct
{
uint32_t* thing1;
}collection;
/*
* VS2012 compiler used.
* Scenarios:
* 1) Don't allocate thing1. Program runs poorly.
* 2) Allocate thing1 but don't delete it. Program runs awesome.
* 3) Allocate thing1 and delete it. Program runs poorly.
*
* Debug or Release mode does not affect outcome. GCC's compiler is fine.
*/
int _tmain(int argc, _TCHAR* argv[])
{
collection ** colArray = new collection*[SIZE];
for(int i=0;i<SIZE;i++)
{
collection * mine = new collection;
mine->thing1 = new uint32_t; // Allocating without freeing runs fine. Either A) don't allocate or B) allocate and delete to make it run slow.
colArray[i] = mine;
}
cout<<"Done with assignment\n";
for(int i=0;i<SIZE;i++)
{
delete(colArray[i]->thing1); // delete makes it run poorly.
delete(colArray[i]);
if(i > 0 && i%100000 == 0)
{
cout<<"100 thousand deleted\n";
}
}
delete [] colArray;
cout << "Done!\n";
int x;
cin>>x;
}