question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Canvas draw and animation creating Memory Leaks

See original GitHub issue

Wavesurfer.js version(s):

1.4.0

Browser version(s):

Safari and Chrome

Use behaviour needed to reproduce the issue:

All use cases …

I was seeing a never ending increase in memory usage when instantiating a wavesurfer instance and playing back an audio file (tried both, as MediaElement and WebAudio) that is NOT released after calling myWave.destroy() to destroy the instance.

At first i suspected the memory leaks had something to do with WebAudio or even the Media element, but then to test this theory, i modified the wavesurfer code and disabled the render of the canvas.

As soon as i disabled the render of the canvas the memory leak was gone.

It seems there are references to the canvas elements, event listeners and animation that are not properly removed on destroy.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
gitdisruptcommented, May 14, 2017

OK EVERYONE!! I can’t believe i didn’t think to try this earlier … but i believe i have made a HUGE improvement to performance and memory usage!!

VERY SIMPLY … For both the drawBars and drawWave methods, i wrapped their contents with a requestAnimationFrame() call, and this DRAMATICALLY reduced memory usage!!!

ALSO, at the end of those calls, i cancel the request and NULL the property.

Here is what i did …

drawBars: function (peaks, channelIndex, start, end) {
        let requestAnimationFrame = window.requestAnimationFrame || window.webkitRequestAnimationFrame;
        var scope = this;
        requestAnimationFrame(function() {
            // contents of the drawBars method here
            // all references to this. changed to scope.
        });
        requestAnimationFrame = null;
       scope = null;
}

And then of course i did the same for the drawWave method as well.

I am betting there are other improvements that can be done as it relates to memory leaks, like nulling properties when they are no longer needed, but i feel like this alone makes for a great improvement.

Hey @entonbiba, can this update be applied to the latest version?

With all that said though … i should point out that when destroying a wave instance, the memory that is used is still NOT released … VERY important to note … so this issue should remain open.

I believe though that the memory usage that is never released is likely related to the canvas elements and not necessarily the WebAudio or MediaElement instance.

In the meantime im working to apply the same idea to other methods like fillRect for example

Cheers!!

1reaction
agamemnuscommented, May 27, 2017

Yes, I see a lot of memory leaks on destroy in relatively young libraries, too.

Setting those values to null won’t do anything.

PR coming…

Read more comments on GitHub >

github_iconTop Results From Across the Web

Android canvas.draw leaks graphic memory - Stack Overflow
After testing your code, instead of a memory leak, seems to be allocated memory space which hasn't been reclaimed yet.
Read more >
HTML5 Canvas How to avoid Memory leaks Tip - Konva
HTML5 Canvas How to avoid Memory leaks Tip ... using konva, author is creating new layers, stages, shapes, texts and from that he...
Read more >
(Fix) Memory Leaks: Animating HTML5 Canvas - Illyriad
What we are interested in is how to fix it, and also achieve some performance gains from not continuously creating DOM elements. The...
Read more >
Canvas draw and animation creating Memory Leaks - - Bountysource
Coming soon: A brand new website interface for an even better experience!
Read more >
Graphics - PixiJS
Create a Graphics object, set a fill color, draw a rectangle let obj = new ... be aware of the underlying GraphicsGeometry object...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found