ActionScript - Difference Between Primitive / Non-

2019-01-18 07:07发布

问题:

my understanding is that primitive types (uint, string, Number, etc.) of a class do not need to be set to null for garbage collection.

for example, i am not required to write this dispose() method in the following class:

package
{
//Imports
import flash.display.Shape;

//Class
public class DrawSquare extends Shape
    {
    //Properties
    private var squareColorProperty:uint;

    //Constructor
    public function DrawSquare(squareColor:uint)
        {
        squareColorProperty = squareColor;

        init();
        }

    //Initialize
    private function init():void
        {
        graphics.beginFill(shapeColorProperty);
        graphics.drawRect(0, 0, 200, 200);
        graphics.endFill();
        }

    //Dispose
    public function dispose():void
        {
        squareColorProperty = null;
        }

    //Get Shape Color
    public function get squareColor():uint;
        {
        return squareColorProperty;
        }
    }
}

if this is true, which i believe it is, what is the difference between objects of primitive types and objects of non primitive types concerning memory allocation?

回答1:

As far as I know, the most complete and detailed explanation of GC logics in flash player VM is located in the blog of Alex Harui, written back in 2007. Direct link: GCAtomic.ppt.

And here are some useful hints on GC from Grant Skinner.

GC logic deals with references and reference counting. And since you can not obtain a reference to a primitive in ActionScript, you can do nothing about GC in this aspect.

EDIT Just remembered another nice set of articles on GC and resource management by Grant Skinner.



回答2:

The GC removes objects that are not strong-referenced by any object. Primitive typed fields are not referenced at all - their values are directly stored in the the containing object's memory (at least I think so).

I hope it helps.