title: EMGU -" Exception of type 'System.OutOfMemoryException' was thrown."
body: <p>I've got the message above, while I'm trying to run my algorithm on more than 26 frames.</p>
  
  <p>My program is in C#, with my experience with OpenCV in java(Android) I know that I need to release the matrix, do I need to do it here also?</p>
  
  <p>this is the part with the problem:</p>
  
  <pre><code> private void toolStripButton2_Click(object sender, EventArgs e)
          {  
              for (int i = 0; i &lt; FRAME_SIZE; i++)
              {
                  ColorImage = _Capture.QueryFrame();
                  if (ColorImage != null)
                  {
                      GrayImage = ColorImage.Convert&lt;Gray, float&gt;();
                      toolStripProgressBar1.Value = i;
                      if (i == 0)
                      {
                          Max_G2 = new Image&lt;Gray, float&gt;(GrayImage.Width, GrayImage.Height);
                          mainimage = new Image&lt;Bgr, byte&gt;(GrayImage.Width, GrayImage.Height);
                      }
                      FindMax(GrayImage.Copy());
                  }
                  else
                  {
                      toolStripStatusLabel1.Text = "The video is too short!";
                      break;
                  }
              }
          }
  
  private void FindMax(Image&lt;Gray, float&gt; CurrGray)
          {
  
              Image&lt;Gray, float&gt; TempImg = new Image&lt;Gray, float&gt;(CurrGray.Width, CurrGray.Height);
  
              Matrix&lt;float&gt; kernel1 = new Matrix&lt;float&gt;(new float[1, 2] { { -1, 1 } });
              Matrix&lt;float&gt; kernel2 = new Matrix&lt;float&gt;(new float[2, 1] { { -1 }, { 1 } });
  
              Point anchor = new Point(0, 0);
  
              CvInvoke.cvFilter2D(CurrGray, TempImg, kernel1, anchor);
              CvInvoke.cvFilter2D(CurrGray, CurrGray, kernel2, anchor);
              TempImg = TempImg.Pow(2);
              CurrGray = CurrGray.Pow(2);
  
              CurrGray = CurrGray.Add(TempImg);
              CurrGray = CurrGray.Pow(0.5);
  
              Max_G2._Max(CurrGray);
          }
  </code></pre>
  
  <p>1 more thing, I already tried to dispose all the matrix and images, but it doesn't work for me.
  What do I miss here? </p>
  
  <p>Thanks!</p>
  
  <p><strong>EDIT 1: ( Code with dispose)</strong></p>
  
  <pre><code>private void toolStripButton2_Click(object sender, EventArgs e)
          {
              for (int i = 0; i &lt; FRAME_SIZE; i++)
              {
  
                  ColorImage = _Capture.QueryFrame();
                  if (ColorImage != null)
                  {
                      GrayImage = ColorImage.Convert&lt;Gray, float&gt;();
  
                      toolStripProgressBar1.Value = i;
                      if (i == 0)
                      {
                          Max_G2 = new Image&lt;Gray, float&gt;(GrayImage.Width, GrayImage.Height);
                          TempImg = new Image&lt;Gray, float&gt;(GrayImage.Width, GrayImage.Height);
                          mainimage = new Image&lt;Bgr, byte&gt;(GrayImage.Width, GrayImage.Height);
                      }
                      FindMax(GrayImage);
  
                      ColorImage.Dispose();
                      ColorImage = null;
                      GrayImage.Dispose();
                      GrayImage = null;
  
                      Thread.Sleep(100);
                  }
                  else
                  {
                      toolStripStatusLabel1.Text = "The video is too short!";
                      break;
                  }
              }
  
          }
  
  private void FindMax(Image&lt;Gray, float&gt; CurrGray)
          {
  
  
  
              Matrix&lt;float&gt; kernel1 = new Matrix&lt;float&gt;(new float[1, 2] { { -1, 1 } });
              Matrix&lt;float&gt; kernel2 = new Matrix&lt;float&gt;(new float[2, 1] { { -1 }, { 1 } });
  
              Point anchor = new Point(0, 0);
  
              CvInvoke.cvFilter2D(CurrGray, TempImg, kernel1, anchor);
              CvInvoke.cvFilter2D(CurrGray, CurrGray, kernel2, anchor);
  
              TempImg.Pow(2);
              CurrGray.Pow(2);
  
              CurrGray.Add(TempImg);
              CurrGray.Pow(0.5);
  
              Max_G2._Max(CurrGray);
  
              CurrGray.Dispose();
              CurrGray = null;
  
          }
  </code></pre>