Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cv2.MinMaxLoc causes Memory leak #1642

Closed
TakuNishiumi opened this issue Jan 12, 2024 · 4 comments
Closed

Cv2.MinMaxLoc causes Memory leak #1642

TakuNishiumi opened this issue Jan 12, 2024 · 4 comments

Comments

@TakuNishiumi
Copy link

TakuNishiumi commented Jan 12, 2024

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

  • Windows 10
  • OpenCVSharp4

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?

@TakuNishiumi TakuNishiumi changed the title CV2.MinMaxLoc causes Memory leak Cv2.MinMaxLoc causes Memory leak Jan 12, 2024
@shimat
Copy link
Owner

shimat commented Jan 15, 2024

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

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

  • Windows 10
  • OpenCvSharp 4.9.0.20240106
  • .NET 8 / .NET Framework 4.8
  • Configuration: Debug/Release

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

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
Copy link
Author

@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
Copy link
Owner

shimat commented Jan 15, 2024

DotやCrossは引数としてやはり InputArray を取るため、同様の問題によるものと思います。

public double Dot(InputArray m)

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

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

@TakuNishiumi
Copy link
Author

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

@shimat shimat closed this as completed Jan 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants