如何在Visual Studio C ++中增加内存限制 [英] how to increase memory limit in Visual Studio C++

查看:1161
本文介绍了如何在Visual Studio C ++中增加内存限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要帮助.在Windows-Visual Studio上运行C ++代码时,我遇到了问题.

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

当我在Linux环境中运行该代码时,对我能够动态分配的内存没有限制(直到RAM中可用的大小).

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).

但是在VS Compiler上,它不允许我创建超出限制大小的数组. 我已经尝试了/F选项和20-25的Google链接来增加内存大小,但是它们似乎并没有太大帮助.

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.

我目前只能在3GB可用空间中分配大约100mb.

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

如果在Windows中有解决方案,而在Visual Studio的编译器中没有,我会很高兴听到,因为我有CUDA TeslaC2070卡,由于我想运行CUDA/,事实证明在Windows上它几乎没有用. Windows环境下的C ++代码.

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.

这是我的代码.当LENGTH> 128时,它会失败(没有图像640x480pngs.每个图像都小于0.5mb.我还通过计算OpenCV和我使用的数据结构和类型,计算了它所需的近似内存大小,但它仍然小于2gb) . stackoverflow异常.与动态分配相同.我已经最大化了堆和栈的大小.

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;
}

推荐答案

问题是您要在堆栈中的主函数中分配一个巨大的多维数组(双a [..] [..] [..] ).不要在堆栈上分配这么多的内存.使用malloc/new在堆上进行分配.

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.

这篇关于如何在Visual Studio C ++中增加内存限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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