Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cpu and memory consumption is going high when we start capturing the video using Accord

I am using Accord.Video.FFMPEG for recording the screen. I am facing the issue like both CPU and memory utilization is going too high. usually, our process uses max 2% of CPU and 30 MB of memory but when we start the video capture then the CPU is going up to 17% and memory is reaching up to 700MB. I tried to put

GC.Collect();

but found no use.

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);
    }
}
like image 229
Ravi Kanth Avatar asked May 10 '18 13:05

Ravi Kanth


1 Answers

Graphics and Pen are IDisposable, I would start with disposing of them.

If that doesn't help, then you can use PerfView tool to do several heap snapshots while your application is running and compare them to find objects not being destroyed. The Perfview can show CPU data profile, .net memory allocations and some GC-related statistics (time spent during garbage collection, information every GC event etc). There is a good tutorial on Channel9

Finally, you can take a full memory dump and then analyze big objects references with WinDbg or Visual studio (only ultimate/enterprise edition)

like image 57
dlxeon Avatar answered Oct 25 '22 18:10

dlxeon