soundjs play potential memory leak by creating many WebAudioSoundInstance

283 Views Asked by At

If I continuously play a sound with soundjs in Chrome, there is an infinite increase of WebAudioSoundInstance count and therefore memory consumption grows.
How can one avoid it?

createjs.Sound.registerSound("audio/alarm.mp3", "alarm");

setInterval(function () {
 createjs.Sound.play("alarm");
}, 6500);

Screenshot of object instances

2

There are 2 best solutions below

0
On

For now I've found this dirty workaround:

var i = 0;
createjs.Sound.registerSound("audio/alarm.mp3", "alarm");

setInterval(function () {
    if (i == 10) {
        createjs.Sound.removeSound("alarm");
        createjs.Sound.registerSound("audio/alarm.mp3", "alarm");
        i = 0;
    }
    createjs.Sound.play("alarm");
    i++;
}, 6500);
0
On

I have found a solution to this issue. Each time you play a sound it creates a webaudiosoundinstance, it also returns it, you could store it into a variable, and if the variable exists and is not null you can call play on that rather than calling the global function... this will keep your memory stable within the interval and does not require to unload and reload the sound.