Reputation: 3851
I have a question regarding AS3 memory management I wonder if anyone could help me with.
Supposing I created an instance variable for a Class, in this case or type Sound:
public class SoundStore extends Sprite{
var s:Sound;
Then within various class functions I referenced this variable multiple times, each time I wanted to load in a new sound:
s = new Sound();
Am I correct in thinking that each time I created a new Sound I would be overwriting the previous allocated memory?
Thanks
Upvotes: 0
Views: 1914
Reputation: 28316
No. AS3 is a garbage collected language, which uses reference counting to dispose of unused objects.
The s
variable is, internally, a pointer to a block of memory that contains a Sound
object. Every time you do s = new Sound()
AS3 will create a new Sound
object in memory and set the s
pointer to the address of that object. The old object still exists in memory. If you have no references to the old object, the garbage collector will dispose of the object at some point, usually its next collection round. This means that at any point in time you may have multiple Sound
objects in memory that aren't being referenced but that are still using up resources. The garbage collector is designed to periodically trawl through all allocated objects and dispose them if there are no references to them.
Here's a nice article on the GC in Flash / AS3: http://www.adobe.com/devnet/flashplayer/articles/garbage_collection.html
Upvotes: 5