- Home /
When the scene is changed once loaded the audio clip do not leave the memory. The total audio memory do not change. How can the memory be freed?
Hello, I was making a audio Controller to control and play audio clips using the controller. Audio controller is an indestructible game object, starting at the splash. It consists of the audio source which plays the clips needed. The Play function is with the audio controller. But the clips itself shall be with the individual scene with individual managers to manage the audio clips. Managers are the gameobject which consists of the reference to the clips remaining only in the scene. Now to the problem.
Problem 1: When scene 1 is loads so loads the clips in the memory. When scene 2 loads the total number of clips and the total audio memory accumulates. Even when returned to scene 1 the total audio memory and number of clips remain same for the rest of the game. Shouldn't the memory be freed of the previous clips when the new scene was loaded and the old scene destroyed(Using Unity.SceneManagement). Individual clips are referenced with individual scene in a game object as manager(scene2Manager as game object).
Problem 2: I tried Resources.UnloadAsset(audio.clip) on the managers in the OnDestroy() to run it at the end of the scene. It does work but with a more critical problem. About 2mb of residue(memory-leak) is left in the total audio memory found in the audio profiler. lets, sceme 1 total memory=10mb clip number=10. transition to scene 2 total memory=20mb clip number=10. Transition back to scene 1 total memory=12mb clip number=10. again transition to scene 2 total memory=22mb clip number=20. And this residue does not clear even when the unity editor is restarted. the computer has to be restarted to clear this residue.It accumulates over each scene transition.
Thank you for your patience. I hope i was able to make you understand the problem. I am working on android build of the unity. version 5.3.3f1.
Answer by meat5000 · Mar 19, 2016 at 05:17 PM
How about UnloadAudioData ?
UnloadAudioData does not work. returns error message. Currently only clips that are not pre-loaded can be unloaded via UnloadAudioData(). UnityEngine.AudioClip:UnloadAudioData()
Also UnloadUnusedAssets do not work, returns similar error message.
I was able to solve the problem 1 using UnloadAudioData in OnDestroy function of the script. But the Preload Audio Data on the audio clips need to be unticked. Then the UnloadAudioData shall work. The clips have to be unloaded manually, but loads when the .Play() function is called. see Pre Load Audio Data... http://docs.unity3d.com/$$anonymous$$anual/class-AudioClip.html
crowd.clip.UnloadAudioData()