Memory Management - Clean Sound From Memory

BlitzMax Forums/BlitzMax Beginners Area/Memory Management - Clean Sound From Memory

McFox(Posted 2005) [#1]
Hi, i'm trying to understand how to clean a sound file from the memory when the file is no longer needed,

Exemple :

Graphics 1,1,0
FlushMem()
Print "Starting "+MemAlloced()
WaitKey
sndSfx = LoadSound("sfx\bip.ogg",True)
chan = PlaySound(sndSfx)
FlushMem()
Print "Playing Sound File "+MemAlloced()
WaitKey
StopChannel chan
Release sndSfx
Release sound
Release chan
FlushMem()
Print "Stop & Clean "+MemAlloced()
WaitKey


You can get the bip here :
[url]http://mcfox666.free.fr/sfx/bip.ogg[/url]

The code gave me :

Starting 85040
Playing Sound File 87712
Stop & Clean 87640


As you can see, after the "stop and clean" the sound file seams to be still here. How can I realy release it from mem ?

Thanks

McFox


Yan(Posted 2005) [#2]
You have...

From what I can gather, when the garbage collector frees memory it doesn't necessarily release it back to the system immediately but, for speed reasons, maintains a pool that can be reallocated when needed. This pool is released upon exit.


Dreamora(Posted 2005) [#3]
Squatting: What you are talking off is only affecting memusage() not memalloced()


Yan(Posted 2005) [#4]
It's quite possible that I'm being a knob and I've got this wrong, but this is how I see it...

memusage() returns the total memory the app has allocated and de-allocated while it's been running.
memalloced() shows memory allocated to the app at that time.

The memory pool described above must still be allocated by blitz, otherwise what's the point?

Have I got it wrong again dad?


McFox(Posted 2005) [#5]
The memory pool described above must still be allocated by blitz, otherwise what's the point?"


Yeah, there are no other reason...

But Imagine a game where there are 1 music per map. If you play the game a long time, you'll have a memory leak (so you'll crash).
The sound here is just a little file but immagine with a 3 Mo / music file...


McFox(Posted 2005) [#6]
So... Anyone know how to remove the sound from mem ?


Dreamora(Posted 2005) [#7]
Looks to me like an error. In maximum there should remain 4 bytes in RAM after freeing it.

Did you try to program in OO style instead of procedural?


Yan(Posted 2005) [#8]
I think you've misunderstood what I meant.
I wasn't suggesting that BMax continued to hoard memory until it ran out. I was merely offering an explanation as to why the sound appeared not to be freed straight away. When you release the sound handle, the memory it used is handed over to the garbage collector to be deallocated and that may not happen immediately. AFAIC it's not a memory leak as the GC is in control.

Dreamora is right though, you should really be using objects rather than integer handles...
snd:TSound = Loadsound("sound.ogg")
...ETC...



skidracer(Posted 2005) [#9]
I'm looking into this -

1. the sample memory the audio mixer uses is allocated in C++ so I don't think will show up in MemAlloced / MemUsage reports

2. stopchannel is not immediate and can take up to 20ms to take effect

3. it's possibly a bug in my ogg loader code, you may want to test with .wav to see if you get the same results.


Yan(Posted 2005) [#10]
LOL...Alright, I'm a knob then...;o)


McFox(Posted 2005) [#11]
1. the sample memory the audio mixer uses is allocated in C++ so I don't think will show up in MemAlloced / MemUsage reports
I think you're right, because the mem usage that WinXP show is different.

3. it's possibly a bug in my ogg loader code, you may want to test with .wav to see if you get the same results.


leme test... I'll post after


McFox(Posted 2005) [#12]
Ok... look at this code :

Graphics 1,1,0

Type TMusic
	Global ChnBgm:TChannel
	Field Sound:TSound
	
	Method PlayMusic()
		ChnBgm = PlaySound(Sound)
	End Method
	
	Method StopAndClean()
		StopChannel(ChnBgm)
		ChnBgm = Null
		Sound = Null
		FlushMem
	End Method
	
	Function Create:TMusic(path$)
		Local snd:TMusic = New TMusic
		snd.Sound:TSound = LoadSound(path$,True)
		Return snd
	End Function
End Type

Print "1"
FlushMem()
Print MemAlloced()
WaitKey
Local bgm1:TMusic
bgm1 = TMusic.Create("./bgm/test.wav")
FlushMem()
Print MemAlloced()
WaitKey
bgm1.PlayMusic()
FlushMem()
Print MemAlloced()
WaitKey
bgm1.StopAndClean()
bgm1 = Null
FlushMem()
Print MemAlloced()
WaitKey
Print "2"
FlushMem()
Print MemAlloced()
WaitKey
Local bgm2:TMusic
bgm2 = TMusic.Create("./bgm/test.wav")
FlushMem()
Print MemAlloced()
WaitKey
bgm2.PlayMusic()
FlushMem()
Print MemAlloced()
WaitKey
bgm2.StopAndClean()
bgm2 = Null
FlushMem()
Print MemAlloced()
WaitKey


This code gave me (with my file) :
1
85022
87646
87658
87622
2
87622
87646
87658
87622


These are the value I can read with WinXP process viewer (ctrl + alt + supr) :
1
10 652 KB
15 972 KB
15 972 KB
15 972 KB
2
15 972 KB
20 868 KB
20 868 KB
20 868 KB

Well... I call this a memory leak lol


Yan(Posted 2005) [#13]
What about this...
I get...
Entry -> Allocated : 4398 - Used : 6592

Start -> Allocated : 96624 - Used : 113132
Load -> Allocated : 24456013 - Used : 24475756
Free -> Allocated : 99208 - Used : 119788

Start -> Allocated : 99184 - Used : 119788
Load -> Allocated : 24456013 - Used : 24475756
Free -> Allocated : 99208 - Used : 119788

Start -> Allocated : 99184 - Used : 119788
Load -> Allocated : 24456013 - Used : 24475756
Free -> Allocated : 99208 - Used : 119788

Start -> Allocated : 99184 - Used : 119788
Load -> Allocated : 24456013 - Used : 24475756
Free -> Allocated : 99208 - Used : 119788

Start -> Allocated : 99184 - Used : 119788
Load -> Allocated : 24456013 - Used : 24475756
Free -> Allocated : 99208 - Used : 119788

Start -> Allocated : 99184 - Used : 119788
Load -> Allocated : 24456013 - Used : 24475756
Free -> Allocated : 99208 - Used : 119788

Start -> Allocated : 99184 - Used : 119788
Load -> Allocated : 24456013 - Used : 24475756
Free -> Allocated : 99208 - Used : 119788

Exit -> Allocated : 99498 - Used : 119788


McFox(Posted 2005) [#14]
Look in WinXP proces... the mem is growting and growing every time you load the sound file


McFox(Posted 2005) [#15]
I don't understand...

Remove the
	Repeat
		char = GetChar()
	Until char Or (ChannelPlaying(chn) = 0)

and look at windows xp process view...

When the memory come around 550 MB used (that's a lot...)
The memory begin to going down @ around 60-100 MB...

...What would happen on a low Ram computer...


Yan(Posted 2005) [#16]
Oh yeah...It's leaking like a leaky thing.

I wish you'd mentioned that in your first post. ;o)


McFox(Posted 2005) [#17]
I wish you'd mentioned that in your first post. ;o)


To be honnest, I didn't look at Process view when I first post, I just noticed there's a real leak after.

Hope It'll be fix on next update...
I can't work with sound files if there is a mem leak. (I'm not crazy lol)


Cartman(Posted 2005) [#18]
Any update on this issue? I have two projects that are depending on the ability to load/change sound files. One is a game and the other is a Jukebox program that will definately need this fixed. If there are any work arounds I would also be interested in any idea.

Thanks


Yan(Posted 2005) [#19]
This was fixed in the 1.10 update.


Cartman(Posted 2005) [#20]
Thanks TwoEyedPete.

That's what I needed. I thought I had installed that update, but apparently I hadn't. Thanks again for your help.