How to "unload" sounds?

I'm running a script that loads several sound files using p5.sound. However, as I load too many of them (more than 100), my browser starts getting slow and unresponsive.

Is there a way to "unload" sounds? I'm storing them in an array. I've tried assigning the array to an empty array by doing array = [] , or doing array = null, but neither seem to actually unload the p5.SoundFile.

Any ideas?


  • Answer ✓

    Hidden method dispose() should get rid of those: \m/

  • Thank you! Very useful hidden method ;)

  • edited December 2015

    Actually, I was trying it again and it seems that the files are still loaded somewhere... because even if i dispose() , my browser still gets slower and slower with every new file loaded. Any ideas of why this might be hapenning, @GoToLoop?

    OK, I'm actually not doing dispose() properly... I can't seem to be able to access the dispose() method.

  • After dispose(), also get rid of it from your array container and all other places.
    Apart from that I dunno since I've never used that library yet. 8-|

  • @GoToLoop, I did get rid of it from the array container, but nothing... My guess is that I don't know how to access the prototype right. I've tried:

    for (var i = 0; i < soundsArray.length; i++) {


    for (var i = 0; i < soundsArray.length; i++) {


    for (var i = 0; i < soundsArray.length; i++) {

    But it still doesn't work.

  • Whoa! No need to go that far! If you're clearing the whole array, just make its length = 0:

    for (var i = 0; i != soundsArray.length; soundsArray[i++].dispose())
    soundsArray.length = 0;

    But for best management you should decide for how many p5.soundfile you wanna keep loaded & ready to play().

    Are they played sequentially 1 after the other? Or the list can be played backwards too?
    Or perhaps it's totally random and unpredictable?

  • They are played sequentially, one after the other. They're sentences, being read word by word.

    I got an error from the for loop (it's evaluating like this in the VM):

    with (typeof commandLineAPI !== 'undefined' ? __commandLineAPI : { __proto: null }) { for (var i = 0; i != soundsArrayOne.length; soundsArrayOne[i++].dispose())


    But the thing is that the .dispose() method does nothing to the sound object. It returns undefined and if I check the sound object after it, everything is still there, buffer and all.

  • edited December 2015 Answer ✓

    The goal of dispose() isn't to erase the p5.soundfile object itself.
    But to release the hardware resources it's using!

    Actually just like in Java, we can't erase any object from memory by own own volition.
    Most we can do is to get rid of all of its references and await for JS' GC to finally deallocate its contiguous block of memory from the heap zone.

    Please don't mess w/ __proto__ and library's prototype objects unless you know what you're doing.

    As mentioned before, I've got no experience w/ that library. Perhaps call stop() before using dispose()?

  • @GoToLoop, thanks for the replies.

    Let me explain better what I'm doing.

    I'm playing three simultaneous sentences, each has about 50 words. Each word is a sound file, so max of 150 sound files.

    I can load all 150 sound files at once and play them sequentially and it works fine.

    I then dispose() of all 150 sound files, empty the arrays that contained them, and proceed to load the next sound files. At this point, the performance of the browser really goes down. So it seems that even using dispose() , it's not really freeing up the memory, as you've pointed out.

    In this case there's probably nothing more I can do. I will use another approach which is to refresh the page, which definitely frees the memory.

    (Will keep my hands out of p5's prototypes for now 8-} )

  • edited December 2015

    Perhaps call remove() as well after dispose()?:

    for (var s, i = 0; i != soundsArray.length; ++i) {
      s = soundsArray[i];
      s.dispose(), s.remove();
    soundsArray.length = 0;

    If that doesn't work, better file an issue at p5.js-sound's repo below:

    Mention this forum link there so they have a good start too.

  • edited December 2015

    I don't think the remove() method applies to p5.SoundFiles...

    Issue opened here:

  • I think in a recent thread I mentioned that the p5 sound library creates a sound object internally and within a closure; meaning it exists independently of the variable you assign it to... Ah - here it is.

    I never got time to dig into this but this point seems relevant here:

    If that's the case then this could be a bug and perhaps even a potential memory leak.

  • edited December 2015
Sign In or Register to comment.