AS3 - Memory management

依然范特西╮ 提交于 2019-12-25 05:32:29

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/8034522/as3-memory-management

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!