title: Out of memory error for a C++ executable
body: <p>I am trying to debug a weird Out Of Memory Exception. I have a C++ executable, that among other things, uses OpenCV and checks for Structural Similarity of two images.</p>
  
  <p>Code:</p>
  
  <pre><code>Scalar getMSSIM( const Mat&amp; i1, const Mat&amp; i2)
  {
  const double C1 = 6.5025, C2 = 58.5225;
  /***************************** INITS **********************************/
  int d     = CV_32F;
  
  Mat I1, I2;
  i1.convertTo(I1, d);           // cannot calculate on one byte large values
  i2.convertTo(I2, d);
  
  Mat I2_2   = I2.mul(I2);        // I2^2
  Mat I1_2   = I1.mul(I1);        // I1^2
  Mat I1_I2  = I1.mul(I2);        // I1 * I2
  
  /*************************** END INITS **********************************/
  
  Mat mu1, mu2;   // PRELIMINARY COMPUTING
  GaussianBlur(I1, mu1, Size(11, 11), 1.5);
  GaussianBlur(I2, mu2, Size(11, 11), 1.5);
  
  Mat mu1_2   =   mu1.mul(mu1);
  Mat mu2_2   =   mu2.mul(mu2);
  Mat mu1_mu2 =   mu1.mul(mu2);
  
  Mat sigma1_2, sigma2_2, sigma12;
  
  GaussianBlur(I1_2, sigma1_2, Size(11, 11), 1.5);
  sigma1_2 -= mu1_2;
  
  GaussianBlur(I2_2, sigma2_2, Size(11, 11), 1.5);
  sigma2_2 -= mu2_2;
  
  GaussianBlur(I1_I2, sigma12, Size(11, 11), 1.5);
  sigma12 -= mu1_mu2;
  
  ///////////////////////////////// FORMULA ////////////////////////////////
  Mat t1, t2, t3;
  
  t1 = 2 * mu1_mu2 + C1;
  t2 = 2 * sigma12 + C2;
  t3 = t1.mul(t2);              // t3 = ((2*mu1_mu2 + C1).*(2*sigma12 + C2))
  
  t1 = mu1_2 + mu2_2 + C1;
  t2 = sigma1_2 + sigma2_2 + C2;
  t1 = t1.mul(t2);               // t1 =((mu1_2 + mu2_2 + C1).*(sigma1_2 + sigma2_2 + C2))
  
  Mat ssim_map;
  divide(t3, t1, ssim_map);      // ssim_map =  t3./t1;
  
  Scalar mssim = mean( ssim_map ); // mssim = average of ssim map
  return mssim;
  </code></pre>
  
  <p>}</p>
  
  <p>My matrices are huge - 3500 x 2500. I have a 32bit RHEL5 with 4GB RAM.
  When I run this executable as such from the command line, it runs fine. 
  But when I am calling this executable through apache-commons Executor() from a Java program, as an asynchronous process, it throws an Out Of Memory exception. Also I tried running this program under valgrind and it throws the same out of memory exception. </p>
  
  <p>This function is called a fairly large number of times(around 2000 times). When run under valgrind and simultaneously monitor the memory usage with top, I see that after each execution of this function, the physical memory occupied kept increasing, ultimately leading to OutOfMemory exception. But when run normally, after each execution of this function, the memory consumption returns back to normal. This almost leads me to conclude that somehow, when run under valgrind, the function stack still persists without getting properly deallocated.</p>
  
  <p>What is causing this behavior?</p>
  
  <p><strong>UPDATE:</strong> Valgrind version 3.9.0 does not throw OutOfMemory exception. Its only with Valgrind version 3.5.0</p>