Giter VIP home page Giter VIP logo

Comments (4)

shimat avatar shimat commented on June 11, 2024 1

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

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

  • 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の一番の設計ミスと考えています)

from opencvsharp.

TakuNishiumi avatar TakuNishiumi commented on June 11, 2024 1

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

from opencvsharp.

TakuNishiumi avatar TakuNishiumi commented on June 11, 2024

@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を立てたほうが良いようでしたら再投稿いたします。
どうぞよろしくお願いいたします。

from opencvsharp.

shimat avatar shimat commented on June 11, 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

from opencvsharp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.