Search code examples
c#winformsmemory-leaksbitmapaforge

C# AForge huge memory consumption


After a lot of research I turn to you for help as I don't know what to do anymore. I am using AForge VideoCaptureDevice to show the camera in picturebox. However the memory consumption is pretty high and garbage collector does not want to release the memory for some reason. I created simple testing application which consists of two forms (Form1 and Form2). Form1 is there just to open Form2 via button click. Form2 contains picturebox, which contains the current frame. Form2 also contains button to stop the VideoCaptureDevice and dispose image + close the form. When I open Form2 via Form1, then click on button to stop the VideoCaptureDevice and close the form and I do it over and over again, memory is growing (for example to 3GB) and GC does not release it - sometimes it does.

Problem is that in my real app, when this happens, memory grows up to ~1.1GB and then VideoCaptureDevice stops triggering NewFrame handler until I call GC.Collect() to cleanup the memory.

I tried everything and could not solve the problem.

namespace AForgeTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var form2 = new Form2();
            form2.ShowDialog();
        }
    }
}
namespace AForgeTest
{
    public partial class Form2 : Form
    {
        private FilterInfoCollection _filterInfoCollection;
        private VideoCaptureDevice _videoCaptureDevice;

        public Form2()
        {
            _videoCaptureDevice = new VideoCaptureDevice();//new VideoCaptureDevice();
            _videoCaptureDevice.NewFrame += new NewFrameEventHandler(FinalFrame_NewFrame);
            _videoCaptureDevice.VideoSourceError += new VideoSourceErrorEventHandler(ErrorHandler);
            _filterInfoCollection = new FilterInfoCollection(FilterCategory.VideoInputDevice);
            InitializeComponent();
        }

        private void ErrorHandler(object sender, VideoSourceErrorEventArgs eventArgs)
        {
            Console.WriteLine("Video feed source error: " + eventArgs.Description);
        }

        private void FinalFrame_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            var bitmap = pictureBox1.Image;
            pictureBox1.Image = new Bitmap(eventArgs.Frame);
            bitmap?.Dispose();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            _videoCaptureDevice.Source = _filterInfoCollection[0].MonikerString;
            _videoCaptureDevice.Start();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            StopVideoCapture();
            this.Close();
        }

        public void StopVideoCapture()
        {
            while (_videoCaptureDevice.IsRunning)
            {
                _videoCaptureDevice.SignalToStop();
                _videoCaptureDevice.WaitForStop();
            }

            _videoCaptureDevice.NewFrame -= new NewFrameEventHandler(FinalFrame_NewFrame);
            _videoCaptureDevice.VideoSourceError -= new VideoSourceErrorEventHandler(ErrorHandler);
            _videoCaptureDevice = null;

            var bitmap = pictureBox1.Image;
            pictureBox1.Image = null;
            bitmap?.Dispose();
        }
    }
}

Solution

  • Thanks to Dong Li's comment, I solved the problem. I replaced the old AForge library with the new Accord library and everything seems to be working properly.

    Old Library

    New Library

    The first image shows the memory consumption when using the old library. In the second picture, on the other hand, the new library is used.