Search Unity

Best approach to allocate audio clips, performance and memory wise

Discussion in 'Scripting' started by calpolican, Dec 3, 2019.

  1. calpolican

    calpolican

    Joined:
    Feb 2, 2015
    Posts:
    204
    Basically, what I'm wondering is to what extent should I keep the audio clips stored in variables taking memory, and how much should I load at the moment of usage and dispose later. Since audio clips tend to take so much space I've been tempted to Load everything from resources (except from the Background music) at the moment of use, and get rid of it, but some audios do play often. So I'm wondering if there's like a rule of thumb.
    Any tip, regarding sound loading, compression of the clip, best formats, garbage collection, etc, will also welcomed.
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    607
    I'm really not an audio expert, but as a rule of thumb you dont want to unload things that cause the GC to run in your Update() loop. So while dynamically loading the audio clips should be fine to cut down on loading times (if they are small enough to be easily loaded at runtime), discarding them and then having to reload them just doesnt seem like a good idea to me performance wise.
    How much memory would loading all the audio clips (for a given scene) take up? Unless you develop a really huge game, i cant imagine it taking up more than a couple / douzen MB of RAM, in which case i honestly would just load all of it. You can assume that most people have at least 'a few' gigabytes of free ram for your game while playing. I'd rather use more ram than having to deal with the GC causing spikes in the framerate.

    Also, from what i read here: https://forum.unity.com/threads/the-ins-and-outs-of-audio-memory-in-unity.97690/
    the process of unloading audio clips itself is quite expensive. Unless i misunderstood something there.
     
  3. calpolican

    calpolican

    Joined:
    Feb 2, 2015
    Posts:
    204
    Well, thanks for that link, it's really useful. Memory allocation from sound, specially in mobile is huge, I already have a memory problem with it. Along with textures, it's what always takes most of the memory. Also, remember that you can garbage collect by hand when you feel it's a good moment. Plus now Unity even offers the incremental garbage collection (a feature that breaks the task into multiple frames, so it gets rid of the spikes (though I havent used it and it's still experimental)). Obviously, perfromance wise, it's always better to store everything and don't delete a thing, without loading you save performance, but memory wise, it's quite the opposite, so it's about finding the balance between both.
     
  4. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    607
    I'm aware of incremental garbage collection. I wasnt aware that you are developing for mobile, which of course makes memory availability an entirely different story. Still, most audio clips (that arent actual background music) should be small enough to stay in memory. For bigger clips, or music, i would probably consider unloading them as well if memory is a major concern for your project.