views:

47

answers:

1

I'm developing an AS3 application which has some memory leaks I can't find, so I'd like to ask some newbie questions about memory management.

Imagine I have a class named BaseClass, and some classes that extend this one, such as ClassA, ClassB, etc.

I declare a variable:

myBaseClass:BaseClass = new ClassA();

After a while, I use it to instantiate a new object:

myBaseClass = new ClassB(); 

some time after

myBaseClass = new ClassC(); 

and the same thing keeps happening every x millis, triggered by a timer.

Is there any memory problem here? Are the unused instances correctly deleted by the garbage collector?

Thanks!

+1  A: 

Assuming you have no other references to the instance (or, possibly, its contents), the garbage collector will clean them up. However, the time before cleanup is, as far as I know, indeterminate (there might be some hard timeline in use, but I've never seen it documented). If you're creating a huge number of instances, you might use up a lot of memory before the first ever gets cleaned up.

There is an AS call (the name of which escapes me at the moment) to force a GC run, but it shouldn't normally be necessary. If you find it necessary, you almost certainly need to rethink how your application works.

Nicholas Knight
Is there any debugging way to check how many references are pointing an instance?
araid
this is the way to force GC but please do not use it. http://www.gskinner.com/blog/archives/2006/08/as3_resource_ma_2.htmltry { new LocalConnection().connect('foo'); new LocalConnection().connect('foo');} catch (e:*) {}
Allan
@araid try this memory monitor. It is handy so that you can try add/removing lines of code and then watch in realtime the memory usuage http://github.com/mrdoob/stats.as
Allan
That is the old way to force the GC, now you should (but your really should not) use System.gc();
TandemAdam