我目前正在编写一个使用 HTML5 音频 API 的应用程序。在 Chrome、IE 和 Firefox 中,我注意到我可以创建一个 Javascript 音频对象,将其设置为播放声音文件,然后将其设为未定义,声音仍然会播放,如以下示例所示:
var a = new Audio;
a.src = 'longAudioFile.mp3';
a.play();
a = undefined;
由于我以类似的方式处理许多音频对象,如果我将一个音频对象设置为未定义,是否会导致内存泄漏,或者浏览器会在播放完毕/设置为暂停时清理它吗?
最佳答案
根据规范:
Media elements must not stop playing just because all references to them have been removed; only once a media element is in a state where no further audio could ever be played by that element may the element be garbage collected.
It is possible for an element to which no explicit references exist to play audio, even if such an element is not still actively playing: for instance, it could have a current media controller that still has references and can still be unpaused, or it could be unpaused but stalled waiting for content to buffer.
还有清理说明:
<...> to release resources held by media elements when they are done playing, either by being very careful about removing all references to the element and allowing it to be garbage collected, or, even better, by removing the element's
src
attribute and anysource
element descendants, and invoking the element'sload()
method.
关于javascript - 将 Audio 对象设置为未定义是否会导致内存泄漏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24256776/