Memory leak with Audio? (Solved)
by Jonathan R Hopkins · in Technical Issues · 12/22/2009 (6:17 pm) · 1 replies
Solved...! Mostly. See "Solution" Below
So I've recently realized that each time I change to a new piece of music within my game, I get between 10,000 and 60,000 K added in memory. The game starts at 57,000 before any music is playing, and when I've triggered each music event, I end up with 400,000 K - This is a serious issue.
Here is how I use audio:
I create the audio profiles as I need them.
This is the code that switches between pieces:
Here is the code for deleting profiles, fading, cross-fading:
So my hope would be that once the profile is deleted, the memory would be returned, but its not. The memory just keeps increasing and never goes down. All my music is between 2 and 8 MB in size and it is all in the .ogg format.
Any ideas on what's going on or what I can do?
So it turns out in my audioDescription, all I needed to do was add the isStreaming bit, and the overhead was cleaned up:
The reason I say it isn't a complete solution, is because there is still a bit of a leak, I believe, but by using "isStreaming" in your AudioDescription, you will significantly reduce your overhead. I was seeing a difference of 1,000K between songs at most, with there generally being no increase, and the game never surpassed 120,000K
I got part of the solution here:
Ronny Bansgund said "Then consider the possibility that the current audio functions might load the
uncompressed Ogg files into memory, rather than properly stream small chunks :/
www.torquepowered.com/community/forums/viewthread/55332
So I've recently realized that each time I change to a new piece of music within my game, I get between 10,000 and 60,000 K added in memory. The game starts at 57,000 before any music is playing, and when I've triggered each music event, I end up with 400,000 K - This is a serious issue.
Here is how I use audio:
I create the audio profiles as I need them.
new AudioProfile(mainTheme)
{
filename="~/data/audio/bgm/mainTheme.ogg";
description = "AudioLooping";
preload = false;
};
$soundLoadsAttempted += 1;
return(true);This is the code that switches between pieces:
echo("Switching tracks from: " @ controller.BGM @ " to: " @ %newAudio);/
%previousAudio = controller.BGM;
controller.BGM = %newAudio;
controller.BGMID = crossFade(controller.BGMID,controller.BGM,4000,4000,2000,controller.masterVolume);
schedule(6000,0,deleteBGM,%previousAudio);Here is the code for deleting profiles, fading, cross-fading:
function soundFadeOut(%handle,%time)
{
%currentVolume = alxGetVolume(%handle);
gradualEffect(%handle,%currentVolume,0,%time,100,alxSetVolume);
schedule(%time,0,alxStop,%handle);
%handle.delete();
}
function soundFadeIn(%handle,%time,%volume)
{
if(isEmpty(%volume))
{
%volume = 1;
}
alxSetVolume(%handle,0.0);
gradualEffect(%handle,0,%volume,%time,100,alxSetVolume);
}
function crossFade(%currentHandle,%newSound,%outTime,%inTime,%delayIn,%volume)
{
%newHandle = alxPlay(%newSound);
alxSetVolume(%newHandle,0);
soundFadeOut(%currentHandle,%outTime);
schedule(%delayIn,0,soundFadeIn,%newHandle,%outTime,%volume);
return(%newHandle);
}
function deleteBGM(%BGM)
{
echo("Deleting BGM: " @ %BGM);
%BGM.delete();
}So my hope would be that once the profile is deleted, the memory would be returned, but its not. The memory just keeps increasing and never goes down. All my music is between 2 and 8 MB in size and it is all in the .ogg format.
Any ideas on what's going on or what I can do?
Solution
So it turns out in my audioDescription, all I needed to do was add the isStreaming bit, and the overhead was cleaned up:
new AudioDescription(AudioLooping)
{
volume=1.0;
isLooping=true;
is3D=false;
type=$GuiAudioType;
isStreaming=true;
};The reason I say it isn't a complete solution, is because there is still a bit of a leak, I believe, but by using "isStreaming" in your AudioDescription, you will significantly reduce your overhead. I was seeing a difference of 1,000K between songs at most, with there generally being no increase, and the game never surpassed 120,000K
I got part of the solution here:
Ronny Bansgund said "Then consider the possibility that the current audio functions might load the
uncompressed Ogg files into memory, rather than properly stream small chunks :/
www.torquepowered.com/community/forums/viewthread/55332
Torque Owner Jonathan R Hopkins
I've tested a .wav in case it was caused by the way .ogg files are handled. It made no difference.
I've tried it with smaller files, which only results in smaller working memory increases, but still no memory consistency.
I've tried not deleting the profile myself to see if maybe I was disrupting Torque's own ability to properly handle memory. Still no luck.
Any thoughts? Any help at all would be greatly appreciated.