Skip to content
Advertisement

Memory Leak when Drawing Video to Canvas using requestAnimationFrame

I am trying to do a dual monitor screen recording by combining screen1 and screen2 in Canvas. I am using vue and electron to do this. But I always got a memory leak, after I troubleshoot my code and narrow the problem. I found that this simple code causes a memory leak, but until now I could not find out why drawing inside the canvas cause a memory leak. I also try to clean the canvas before draw but still get a memory leak. My full code is here:

JavaScript

After narrowing down my problem, I know that this part of my code causes memory leak:

JavaScript

Has anyone face this same issue before? Thank you

Advertisement

Answer

I found the problem was in this.canvas.captureStream(). I want to collage 1920×1080 side by side to create dual-monitor screen recorder, therefore I need a big canvas with a width of 3840×1080. I think that Javascript does not have enough time to do a garbage collector, when I tried to do single recording, with the resolution 1920*1080, everything goes to normal.

Here, if we would to do captureStream on the big canvas, we should choose to sacrifice one of two things below:

  • Set canvas capture stream to less fps for example 15 or 10 fps, for example, this.captureStream(10) for 10 fps

  • Second option is to rescale to smaller canvas size without sacrifice the fps.

Advertisement