Game Development Community

Free used memory again..

by Jan Sorensen · in Torque Game Engine · 12/03/2011 (9:38 am) · 12 replies

In my TGE..

If I (for example) start to play/activate a music number ther are set to 'preload = false', it load and go into memory. After playing, is there a script command to give the used RAM/memory free again?

Hope for help Jan :-).

#1
12/03/2011 (1:16 pm)
What objects are you using? Does the object give you a method that tells you when the track is done playing?

If it does then you should be able to delete the object that holds the data in that function by using the schedule function. Schedule ensures it will be deleted after the function exits. That way you don't crash the engine.
#2
12/03/2011 (2:23 pm)
Hi Frank and thanks for answers :-).

Yes I have a 'playing objekt' and I can test when the music track is stop, but will an 'delete' command release the memory?

My problem is, we have an disco with random music in our game, but if we don't release some memory between the music tracks, Torque goes down (run out of memory.. at last) :-(.
#3
12/03/2011 (2:40 pm)
If the object is storing the memory for the track then yes deleting it should delete the track. Is the track stored in a dataobject or an object? Because if it is a dataobject I am not sure if you can delete it. You may need to look at the C++ implementation to determine that.

Edit:
Make sure you monitor memory usage by the app. You should be able to determine if the objects are getting freed. You could try writing some test scripts that give you buttons to try the loading, playing, freeing of the tracks. Then you should be able to determine if it is getting freed. If not you may need a custom C++ object to solve this.

Edit:
I believe T3D supports streaming audio now. Not sure about TGE. That could be an option.
#4
12/03/2011 (3:23 pm)
My dataobject is only a dynamic 'playing list' as audio profiler as here:

new AudioProfile(Disko1)
{
filename = "fps/data/music/DiscoMusic/Disko1.ogg";
description = Audio2D;
preload = false;
};
.
.
.
And so on...............(for now 26 tracks).

But okay, good idea to make some experiments and see what happens. I can 'minimize' Torque and use the task manager to control what happens with the memory.
#5
12/04/2011 (2:13 am)
Exactly!

Okay, see where it says 'new AudioProfile(Disko1)'?

Ideally when it is finished playing it will call something like (guessing):
function Disko1::done(%this)

Inside that function is where you would schedule a delete:
%this.schedule(100,delete());

If it has a function it calls when it is done then you can do that. If there a function like this:
function AudioProfile::done(%this)

Then you only have to do it in one place for all profiles. However, if you don't want to wipe out all of them then you may have to do it on a case by case basis.
#6
12/04/2011 (1:06 pm)
How did you make out? Did you find a function that gets called when the song is done?

I got to thinking about it and figured you could use the generic object if you set a value in the config of each. Like so:
new AudioProfile(Disko1)
{
filename = "fps/data/music/DiscoMusic/Disko1.ogg";
description = Audio2D;
preload = false;
del_on_done = true;
};

Then in the "done" code for the object:
function AudioProfile::done(%this)
{
   ...
   if(%this.del_on_done == true)
   {
      %this.schedule(100,delete);
   }
}
#7
12/04/2011 (2:40 pm)
Hi again Frank and thanks for your suggestions.

I have sent the matter to our very very busy (with other things) 'scripting man', he is much better to scripting than me, I've just made 'a little first study' for him (maybe its help on his buzy :-D). He also has a membership to GG and he can read in this forum, so I await (with hope) on his comments to me. So I'll come back when I have some results.

Jan.
#8
12/10/2011 (4:50 am)
I've done just a little testing on this... I can say that a delete call does -not- seem to effect the memory usage at all. There may be more to this, hopefully your scripting man will uncover something.
#9
12/11/2011 (9:51 pm)
Does the object still function after you delete it? Maybe it operates under the assumption that you would never delete a datablock?

@Jan,
Hmmm, I am interested in what you find out about this. Hopefully there is a way to do it.

Edit:
There may be a memory leak with using "new":
www.garagegames.com/community/forums/viewthread/116601
Oh, this explains why you won't see memory returned to the OS:
www.garagegames.com/community/forums/viewthread/73681

I am glad I looked into this. It is always nice to know how the engine works when tracking things like this.
#10
12/12/2011 (7:59 am)
Hi again and thank's for interest.

Our 'free-function' works fine with the release of the music used memory. I will come back when our 'man script' has made a useful explanation of how he made it.

Jan :-).
#11
12/12/2011 (1:22 pm)
Hi again :-).

Here is what I got from our 'scripting man', hope it's okay.

Jan :-).

*************

AudioProfiles is created dynamic when needed by creating a string object and eval it.
After it has been created, it can be played by alxPlay.

%evalScript = "%this.audioprofile = \n";

%evalScript = %evalScript @ "new AudioProfile(" @ fileBase(%fileName) @ ")\n";
%evalScript = %evalScript @ "{\n";
%evalScript = %evalScript @ "filename = \"" @ %fileName @ "\";\n";
%evalScript = %evalScript @ "description = Audio2D;\n";
%evalScript = %evalScript @ "preload = false;\n";
%evalScript = %evalScript @ "};\n";

eval(%evalScript);

// Start playing

%this.audioHandle = alxPlay(%this.audioprofile.getName());

//When not needed anymore, it can be removed from memory by calling

%this.audioprofile.delete();
purgeResources();

//A little fix in the graphic engine is also needed to do a complete cleanup.

************************

Ref:
http://www.garagegames.com/community/forums/viewthread/95301/2

audio.cc

void alxCloseHandles()
{
...
...

// Add this line....

alSourcei( mSource[ i ], AL_BUFFER, AL_NONE );
mHandle[i] = NULL_AUDIOHANDLE;
mBuffer[i] = 0;
}
#12
12/12/2011 (2:44 pm)
Awesome! Thanks for updating with your solution. I am sure this will help someone down the road.

I will definitely remember: "purgeResources();"

I also learned a lot. Thanks!