Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Audio Audio Memory Management Optimizations

Discussion in 'Audio & Video' started by Serge144, Feb 11, 2021.

  1. Serge144

    Serge144

    Joined:
    Oct 2, 2018
    Posts:
    64
    Hello,

    I followed this Brackeys tutorial to set up an Audio Singleton Manager. (
    )

    So basically everything is set and running, but now I'm looking to optimize a bit the audio management.
    Currently I have around 20 audio clips (15 non-loop, 1 second clips and the other 5 looped background clips less than 1 minute). I already made some optimizations:
    1. Converting all the clips from Stereo to Mono (which reduces the clip size to almost half) and don't really need it to be Stereo ( game for mobile )
    2. For looped background clips, I have set the Load Type to Streaming which dramatically reduces the memory usage ( http://www.theappguruz.com/blog/optimize-game-sounds-in-unity )
    3. For most of the frequent non-loop audio clips (such as footsteps etc) I have set the Compression Format to ADPCM, and Sample Rate Setting to Optimize Sample Rate: https://docs.unity3d.com/Manual/class-AudioClip.html
    However I'm still a bit worried about Brackeys approach, because it seems that, we are loading a bunch of audio clips at the start of the game, and some of the clips I only use for one scene, so makes me wonder if these clips should always be in memory even though they are not needed for a specific scene.

    What would be the best approach for me to release the memory of some of audioclips when they are not needed? I have been thinking about doing the following.
    1. For the audioclips that are unique to a scene, manually remove them from the Audio Singleton Manager and place them directly in the scene and in the source using the Inspector. This way it will be loaded only when the scene loads correct?
    2. I already have a method on my Audio Singleton Manager which Plays or Stops the desired audio clips according to the current scene being played. I could use this in some way to release memory from unused audioclips, for example: When entering a specific scene I would use AddComponent (AudioSource) the way I am already doing and then when It is no longer needed I would use Destroy (also AudioSource component). This would release the audio memory no?
    Thank you in advance.
     
    Last edited: Feb 11, 2021
  2. JLF

    JLF

    Joined:
    Feb 25, 2013
    Posts:
    137
    It’s possible to manually load or unload the audio data of an audio clip to save memory. To do it, uncheck preload audio data in the import settings for the clip. Make sure load in background is checked.

    when the clip is played it will automatically load the data. There may be a delay Or you can use audioClip.LoadAudioData(); to load it before you need it.
    When you no longer need it use audioClip.UnloadAudioData(); to save memory.

    If it helps, I wrote a whole article on audio optimisation in unity which you can read here: https://gamedevbeginner.com/unity-audio-optimisation-tips

    hope that helps. All the best
     
    Gozum, Claytonious and Serge144 like this.
  3. Serge144

    Serge144

    Joined:
    Oct 2, 2018
    Posts:
    64
    Thank you, I will do this.