3

I have been noticing that my programs' memory usage keeps increasing pointlessly at times. Specially when i am using cvWaitKey(0), my program tends to shoot memory usage in 10 seconds to a huge size.

Is there a fix for this, or this is an OpenCv bug?

I have a simple function called Show_Image, to we which have set a callback function to get the pixel values:

void Show_Image(IplImage *img)
{
    cvNamedWindow("IMAGE_WINDOW", CV_WINDOW_AUTOSIZE); 
    cvSetMouseCallback("IMAGE_WINDOW", GETPIXEL , (void*)img);
    cvShowImage("IMAGE_WINDOW", img );
    cvWaitKey(0);
    cvDestroyWindow("IMAGE_WINDOW");
}
Expert Novice
  • 1,943
  • 4
  • 22
  • 47
  • Post a minimal complete program that demonstrates the problem (http://sscce.org). Your current description is far too vague to be investigated by anything but guesswork and prior experience. – Mankarse Oct 31 '11 at 02:37
  • @Mankarse - posted relevant code... please check. – Expert Novice Oct 31 '11 at 02:43

1 Answers1

2

It seems there is no error and no workaround for this. When you call cvWaitkey(), the function processes all the windows message queue. And because you have a mouse callback, it always processes something there. This, combined with the weak capability of the system to show the real memory usage for a process, can give you a false leak alarm.

However, to find the source of the problem, add in your program a line of code, like below

getchar();

Wait for the process memory to increase, then press any key to get out of the message loop in cvWaitKey(), and then wait for a minute in the getchar() function. If the memory did not go down in that minute, it may be a leak. Feel free to fill a ticket in the openCV bug tracker https://code.ros.org/trac/opencv/wiki

Also try different methods to measure the increase in memory: How to measure actual memory usage of an application or process? or Tracking CPU and Memory usage per process

Community
  • 1
  • 1
Sam
  • 19,708
  • 4
  • 59
  • 82