I'm using BitmapSource to draw frames as video that get from IP Camera. The below is summary code.
while(true)
{
char* rgbBuffer = GetFrameFromCamera();
BitmapSource^ bitmapSource = BitmapSource::Create(resX, resY, 96, 96, PixelFormats::Bgr24, nullptr, (IntPtr)rgbBuffer, rgbBufferLength, linesize);
wpfVideoControl.Draw(bitmapSource);
delete []rgbBuffer;
}
The problem is, the memory usage that watched from Task Manager is very large. And after along time, when the memory usage about 1300MB, the application is not responding
.
My application is 32bit, and the resolution of IP Camera is 1280x960, frame per second is 25 and there are 4 cameras. Each bitmap frame is about 3,5 MB. It mean the velocity allocation memory for this case about 3,5*25*4 = 350 MB/second.
So the memory is increased very fast and seem the GC can not cover it. Therefore that caused "The application is not responding"
.
I have tried to call GC.Collect()
in each while loop as below code. And the application was worked fine. But this caused the CPU consuming.
while(true)
{
GC::Collect();
GC::WaitForPendingFinalizers();
char* rgbBuffer = GetFrameFromCamera();
BitmapSource^ bitmapSource = BitmapSource::Create(resX, resY, 96, 96, PixelFormats::Bgr24, nullptr, (IntPtr)rgbBuffer, rgbBufferLength, linesize);
wpfVideoControl.Draw(bitmapSource);
delete []rgbBuffer;
}
To avoid the CPU consuming, I have tried to call GC.Collect()
after 1s by the timer thread below
void TimerThread()
{
while(true)
{
GC::Collect();
GC::WaitForPendingFinalizers();
Sleep(1000);
}
}
This way was solved the CPU consuming issue. But the problem very large memory usage still exist.
Someone can show me the best way to solve my problem.