Pregunta

I have a general kind of question regarding globally declared arrays. I came across this issue. I cannot run my application several times at the same time anymore, I get a message (in German):

die Auslagerungsdatei ist zu klein, um diesen Vorgang auszuführen (which translates to something like this: the storage file is too small to execute this process)

my question is: is this due to too many arrays declared as global or would the problem have to be related to something else, perhaps too many IplImages (I have declared hundreds of them as global) ? I have noticed that there seems to be a limit for the size of arrays in general. However, declaring several smaller arrays that sum up to much bigger size works - or at least up to a certain limit as well ... hence the problem above? What can I do to avoid this?

programming language: c++, vs 2008, console app

¿Fue útil?

Solución

It doesn't matter if your variables are global, local, class members, etc, for your question. The important fact is the lifespan of those variables. You can face memory problems if you load a lot of images at the same time. For example (let me use the C++ syntax with cv::Mat instead of IplImage -you should as well-):

vector<cv::Mat> images;
for(int i = 0; i < 1000000; ++i)
  images.push_back(cv::imread("image.png"));

This piece of code will allocate one million matrices with one million images. Of course, this is going to take a huge amount of memory and, probably, your program will crash.

Note that in C++, you can control the lifespan of your non-static variables by limiting their scope:

for(int i = 0; i < 1000000; ++i)
{
  cv::Mat im = cv::imread("image.png");
} 
// im does not exist anylonger

In this code, you load an image one million times, but you only take memory for one image, because at the end of each iteration, im is deallocated. Note that in C, with the IplImage, you must deallocate the images by yourself with cvReleaseImage.

So, is it important that your variables are global? No, unless you don't control their lifespan and you keep them allocated all the time, even if you don't use them.

Update: You can have local static variables whose lifespan is the entire time your app is running. These are not global (do not have a global scope) but they take memory all the time:

void someFunction()
{
  static cv::Mat images[1000000];
  // one millon of matrices are allocated all the time, even when someFunction ends
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top