Halcon FindNccModel导致C#中的内存泄漏 [英] Halcon FindNccModel causes memory leak in C#

查看:2157
本文介绍了Halcon FindNccModel导致C#中的内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中使用Halcon 13函数FindNccModel会导致以下错误: HALCON错误#6001:运算符find_ncc_model中没有足够的可用内存

Using the Halcon 13 function FindNccModel in C# causes the following error: HALCON error #6001: Not enough memory available in operator find_ncc_model

class Program
{
    static void Main(string[] args)
    {
        HImage Image = new HImage(@"08_09_09_41_33_582_OK_000000153000.png");
        double MidpointRow = 1053.5210373923057, MidpointCol = 1223.5205413999142;

        int iCounter = 0;

        while (true)
        {
            HNCCModel model = new HNCCModel(@"000000135000Mark_0.ncm");

            HXLDCont hxCont = new HXLDCont();
            hxCont.GenRectangle2ContourXld(
                721.9213759213759,
                1775.862648221344,
                -0.99483767363676778,
                72,
                14.5);

            HTuple htRowXLD, htColXLD;
            hxCont.GetContourXld(out htRowXLD, out htColXLD);
            HTuple htRadius = new HTuple();
            htRadius = new HTuple(htRowXLD.TupleSub(MidpointRow).TuplePow(2) + htColXLD.TupleSub(MidpointCol).TuplePow(2)).TupleSqrt();
            HRegion hrAnnulus = new HRegion();
            hrAnnulus = hrAnnulus.GenAnnulus(MidpointRow, MidpointCol, htRadius.TupleMin() - 5.0, htRadius.TupleMax() + 5.0);

            HImage hiTemp = Image.Clone();
            HImage hiTemp2 = hiTemp.Rgb1ToGray();
            HImage hiTemp3 = hiTemp2.ReduceDomain(hrAnnulus);

            HTuple htRow, htColumn, Angle, Score;

            model.FindNccModel(hiTemp3, -0.39, 6.29, 0.65, 1, 0, "true", 0, out htRow, out htColumn, out Angle, out Score);

            hxCont.DisposeIfNotNull();
            hrAnnulus.DisposeIfNotNull();
            model.Dispose();

            hiTemp.DisposeIfNotNull();
            hiTemp2.DisposeIfNotNull();
            hiTemp3.DisposeIfNotNull();

            Console.WriteLine(iCounter++.ToString());
        }
    }
}

public static class DL_HalconUtilityClass
{
    public static HRegion GenAnnulus(this HRegion region, double dCenterRow, double dCenterColumn, double dRadiusSmall, double dRadiusBig)
    {
        region.GenEmptyRegion();

        if (dRadiusSmall > dRadiusBig)
        {
            throw new NotSupportedException("Wrong input parameters. Small radius is bigger than big radius.");
        }

        HRegion hrCircleSmall = new HRegion(dCenterRow, dCenterColumn, dRadiusSmall);
        HRegion hrCircleBig = new HRegion(dCenterRow, dCenterColumn, dRadiusBig);

        region = new HRegion();
        region = hrCircleBig.Difference(hrCircleSmall);

        hrCircleSmall.Dispose();
        hrCircleBig.Dispose();

        return region;
    }

    public static void DisposeIfNotNull(this HImage hiImage)
    {
        if (hiImage != null) hiImage.Dispose();
    }

    public static void DisposeIfNotNull(this HRegion hrRegion)
    {
        if (hrRegion != null) hrRegion.Dispose();
    }

    public static void DisposeIfNotNull(this HObject hoObject)
    {
        if (hoObject != null) hoObject.Dispose();
    }
}

该函数本身可以在while循环中无休止地运行,但是如果将其与我们的程序结合使用,则会导致内存异常.另一方面,没有此功能,程序本身也可以无休止地运行.有趣的是,该错误发生在程序到达典型的1,1 Gb内存之前,这意味着存在内存泄漏.

The function itself can run endlessly in an while loop, but if it's combined with our program it causes a memory exception. On the other hand the program itself can run endlessly without this function. It is also interesting that the error happens before the program reaches typical 1,1 Gb of memory which means that there is a memory leak.

我在Halcon文档中没有找到关于此问题的任何参考,升级到最新的Halcon 13版本或使用Halcon XL都无济于事.有人知道什么可能导致此问题吗?

I didn't find any references to this problem in Halcon documentation and upgrading to the newest Halcon 13 version or using Halcon XL did not help. Does anyone know what could cause this problem?

推荐答案

Halcon有两个内存管理优化系统设置:global_mem_cache和临时_mem_cache. global_mem_cache没有影响,但是将临时_mem_cache参数设置为"idle"或"shared"可以解决此问题.

Halcon has two memory management optimization system settings: global_mem_cache and temporary_mem_cache. The global_mem_cache had no influence, but set the temporary_mem_cache parameter to "idle" or "shared" solved the problem.

默认设置为独占",其中每个线程在本地缓存临时内存.这是Halcon文档的摘录:

Default setting is "exclusive" where temporary memory is cached locally for each thread. This is an excerpt from Halcon documentation:

'temporary_mem_cache'*),'tsp_temporary_mem_cache' 此参数控制临时内存高速缓存的操作模式.临时内存缓存用于通过缓存在操作员执行期间临时使用的内存来加速应用程序.对于大多数应用程序,默认设置(独占")将产生最佳效果.支持以下模式:

'temporary_mem_cache' *), 'tsp_temporary_mem_cache' This parameter controls the operating mode of the temporary memory cache. The temporary memory cache is used to speed up an application by caching memory used temporarily during the execution of an operator. For most applications the default setting ('exclusive') will produce the best results. The following modes are supported:

  • 'idle'临时内存缓存已关闭.与其他模式相比,此模式将占用最少的内存,但也会降低性能.

  • 'idle' The temporary memory cache is turned off. This mode will use the least memory, but will also reduce performance compared to the other modes.

共享"的所有临时内存都全局缓存在临时内存库中.与独占"模式相比,此模式将使用较少的内存,但通常也将提供较少的性能.

'shared' All temporary memory is cached globally in the temporary memory reservoir. This mode will use less memory than 'exclusive' mode, but will also generally offer less performance.

'exclusive'每个线程的所有临时内存都在本地缓存.此模式将使用最多的内存,但通常也会提供最佳性能.

'exclusive' All temporary memory is cached locally for each thread. This mode will use the most memory, but will generally also offer the best performance.

'aggregate'大于通过'alloctmp_max_blocksize'参数设置的阈值设置的临时内存块将被缓存在全局内存库中,而所有较小的块将被聚合为一个块,该块针对每个线程本地缓存.如果禁用了全局内存库,则将释放大块.将根据线程到目前为止所看到的临时内存使用量来调整聚合块的大小,但不会大于"alloctmp_max_blocksize"(如果设置)或小于"alloctmp_min_blocksize"(如果设置).此模式可以平衡内存使用和速度,但是需要正确设置应用程序的内存使用模式的"alloctmp_min_blocksize"和"alloctmp_max_blocksize",以提高效率.

'aggregate' Temporary memory blocks that are larger than the threshold set with the 'alloctmp_max_blocksize' parameter are cached in the global memory reservoir, while all smaller blocks are aggregated into a single block that is cached locally for each thread. If the global memory reservoir is disabled, the large blocks are freed instead. The aggregated block will be sized according to the temporary memory usage the thread has seen so far, but it will not be larger than 'alloctmp_max_blocksize' (if set) or smaller than 'alloctmp_min_blocksize' (if set). This mode balances memory usage and speed, but requires correctly setting 'alloctmp_min_blocksize' and 'alloctmp_max_blocksize' for the application's memory usage pattern for effectiveness.

请注意,缓存模式'idle'设置为独占运行模式,而其他模式设置为可重入模式.

Note that cache mode 'idle' is set in exclusive run mode, whereas the other modes are set in reentrant mode.

为了向后兼容,还接受值'false'和'true';它们分别对应于"idle"和"exclusive".

For backward compatibility, the values 'false' and 'true' are also accepted; they correspond to 'idle' and 'exclusive', respectively.

这篇关于Halcon FindNccModel导致C#中的内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆