Game Development Reference
In-Depth Information
reference counting technique of the GC happens automatically and
immediately when the number of references to an object changes.
However, because mark sweeping requires running the entire
length of the object tree in memory, it is very intense on the sys-
tem and is only run periodically . In my experience, this is usually
pretty frequently on decent machines, but it cannot be counted on
for split-second accuracy. Don
there are a few
things you can do to help the garbage collector run thoroughly and
t worry, though
Be diligent about removing your references to objects.
If you have multiple references to objects in your classes, I sug-
gest writing a function called cleanUp in classes that contain a
lot of references. This function can perform tasks like setting
references to null and emptying Arrays. By helping the reference
counting mechanism of the GC, you
easier on Flash and therefore less taxing on your game.
2. Use weakly referenced listeners.
Event listeners are a commonplace for memory leaks because
developers add them and then neglect to remove them. Any
object that is dispatching events contains a list of all the objects
listening to those events. Even if the listening object has all of its
external references set to null , it will still be in this listener list.
what are known as weak references. Weak references are not
counted as part of the reference counting mechanism of the GC,
so if only the remaining references to an object are weak, it will
be deleted. Simply set the fifth parameter of the addEventListener
method to true to use weak references. I recommend always
using them as they will save you endless headaches, and there is
not a scenario I have come across yet where using weak refer-
ences had a negative impact.
Avoid using dynamic objects other than for lists.
As a best practice, you should always use statically typed
classes, as opposed to dynamic classes, which allow you to add
new properties and methods at runtime. By forcing yourself to
intentionally declare the variables and object references you
want to use in your classes, you keep better track of them. Also,
statically typed classes require less memory as instances
because they do not require a lookup table to hold the dynami-
cally created properties and methods. Dynamic objects are a
common way references to other objects get lost so that they
not effectively garbage collected.
4. Use the unloadAndStop method in Flash Player 10.
Like I mentioned a brief while before in the section on loading
external files, unless you
re still developing Flash Player 9 con-
tent, always use the unloadAndStop method for getting rid of
loaded content. It does a far more effective job of preparing all
Search Nedrilad ::

Custom Search