shimat / opencvsharp

OpenCV wrapper for .NET
Apache License 2.0
5.22k stars 1.13k forks source link

Cv2.MinMaxLoc causes Memory leak #1642

Closed TakuNishiumi closed 5 months ago

TakuNishiumi commented 5 months ago

Summary of your issue

It looks like that the below code causes Memory leak. I tried ResourcesTracker, but does not work well for this problem.

Environment

What did you do when you faced the problem?

Example code:

for (int i = 0; i < 10000000; i++)
{
    double[] numbers = new double[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

    using (Mat mat1= Mat.FromArray(numbers))
    {
        Cv2.MinMaxLoc(mat1, out _, out _, out _, out _);
    }
}

Output:

Increase the Memory usage... Do you have any ideas to solve this?

shimat commented 5 months ago

(日本の方とお見受けして日本語で回答します。)

私の環境ではリークらしき状況は確認できませんでした。

一つだけ思い当るフシとして、以下のようなコードをお試しいただけますか。

for (long i = 0; i < 1000000000; i++)
{
    double[] numbers = new double[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

    using (Mat mat1 = Mat.FromArray(numbers))
    using (InputArray ia1 = mat1)
    {
        Cv2.MinMaxLoc(ia1, out _, out _, out _, out _);
    }
}

Cv2.XXX メソッドたちの大半はInputArrayを取るようになっており、Matは暗黙にキャストされます。そうして作られるInputArrayオブジェクトが解放されない疑いはあります。(これは現行OpenCvSharpの一番の設計ミスと考えています)

TakuNishiumi commented 5 months ago

@shimat 様 早速お返事をいただきましてありがとうございます。 いただいたコードではメモリリークは発生しませんでしたので、おっしゃる通りなのではないかと思っております。

※ところでこの件とは関係ないかもしれませんが、下記のようなコード(Mat同士のDotやCross)でもメモリリークが発生するように見えます。

  double[] arr1 = new double[3] { 0, 1, 2 };
  double[] arr2 = new double[3] { 1, 2, 3 };

  Mat mat1, mat2;

  mat1 = Mat.FromArray(arr1);
  mat2 = Mat.FromArray(arr2);

  for (int i = 0; i < 100000000; i++)
  {
     //  using (Mat val = mat1.Dot(mat2)) 
      using (Mat val = mat1.Cross(mat2)) 
      { 
      }
  }

解決策をご存知でしたら、教えていただけますと幸いです。 また、別のissueを立てたほうが良いようでしたら再投稿いたします。 どうぞよろしくお願いいたします。

shimat commented 5 months ago

DotやCrossは引数としてやはり InputArray を取るため、同様の問題によるものと思います。 https://github.com/shimat/opencvsharp/blob/c20b95c59db37be71f52498de247596218d21e81/src/OpenCvSharp/Modules/core/Mat/Mat.cs#L1757

前のコメントのように using で全て取り扱うのが最も確実ですが、ほかに GC.Collect(); を頻繁に呼びまくる、という手もあります。簡易的な回避には良いかもしれません。

TorchSharp でのこちらのドキュメントは、だいたい事情はOpenCvSharpとも共通しており参考になるかもしれません。https://github.com/dotnet/TorchSharp/blob/main/docfx/articles/memory.md#technique-1-automatic-disposal-via-garbage-collection

TakuNishiumi commented 5 months ago

承知しました。 ご丁寧にありがとうございました。