View Full Version : memory leakage on snapshot

April 6, 2009, 11:12:17
I've a problem with the snapshot photo
I'm testing the sample given with the sdk
The application start at 25560Ko of memory utilisation
I take 10 photos and now I'm at 36800Ko
How can I clear the memory of the snapshot.
Since I finish the post it is at 37000Ko I think the video control hav a problem too
37200Ko .....:confused:

Stefan Geissler
April 6, 2009, 11:32:06
Hello Sdecorme,

The problem is the .NET Garbage Collector. This is, what happens:

- Every time the live mode is started, the image ring buffer is re-created.
- The 10 new ImageBuffer objects "own" the memory of these these images.
- When live mode is stopped and started again, 10new objects are created, the old ones left
alone... until a garbage collection occurs.

It seems like .NET does not like to do such a garbage collection too often: you can see it increases for 5-8 start/stop cycles, until a garbage collection occurs and all the memory is freed again.

To get rid of your problem, you have to force a garbage collection (GC.Collect) after LiveStart - this releases the old and unused buffers.

I hope, this explains your problem.

April 6, 2009, 11:57:21
Ok for the liveview the memory size doesn't increase . But it doesn't work after the snapshot function.

Stefan Geissler
April 6, 2009, 12:00:56
Dear Sdecorme,

I would like to know, whether the live video stream is running all over the time or whether it is started extra for every snapshot.

Also it is interresting to know, what you are doing after the snapshot.

Thank you in advance.

Stefan Geissler
April 6, 2009, 12:07:52
Just an addition: We do not know, how often the garbage collector is called. It seems on how much memory was allocated. Thus it may takes a lot of time, until the garbage collector is called.

April 6, 2009, 12:34:40
I will look for a long time how it work increase .
But for ths snapshot it is critical if I use it too much

Stefan Geissler
April 6, 2009, 12:37:13
Thank you. Please let me know, what your are doing in your "snapShot".

April 6, 2009, 12:49:52
This is the code from the sample "grabbing an image"

private void cmdSaveBitmap_Click(object sender, System.EventArgs e)
SaveFileDialog saveFileDialog1;
saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "bmp files (*.bmp)|*.bmp|All files (*.*)|*.*";
saveFileDialog1.FilterIndex = 1;
saveFileDialog1.RestoreDirectory = true;

if( saveFileDialog1.ShowDialog() == DialogResult.OK)
icImagingControl1.MemorySaveImage(saveFileDialog1. FileName);

I make the memory test with this code

Stefan Geissler
April 6, 2009, 13:00:25
Thank you very much for the code snippet. I would like to know (sorry for asking sooo much), whether the live video stream isalready running, when this function is called. If not, MemorySnapImage starts the live stream, snaps an image and stops it again. This could explain the rising memory usage.

April 6, 2009, 13:55:31
Yes the video streaming is running all the time , and I take a photo while the video is running

private void cmdStartLive_Click(object sender, System.EventArgs e)

I've had the GC.Collect(); like you said before

Stefan Geissler
April 6, 2009, 15:04:16
Thank you for your code. You may remove the GC.Collect(), it has no effect here.

I tried the sample, started the live video stream and then clicked the snap button. The memory usage rised until 67 MByte and stays there. I also called 100 times MemorySnapImage and MemorySaveImage, but the memory usage did not grow.

There is no memory leak. Since your computer does not report an out of memory error, you can ignore this .NET error handling.

April 6, 2009, 15:08:54
I didn't take photo more then 20 times .
So there is no way to free the memory usage ?
Thanks for your quick response.

Stefan Geissler
April 6, 2009, 15:12:12

So there is no way to free the memory usage ?
Unfortunately not, but you do not need to care about this.

You may also have a look on the memory usage, while you move the mouse pointer over the live video. It rises too, but some time later, if there was too much memory allocated it should be freed automatically.

For us "old C programmers" this may looks strange and unusual, but I think, this is correct .NET memory management.