0

My Code is basicly like that:

Collecting frames from webcam on every 100ms in this method.

MutexControl.Image.WaitOne();
image = null; 
image = (Bitmap)eventArgs.Frame.Clone();

Bitmap Myimage = new Bitmap(Form1.image);
MutexControl.Image.ReleaseMutex();

image is My static reference. I use it in other parts of my program. is this make any problem with memory management. Does Garbage Collector collect my trash images?

Rohit Vats
  • 79,502
  • 12
  • 161
  • 185
freewave
  • 33
  • 1
  • 3

1 Answers1

1

The System.Drawing.Bitmap class implements IDisposable, so yes, you should be disposing it before reassigning the static field. If you don't dispose it, you may put excessive pressure on the finalizer thread and you will see increased memory usage.

x0n
  • 51,312
  • 7
  • 89
  • 111
  • Yes, memory usage incrasing time by time. But then suddenly it falls down to its starting point. When i used disposing with image object, sometimes i saw a SystemDrawing.dll exception. you can see in here : http://blog.lavablast.com/post/2007/11/The-Mysterious-Parameter-Is-Not-Valid-Exception.aspx mine is similar problem. When i used my image without dispose any kind of exception thrown by system. in link RKV ansvers it. – freewave Dec 15 '13 at 17:21
  • Yes, this is expected - it will grow and grow until the GC gets to all of the undisposed bitmap instances. If you dispose them, the GC will be able to reclaim the memory much faster. – x0n Dec 15 '13 at 17:37