经过大量的研究,我转向你的帮助,因为我不知道该怎么办了.我使用AForge VideoCaptureDevice在picturebox中显示相机.然而,内存消耗相当高,垃圾收集器不想释放内存出于某种原因.我创建了简单的测试应用程序,其中包括两个窗体(Form 1和Form 2)。Form 1只是通过按钮单击打开Form 2。Form 2包含picturebox,其中包含当前帧。Form 2还包含停止VideoCaptureDevice并处理图像+关闭窗体的按钮。当我通过Form 1打开Form 2时,然后单击按钮停止VideoCaptureDevice并关闭窗体,我一遍又一遍地这样做,内存正在增长(例如到3GB),GC不会释放它-有时会。
问题是,在我真实的的应用程序中,当这种情况发生时,内存增长到~1.1GB,然后VideoCaptureDevice停止触发NewFrame处理程序,直到我调用GC.Collect()来清理内存。
我尝试了所有方法,但无法解决问题。
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();
}
}
}
1条答案
按热度按时间11dmarpk1#
多亏了董丽的点评,我解决了这个问题,我把旧的AForge库换成了新的雅阁库,一切看起来都很正常。
第一张图显示了使用旧库时的内存消耗。另一方面,在第二张图中,使用了新库。