1

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(); } } } 
6
  • Form2 must be declared with an using statement or explicitly disposed after .ShowDialog(). Read the notes here: How to time the presentation and extraction of frames from a video file? (especially about your eventArgs.Frame) Commented Mar 20, 2023 at 14:28
  • First of all, thank you for the quick reply. My bad that I did not disposed Form2, because I was writing this test app in a hurry. However, this is not the solution anyway. Commented Mar 20, 2023 at 14:56
  • 1
    Did you know that Aforge.NET is deprecated and got replaced with Accord.NET. Try using the later version of the earlier, perhaps the newer library has better memory management. Commented Mar 20, 2023 at 15:13
  • Disposing of the modal Form is something you have to do, independently. You didn't read the notes about the video capture frames you receive. See the note in the Q&A I've linked and follow the code - Also note that imported namespaces Commented Mar 20, 2023 at 15:14
  • As I already wrote, I just forgot to dispose of the modal window in my haste, but nowhere do I write that it is unnecessary. I merely wrote that it doesn't solve the problem I'm describing - I've modified and tested the code, by the way. I have looked at your solution, but I don't see any reason to solve it this way in this case, and I don't want to just blindly copy it. Commented Mar 20, 2023 at 20:39

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Glad o know that my suggestion worked.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.