我正在创建一个简单的计算系统,可以进行一些计算并存储所有结果。我需要存储大约36000个时间步,空间粒度为100万个元素。似乎我需要分配
36000 * 1000000 * sizeof(double) = 237.191 MB
,这在具有16 GB RAM内存的PC上应该不是问题。然而,在522时的步长,我得到bad_alloc
。我的计算是错误的,还是Windows上有一些非常严格的内存管理规则?
代码是:
int timeSteps=std::ceil(3600.*10./1.); //10 hours with 1 second step
int Nx=1000, Ny=1000; //grid resolution in x and y direction
cout<<"Time steps "<<timeSteps<<endl;
cout<<"Grid dimensions Nx*Ny "<<Nx<<"*"<<Ny<<endl;
cout<<"Total memory used: "<<(double)(Nx*Ny*timeSteps*sizeof(double))/1000000.<<" MB"<<endl;
std::vector<double*> timeStepSolution;
timeStepSolution.reserve(timeSteps);
for (int i=0;i<timeSteps;i++)
{
cout<<i<<endl;
double *p=new double[Nx*Ny];
timeStepSolution.push_back(p);
}
for (int i=0;i<timeSteps;i++)
{
for (int j=0;j<Nx*Ny;j++)
timeStepSolution[i][j]=rand() % 100;
}
for (int i=0;i<timeSteps;i++)
{
delete[] timeStepSolution[i];
}
timeStepSolution.clear();
答案 0 :(得分:4)
36000 * 1000000 * 8 = 288000000000 bytes
这是:
288000000000 / 1024 = 2812500000.00 kilobytes
281250000.00 / 1024 = 274658.20 megabytes
274658.20 / 1024 = 268.22 gigabytes
你的计算中slightly off。