views:

80

answers:

2

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!

A: 

The methods are not "copied" as you call it, the classes just have a reference to them. object1a + object1b might take minimal more memory to store these references but thats negligible in most any case.

DO NOT make member functions static to save memory. If you do it you do something wrong.

Have you actually got any metrics testing this? What are your sources? I imagine you're right, but I'd rather not go off intuition. Thanks!
M2tM
I tested it with taskmanager once creating a lot of objects with static / non static functions.
I was hoping for a little more granularity in the response, I'm holding off on accepting this as the answer for that reason. Taskmanager is poor for benchmarking memory use to the point of being nearly worthless.
M2tM
+1  A: 

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):

AClassStatic.as
class AClassStatic
{
    public static function f():Void  { return ; }
    public static function g():Void  { return ; }
        public static function h():Void  { return ; }
}


AClass.as
class AClass
{
    public function f():Void { return ; }
    public function g():Void { return ; }
    public function h():Void { return ; }
}

test.fla
import AClass
import AClassStatic

var obj1:AClass  = new AClass ();
var obj2:AClassStatic = new AClassStatic ();

if ( sizeof(obj1) == sizeof(obj2) )
    trace("equal");

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.

vulkanino
Thank you, that is basically exactly what I was looking for. When you say the executable occupies 6304 KB vs 6284 KB do you mean the size of the executable, or the highwatermark of memory use during execution?
M2tM
I meant the memory use, sorry.
vulkanino