3

There's a problem when I use Halcon operator find_model_shape in C#. The memory occupied by this operator cannot be released even if using clear_shape_model as Halcon says.

It seems like the memory occupied by find_shape_model is larger when the more complicated shape searching parameters are set to be.

I just want to know how to release the memory after using this operator.

private void button1_Click(object sender, EventArgs e)
    {
        HTuple hv_ModelId = null;
        HTuple hv_ModelRow = null;
        HTuple hv_ModelColumn = null;
        HTuple hv_ModelAngle = null;
        HTuple hv_ModelScore = null;
        HObject ho_Image = null;


        HOperatorSet.GenEmptyObj(out ho_Image);
        ho_Image.Dispose();
        HOperatorSet.ReadImage(out ho_Image, "0.bmp");
        HOperatorSet.SetSystem("border_shape_models", "false");
        HOperatorSet.ReadShapeModel("0.shm", out hv_ModelId);

        HOperatorSet.FindShapeModel(ho_Image, hv_ModelId, 0,
            3.14, 0.75, 0, 0.5, "least_squares", 6, 0.75, out hv_ModelRow,
            out hv_ModelColumn, out hv_ModelAngle, out hv_ModelScore);

        hv_ModelRow = null;
        hv_ModelColumn = null;
        hv_ModelAngle = null;
        hv_ModelScore = null;

        ho_Image.Dispose();
        HOperatorSet.ClearShapeModel(hv_ModelId);
        hv_ModelId = null;
    }

Memory before/after using find_shape_model

2 Answers2

3

It looks like you are clearing the shape model correctly using the function:

HOperatorSet.ClearShapeModel(hv_ModelId);

Are you sure the memory is not released? What happens if you read the same shape-model again after you cleared the memory?

HOperatorSet.ReadShapeModel("0.shm", out hv_ModelId);

I would wager that the memory is not affected. Basically how the memory is managed also depends on your OS. Your OS may not immediately give the memory to other processes because it expects that the memory may be used again.

Just out of curiosity: which OS are you using and how did generate the graph in your screenshot?

  • I'm using Windows 10. The graph in the screenshot comes from the diagnose tool of Visual Studio. – Shiqian Chen Oct 29 '17 at 02:30
  • I agree with you that maybe the memory is managed by OS. But in this case, I think it should be more possible that halcon needs more memory when running this operator. I have done another test to use multiple threads to process more pictures at the same time and the memory occupied has been raised. In my test, 1.1G may be the ceiling. So maybe this operator just needs more resources and it has a limit. – Shiqian Chen Oct 29 '17 at 02:38
  • I think in my project I have to use a larger memory to guarantee robustness. – Shiqian Chen Oct 29 '17 at 02:40
  • Not sure if there is a follow-up question here, but I believe halcon does also do some of it's own memory management. The first time some of the functions/operators are called they behave differently. – SchillerFalke2 Oct 30 '17 at 07:38
1

This seems similar to this problem.

Have you tried this?

GC.Collect(); GC.WaitForPendingFinalizers();

Stefano Cavion
  • 641
  • 8
  • 16
  • 1
    I had an issue where picturebox won't be updated until Halcon decides to free its memory (usually passing from 1.1GB to 260MB) despite all of the `*.Dispose()` and despite even actually passing in the code that update the picturebox. Forcing the GC action allow me to stay always at around 260MB and update instantly my picturebox. Thnak you – erik7854 Apr 12 '22 at 09:11