Home > Software engineering >  C# PictureBox: Memory leak when updating multiple times per second
C# PictureBox: Memory leak when updating multiple times per second

Time:07-16

I am using AForge to capture video from a camera and display it in a WinForms PictureBox. AForge supplies an event that gets triggered on every new frame--so, for my camera, 30 times per second.

I'm finding that even when I am careful to dispose of the PictureBox's image, memory usage climbs rapidly (and in a weird pattern... see image below). My guess is that the event is being called more often than it can dispose of the bitmap, but I'm not sure.

Here is what I have inside the newframe event. I'm using Monitor.TryEnter per enter image description here

  1. Video capture started
  2. Unknown event causes memory usage to increase
  3. Unknown event causes memory usage to stabilize
  4. Unknown event causes memory usage to increase

And that repeats. Note that if I change the TryEnter timeout to a higher value, the stable memory usage periods get shorter.

If anyone can offer a solution for this memory leak, I would be very grateful. I did see a similar question here, but it wasn't really answered: enter image description here

After adding GC.Collect():

enter image description here

  • Related