My question is about memory use and objects in actionscript 2. If I have a class definition:
class test1{
public function one(){trace("Hello");}
public function two(){trace("World");}
}
And a second class definition:
class test2{
static public function one(){trace("Hello");}
static public function two(){trace("World");}
}
And then I do the following:
var object1a = new test1();
var object1b = new test1();
var object2a = new test2();
var object2b = new test2();
Is the size of object1a + object1b greater than the size of object2a + object2b because of the functions not being static (and possibly being copied into each object instantiation)? I do not have Actionscript 3 to detect memory use, maybe someone can check how this behaves in AS 3 if it is difficult to determine in AS 2.
I’m just wondering if non-static member functions are all references to the single prototype definitions, or if they are copied wholesale into each function effectively doubling memory use for test1 vs test2. I imagine they are treated as references and then overriding them simply changes the reference to a different function in memory, but I am not sure and would like a bit of clarification.
Thanks!
Non static properties (alias fields, alias member variables) have their own copy for each object instance of a class.
Methods, whether static or not, only exist in one copy for each class.
That makes sense, if you think about it: there’s no reason to copy a behaviuor that doesn’t change. Only the status (variables) do change.
The only difference I can think about between static and non-static methods is the visibility level, that is a non-static method “sees” the object status, while a static method can’t because it works on a class level.
edit (prove):
I have created 10,000 objects of AClass and measured that the executable occupies 6304 Kbyte, while creating 10,000 AClassStatic objects needs 6284 KB. It is different, but irrelevant.