Memory Leaks - How Can You Tell?

BlitzMax Forums/BlitzMax Beginners Area/Memory Leaks - How Can You Tell?

Chalky(Posted 2007) [#1]
Hi all. Have read a lot recently about memory leaks and often see 'fixed minor memory leak' listed in update feature lists. How can I tell if my programme is causing a memory leak? Am I correct in believing leaks only occur if I declare and allocate an object (ie. pixmap, image, imagefont, stream etc.) but don't set it to Null and call GCCollect when I'm done?


Gabriel(Posted 2007) [#2]
No. Leaks also occur when you allocate external memory, not managed by GCC and don't free it again. You may not be doing this, of course.

Incidentally, you don't normally have to "set it to null" to destroy an object. You just have to stop using it, remove it from a container, or let it go out of scope. If you have cyclic references ( two objects both of which reference each other ) then you will need to manually null one to break the cyclic dependency and allow one ( then the other will automatically follow ) to be collected.


FlameDuck(Posted 2007) [#3]
How can I tell if my programme is causing a memory leak?
Leave it running over night. If your machine grinds to a halt, and starts thrashing on the harddrive like mad, that's a good sign.


computercoder(Posted 2007) [#4]
Go for the brute force eh FlameDuck? :)

@Chalky:
You could also look to see if you've duplicated your variables... that is you create an object identical to another object but keep both in memory needlessly.


Chalky(Posted 2007) [#5]
@Gabriel:
I haven't yet reached the lofty heights of allocating external memory so don't have to worry about that! Forgive me for asking (I'm pretty new to Max) but does 'out of scope' mean creating an object inside a function, but not destroying it before exiting the function? If I do this kind of thing, does Max automatically execute GCCollect when a programme terminates?


Gabriel(Posted 2007) [#6]
Forgive me for asking (I'm pretty new to Max) but does 'out of scope' mean creating an object inside a function, but not destroying it before exiting the function?

You can't destroy objects any more, so the second part of that question is somewhat redundant. But yes, if you create a new object and put it into a local variable, where the variable is local to a function, then the object will no longer be reachable when you exit the function and the GC will ( sooner or later ) collect it, and free your memory.


Chalky(Posted 2007) [#7]
Excellent. Thanks.


FlameDuck(Posted 2007) [#8]
Here you go:
SuperStrict

Function doStuff()
	Local i:Int = 45 ' Creates an Integer i which has Local scope to the dostuff() function.
	For Local a:Int = 0 Until 10 ' Creates an Integer a which has Local scope to the For loop.
		i:+a
	Next
	
	' a is now out-of-scope (no longer accessable) and is garbage collected.
	Print i
EndFunction

' i is now out-of-scope, and is garbage collected.

doStuff()
Now I realize it's not the best example in the world, but I think it illustrates the principle of scope fairly well. It also doesn't do that whole recursive reference thing that is the bane of the BlitzMAX GC model.


Chalky(Posted 2007) [#9]
Best example in the world or not - it's a very good illustration - thanks FlameDuck.


Paposo(Posted 2007) [#10]
Hello.

GCC not manage correctly de ciclic references.
If you have two object and the object A reference B and the object B reference A, is good way for make a memory leak.

Bye,
Paposo


Yan(Posted 2007) [#11]
BMax Help Home/Welcome Page>Tutorials and Articles>BlitzMax Memory Management