简体   繁体   English

EmguCV中的图像未释放资源

[英]Images in EmguCV not releasing resources

I am using EmguCV 3.1.0.2282 and I found that when I use an image, there are times when it doesn't release its resources and eats up memory until the PC is out of resources and an out of memory exception is thrown. 我使用的是EmguCV 3.1.0.2282,发现使用映像时,有时它不会释放其资源并占用内存,直到PC耗尽资源并引发内存不足异常。

Here is a test code I did within my application. 这是我在应用程序中执行的测试代码。 When the button is clicked, a new local image is instantiated based on an existing bitmap in memory. 单击该按钮时,将基于内存中的现有位图实例化一个新的本地图像。 It will do a manual dispose if the checkbox is checked. 如果选中此复选框,它将进行手动处理。

    private void button1_Click(object sender, EventArgs e)
    {
        Image<Bgr, Byte> TempImage = new Image<Bgr, Byte>(CurrentLeftBitmap);
        TempImage.ThresholdBinary(new Bgr(2.2, 3.3, 4.4), new Bgr(100.0, 100.0, 100.0));            
        if (checkBox1.Checked)
        {
            TempImage.Dispose();
            TempImage = null;
        }
    }   

I found each time I click on the button, memory goes down and won't be released without an application restart. 我发现每次单击按钮时,内存都会减少,并且在不重新启动应用程序的情况下不会释放内存。 Even when I do a manual dispose, memory still goes down. 即使我进行手动处理,内存仍然会减少。 Funny thing is that if I commented out the ThresholdBinary step, it works fine. 有趣的是,如果我注释掉ThresholdBinary步骤,则可以正常工作。 However, it still requires a manual dispose. 但是,它仍然需要手动处理。 I've also tried the USING statement but still the same. 我也尝试过USING语句,但仍然相同。

My question is that has anyone encounter something similar? 我的问题是有人遇到过类似的事情吗? What is the proper way of implementing these image objects? 实现这些图像对象的正确方法是什么?

Yes, doing this will run you out of memory. 是的,这样做会耗尽内存。 I managed to drain my 32GB system doing 5000 iterations of your method. 我设法消耗了我的32GB系统,进行了5000次方法迭代。 The problem is ThresholdBinary return another Image, you are not taking that image so the memory gets allocated but has no way to be disposed of. 问题是ThresholdBinary返回了另一个图像,您没有获取该图像,因此已分配了内存,但无法处理。 Change 更改

TempImage.ThresholdBinary(new Bgr(2.2, 3.3, 4.4), new Bgr(100.0, 100.0, 100.0));

To

Image<Bgr, byte> newImage = TempImage.ThresholdBinary(new Bgr(2.2, 3.3, 4.4), new Bgr(100.0, 100.0, 100.0));

Will help. 会有所帮助。

Because they are locals the GC will eventually get around to cleaning them up. 因为他们是当地人,GC最终会四处清理它们。 But it is always a good idea to dispose of things. 但是处理事物总是一个好主意。 So I added 所以我加了

TempImage.Dispose();
newImage.Dispose();

Running this 5,000 time my memory usage hardly moved. 运行这5,000次,我的内存使用情况几乎没有变化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM