flash/As3.0 | 2006/08/18 17:26
The delete keyword in Flash is used to remove variable definitions. It doesn't delete objects from memory (this happens behind the scenes using something called the "Garbage Collector"), it just takes a variable you've created and gets rid of it so that it is no longer accessible and is no longer present through iteration (for..in loops, etc.).

Internally, the Garbage Collector, or GC for short, knows when to physicially delete objects in memory when they no longer have any variables that reference them. So, for example, if you have two variables A and B and they both reference ObjectX, deleting variable A will not cause ObjectX to be removed from memory by the GC because variable B still references it. However, if you delete both variables A and B, there will be no more references to ObjectX and the GC will recognize that it needs to be removed from memory

ActionScript Code:

var a:Object = new Object();
var b:
Object = a; // reference same new Object();
delete a;
trace(b); // [object Object] - still exists in memory
delete b;  // GC will mark object for deletion from memory

This works practically the same way for Flash 8 and Flash 9 (ActionScript 1, 2, and 3), though some changes were made in 8 to improve the GC. (Note: GC deletion from memory is not immediate.)

Though the GC has not really changed much with ActionScript 3 and the new virtual machine that runs it, what has changed is the behavior of the delete keyword. Now, the delete keyword only works for dynamic properties of a class instance and not declared class memebers (variables or methods). With ActionScript 1 and 2, delete could be used for anything. ActionScript 3 only lets you delete dynamic variables and locks those which are not.


// ActionScript 2
class DeleteVarClass {
       
    public var myVar:Number;
   
    function DeleteVarClass() {
        myVar = 1;
        trace(myVar); // 1
        delete myVar;
        trace(myVar); // undefined
    }
}

// ActionScript 3
package {
    public class DeleteVarClass {
       
        public var myVar:Number;
           
        public function DeleteVarClass() {
            myVar = 1;
            trace(myVar); // 1
            delete myVar;
            trace(myVar); // 1
        }
    }
}


Because myVar in the above example was declared as part of the class definition, it cannot be deleted using delete in ActionScript 3.

Since you cannot delete class members in ActionScript 3, if you want to cause a variable to no longer reference an object or value in memory you should set your variable's value to null instead of deleting it.


Garbage Collectors는 참조값이 없는한 사용되지 않는 변수에 대한 메모리를 자동으로 찾아서 소거해주는 메모리 소거 프로세스이다. 기능상으로는 AS2.0 이나 AS3.0 이나 크게  달라진 부분은 없지만 한가지, AS3.0 에서는 클래시 맴버변수로 선언된 변수는 제거할수 없게 바뀌었다. 




 
 
태그 : , ,
이 글의 관련글(트랙백) 주소 :: http://kimkijeung.com/trackback/49

Name 
Password 
Homepage 
  secret
Comment 
  글쓰기


[PREV] [1] ... [56][57][58][59][60][61][62][63][64] ... [105] [NEXT]

 
전체 (105)
flash (74)
math&physics (4)
programming (11)
Flex2 (1)
Mac (2)
photo (0)
project (6)
주저리주저리 (3)
유용한 자료들 (1)
diary (0)
Book (1)
web (2)
«   2009/01   »
        1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31