how to increase memory limit in Visual Studio C++

2020-04-16 03:39发布

问题:

Need Help.I'm stuck at a problem when running a C++ code on Windows- Visual Studio.

When I run that code in Linux environment, there is no restriction on the memory I am able to allocate dynamically(till the size available in RAM).

But on VS Compiler, it does not let me create an array beyond a limited size. I've tried /F option and 20-25 of google links to increase memory size but they dont seem to help much.

I am currently able to assign only around 100mb out of 3gb available.

If there is a solution for this in Windows and not in Visual Studio's compiler, I will be glad to hear that as I have a CUDA TeslaC2070 card which is proving to be pretty useless on Windows as I wanted to run my CUDA/C++ code on Windows environment.

Here's my code. it fails when LENGTH>128(no of images 640x480pngs. less than 0.5mb each. I've also calculated the approximate memory size it takes by counting data structures and types used in OpenCV and by me but still it is very less than 2gb). stackoverflow exception. Same with dynamic allocation. I've already maximized the heap and stack sizes.

#include "stdafx.h"

#include <cv.h>
#include <cxcore.h>
#include <highgui.h>

#include <cuda.h>
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#define LENGTH 100
#define SIZE1 640   
#define SIZE2 480
#include <iostream>
using namespace std;


__global__ void square_array(double *img1_d, long N)
{

int idx = blockIdx.x * blockDim.x + threadIdx.x;

img1_d[idx]= 255.0-img1_d[idx];

}


int _tmain(int argc, _TCHAR* argv[])
{   
        IplImage *img1[LENGTH];
        // Open the file.
        for(int i=0;i<LENGTH;i++)
        {   img1[i] = cvLoadImage("abstract3.jpg");}




    CvMat *mat1[LENGTH];
    for(int i=0;i<LENGTH;i++)
    {
        mat1[i] = cvCreateMat(img1[i]->height,img1[i]->width,CV_32FC3 );
        cvConvert( img1[i], mat1[i] );
    }


    double a[LENGTH][2*SIZE1][SIZE2][3];

    for(int m=0;m<LENGTH;m++)
    {
    for(int i=0;i<SIZE1;i++)
    {
         for(int j=0;j<SIZE2;j++)
         {
               CvScalar scal = cvGet2D( mat1[m],j,i);
               a[m][i][j][0] = scal.val[0];
               a[m][i][j][1] = scal.val[1];
               a[m][i][j][2] = scal.val[2];

               a[m][i+SIZE1][j][0] = scal.val[0];
               a[m][i+SIZE1][j][1] = scal.val[1];
               a[m][i+SIZE1][j][2] = scal.val[2];

            }

    }   }

//cuda
double *a_d;
int N=LENGTH*2*SIZE1*SIZE2*3;

cudaMalloc((void **) &a_d, N*sizeof(double));
cudaMemcpy(a_d, a, N*sizeof(double), cudaMemcpyHostToDevice);
int block_size = 370;
int n_blocks = N/block_size + (N%block_size == 0 ? 0:1);
cout<<n_blocks<<block_size;
square_array <<< n_blocks, block_size >>> (a_d, N);
cudaMemcpy(a, a_d, N*sizeof(double), cudaMemcpyDeviceToHost);

//cuda end



char name[]= "Image: 00000";
name[12]='\0';
int x=0,y=0;
for(int m=0;m<LENGTH;m++)
{
for (int i = 0; i < img1[m]->width*img1[m]->height*3; i+=3) 
{ 
 img1[m]->imageData[i]= a[m][x][y][0];
 img1[m]->imageData[i+1]= a[m][x][y][1];
 img1[m]->imageData[i+2]= a[m][x][y][2];

  if(x==SIZE1)
  {
  x=0;
  y++;
  }
x++;
}

    switch(name[11])
    {
        case '9': switch(name[10])
        {
            case '9': 
            switch(name[9])
            {
                case '9': name[11]='0';name[10]='0';name[9]='0';name[8]++;
                break;
                default : name[11]='0';
                name[10]='0';
                name[9]++;
            }break;

            default : name[11]='0'; name[10]++;break;
            }

            break;
        default : name[11]++;break;
    }
        // Display the image.
        cvNamedWindow(name, CV_WINDOW_AUTOSIZE);
        cvShowImage(name,img1);
    //cvSaveImage(name ,img1);
}
        // Wait for the user to press a key in the GUI window.
        cvWaitKey(0);

        // Free the resources.
        //cvDestroyWindow(x);
        //cvReleaseImage(&img1);
    //cvDestroyWindow("Image:");
        //cvReleaseImage(&img2);

        return 0;
}

回答1:

The problem is that you are allocating a huge multidimensional array on the stack in your main function (double a[..][..][..]). Do not allocate this much memory on the stack. Use malloc/new to allocate on the heap.