当我们开始使用Accord捕获视频时,CPU和内存消耗量很高

时间:2018-05-10 13:42:23

标签: c# .net video-capture aforge accord.net

我正在使用Accord.Video.FFMPEG来录制屏幕。我面临的问题是CPU和内存利用率都过高。通常,我们的进程使用最大2%的CPU和30 MB的内存,但是当我们开始视频捕获时,CPU上升到17%,内存达到700MB。我试着把

  

GC.Collect的();

但没有用。

private void video_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
    try
    {
        if (this._isRecording)
        {
            int screenRecordingLength = screenRecrodingRule != null ? screenRecrodingRule.length : 500;
            //Bitmap frame;
            Bitmap frame = eventArgs.Frame;
            {
                Graphics graphics = Graphics.FromImage(frame);
                try
                {
                    CURSORINFO pci;
                    pci.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(typeof(CURSORINFO));
                    if (GetCursorInfo(out pci))
                    {
                        if (pci.flags == CURSOR_SHOWING)
                        {
                            int x = pci.ptScreenPos.x - screenLeft;
                            int y = pci.ptScreenPos.y - screenTop;

                            Color c = Color.Yellow;
                            float width = 2;
                            int radius = 30;
                            if ((Control.MouseButtons & MouseButtons.Left) != 0 || (Control.MouseButtons & MouseButtons.Right) != 0)
                            {
                                c = Color.OrangeRed;
                                width = 4;
                                radius = 35;
                            }
                            Pen p = new Pen(c, width);

                            graphics.DrawEllipse(p, x - radius / 2, y - radius / 2, radius, radius);
                            DrawIcon(graphics.GetHdc(), x, y, pci.hCursor);
                            graphics.ReleaseHdc();
                        }
                    }
                    this._writer.WriteVideoFrame(frame, (DateTime.Now - videoStartTime));
                }
                catch (Exception ex)
                {
                   // this._writer.WriteVideoFrame(frame, (DateTime.Now - videoStartTime));
                }

                //this._writer.Flush();
            }
            if (DateTime.Now.Subtract(videoStartTime).TotalSeconds > screenRecordingLength)
            {
                log.DebugFormat("SR: Stopping the video capturing as the time limit is exceded config length:{0}, video length: {1}.", screenRecrodingRule.length, DateTime.Now.Subtract(videoStartTime).TotalSeconds);
                isVideoCaptureCompeted = true;
                previousScreenRecodringRule = null;
                _streamVideo.SignalToStop();
                Thread.Sleep(100);
                _writer.Flush();
                Thread.Sleep(100);
                _writer.Close();
                StopVideoCapture();
            }
        }
        else
        {
            _streamVideo.SignalToStop();
            Thread.Sleep(100);
            _writer.Flush();
            Thread.Sleep(100);
            _writer.Close();
        }
    }
    catch (Exception ex)
    {
        //log.ErrorFormat("SR: Exception occured while capturing the video {0}, {1}", ex.Message, ex.StackTrace);
    }
}

1 个答案:

答案 0 :(得分:6)

GraphicsPen是IDisposable,我会先处理它们。

如果这没有帮助,那么您可以使用PerfView tool在应用程序运行时执行多个堆快照,并将它们进行比较以查找未被销毁的对象。 Perfview可以显示CPU数据配置文件,.net内存分配和一些与GC相关的统计信息(垃圾收集期间花费的时间,每个GC事件的信息等)。 Channel9

上有一个很好的教程

最后,您可以进行完整的内存转储,然后使用WinDbg或Visual Studio(仅限终极/企业版)分析大对象引用

相关问题