About memory freeing ?

BlitzMax Forums/BlitzMax Programming/About memory freeing ?

Hi :)

I have try to compress a bank :) the code work but i don't
understand why my memory is not free after process ? the output
window return

Executing:Zip Engine.exe
MEM USED 1 : 15917 / 16656
MEM USED 2 : 1447823 / 1448195
MEM USED 3 : 90692 / 95558
Process complete

Theoretically the test number 3 should be the same one as the
test number 1 because i released my RAM? or i'm idiot ?

This is the code used for my test :

Import Pub.ZLib

Print "MEM USED 1 : "+MemAlloced()+" / "+MemUsage()
MySourceBank:TBank=TBank.load("C:\Filou\editor.jpg") 
Print "MEM USED 2 : "+MemAlloced()+" / "+MemUsage()
MyOutBank:TBank=CompressBank(MySourceBank)
MyOutBank.Save("C:\Filou\editor.bin") 

MemFree(MyOutBank,BankSize(MyOutBank))
FlushMem

MemFree(MySourceBank,BankSize(MySourceBank))
FlushMem

Print "MEM USED 3 : "+MemAlloced()+" / "+MemUsage()
End

Function CompressBank:TBank(Bank:TBank)
	Local size=bank.Size()
	Local out_size=size+size/10+32
	Local out:TBank=TBank.Create( out_size )
	compress out.Buf()+4,out_size,bank.Buf(),size
	out.PokeByte 0,size
	out.PokeByte 1,size Shr 8
	out.PokeByte 2,size Shr 16
	out.PokeByte 3,size Shr 24
	out.Resize out_size+4
	Return out
End Function

Function UncompressBank:TBank(Bank:TBank)
	Local out_size
	out_size:|bank.PeekByte(0)
	out_size:|bank.PeekByte(1) Shl 8
	out_size:|bank.PeekByte(2) Shl 16
	out_size:|bank.PeekByte(3) Shl 24
	Local out:TBank=TBank.Create( out_size )
	uncompress out.Buf(),out_size,bank.Buf()+4,bank.Size()-4
	Return out
End Function


Building test
Compiling:test.bmx
flat assembler version 1.51
3 passes, 3877 bytes.
Linking:test.exe
Executing:test.exe
MEM USED 1 : 15897 / 16656
MEM USED 2 : 952484 / 952927
MEM USED 3 : 29097 / 29887

Process complete

I don't understand why my memory is not free after process ?
Because you aren't dereferenceing your banks, ie they still exsist as valid objects, and so aren't being garbagecollected.

Try this code instead:
Import Pub.ZLib

FlushMem
Print "MEM USED 1 : "+MemAlloced()+" / "+MemUsage()

Local MySourceBank:TBank=TBank.load("C:\Dev\42ninjas\bin\logo.png") 

Print "MEM USED 2 : "+MemAlloced()+" / "+MemUsage()

Local MyOutBank:TBank=CompressBank(MySourceBank)
MyOutBank.Save("C:\Dev\logo.bin") 

MyOutBank = Null
MySourceBank = Null

FlushMem

Print "MEM USED 3 : "+MemAlloced()+" / "+MemUsage()

End
Oh and I leave you with this quote from the manual:
Function MemFree( mem:Byte Ptr,size ) Free allocated memory.

The memory specified by mem must have been previously allocated by MemAlloc or MemExtend.


Ok ok !! many thanks FlameDuck !