I have a js function for playing any given sound using the Audio interface (creating a new instance for every call).
This works quite well, until about the 32nd call (sometimes less). This issue is directly related to the release of the Audio instance. I know this because I've allowed time for the GC in Chromium to run and it will allow me to play another 32 or so sounds again.
Here's an example of what I'm doing:
<html><head>
<script type="text/javascript">
function playSound(url) {
var snd = new Audio(url);
snd.play();
snd = null;
}
</script>
</head>
<body>
<a href="#" onclick="playSound('blah.mp3');">Play sound</a>
</body></html>
I also have this, which works well for pages that have less than 32 playSound calls:
var AudioPlayer = {
cache: {},
play: function(url) {
if (!AudioPlayer.cache[url])
AudioPlayer.cache[url] = new Audio(url);
AudioPlayer.cache[url].play();
}
};
But this will not work for what I want to do (dynamically replace a div with other content (from separate files), which have even more sounds on them - 1. memory usage would easily skyrocket, 2. many sounds will never play).
I need a way to release the sound immediately. Is it possible to do this? I have found no free/close/unload method for the Audio interface.
The pages will be viewed locally, so the constant loading of sounds is not a big factor at all (and most sounds are rather short).
As of 2019, it is not possible to explicitly or programmatically trigger garbage collection in JavaScript.
There's a background process in the JavaScript engine that is called garbage collector. It monitors all objects and removes those that have become unreachable.
Use jmap to force GC The Java Memory Map (JMAP) utility has a method that prints a histogram of the Java heap. One side effect of the jmap command is that when it's called, it forces a garbage collection routine.
With a const variable declaration, you can't assign to the variable something little like "" or null to clear its contents. That's really the only difference in regard to memory management. Automatic garbage collection is not affected at all by whether it is declared const or not.
I see at least one (or two) flaws:
snd = new Audio(url);
has no var
in front of it, so snd
is assigned to the global scope. That's usually not what you want: it clutters the global name space and if another script (e.g., an extension) incidentally uses snd
, things will be a mess.
And that's also why
delete snd;
doesn't work: you can't delete global variables:
When declared variables and functions become properties of a Variable object — either Activation object (for Function code), or Global object (for Global code), these properties are created with DontDelete attribute.
So, use
var snd = new Audio(url);
instead. BTW, you can't force the JavaScript engine to do garbage collection.
This is not an exhaustive answer, but to the question "Is there any way to force the chrome js engine to do garbage collection?", a chromium.org guy replied:
In general, no, by design. For testing purposes there is a flag you can pass on the command line to enable a javascript command "window.gc()" to force garbage collection.
--js-flags '--expose_gc'
UPDATE: However, as @plash noted in a comment below, this flag will only work in debug builds.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With