views:

896

answers:

1

I have have a class that I wrote, and it seems bigger than it should be. It doesn't extend anything, and has very little going on - or so I thought - but each one is taking up just under 100k100 bytes ( thanks back2dos ). I guess that I don't have a very good understanding of what really affects how much memory an object takes up in AS3.

If anyone can point me to some reading on the subject that might be helpful, or perhaps explain some insight into how to think about this, that would be awesome.

I would like to keep a LOT of these objects in memory - and I thought I could until now, but at this size I'm going to have to create them or use an object pooling technique of some kind.

Thanks for the assistance.

Edit: Although I've got this in order, I'm keeping the code I posted here for completeness. The class has been heavily modified from the original version. Values that were referencing other files have been made static as to allow the code to run for someone else ( in theory hehehe... ).

Although my situation is sorted out, I'll give the answer to a good reference for information on classes and memory.

In this case the class has 15 variables. I'm only using a single String and a bunch of ints, Numbers, and Booleans with some references to more of the same in globally available XML data. It also imports Point for the constructor, though no points are stored. In testing, even without the global XML references or Point class it's still around a ~84k each. There are getters for 7 of the variables and a couple methods in addition to the constructor. All of which are less than 20 lines ( and I have a very sparse coding style ).

The class mentioned for reference, but feel free to generalize:

package
{
    public class AObject
    {
     private var _counter:int;
     private var _frames:int;
     private var _speed:int;

     private var _currentState:String;
     private var _currentFrame:int;

     private var _offset:int;
     private var _endFrame:int;

     private var _type:int;
     private var _object:int;
     private var _state:int;

     private var _x:Number;
     private var _y:Number;

     private var _w:int;
     private var _h:int;

     private var _update:Boolean;

     public function AObject( targetX : int,  targetY : int,  state : int,  object : int,  type : int )
     {  

      _x = targetX;
      _y = targetY;

      _type = type;
      _object = object;
      _state = state;
      _counter = 0;
      _w = 32;
      _h = 32
      _update = true;
      setState( state );
     }

     public function setState( state:int ) : void
     {
      _currentState = "bob";
      var frameCounter : int = 0;
      var stateCounter : int = state - 1;
      while ( state > 0 )
      {
       frameCounter += 4;
       --stateCounter; 
      }
      _offset = frameCounter;
      _currentFrame = _offset;
      _speed = 10;
      _frames = 4;
      _endFrame = _offset + _frames - 1;
     }

     public function get state() : int
     {
      return _state;
     }

     public function animate() : Boolean
     {
      if ( count() )
      {
       if( _currentFrame < _endFrame )
       {
        ++_currentFrame;
       }
       else
       {
        _currentFrame = _offset; 
       }
       _speed = 10;
       return true;
      }
      else
      {
       return false;
      }
     }

     private var adder: Number = 0;

     private function count():Boolean
     {
      _counter++;
      if ( _counter == _speed )
      {
       _counter = 0;
       return true;
      }
      else
      {
       return false;
      }
     }

     public function get x():int
     {
      return _x;
     }

     public function get y():int
     {
      return _y;
     }

     public function get type():int
     {
      return _type;
     }

     public function get object():int
     {
      return _object;
     }

     public function get currentFrame():int
     {
      return _currentFrame;
     }

     public function get w():int
     {
      return _w;
     }

     public function get h():int
     {
      return _h;
     } 
    }
}
+3  A: 

i am amazed, this compiles at all ... when i try to compile it with the flex SDK, it creates an enormous collision with the built-in class Object, which is the base class of any class, making my trace output overflow ...

other than that, this is an infinite loop if you pass a value for state bigger than 0

while ( state > 0 )
{
  frameCounter += 4;
  --stateCounter; 
}

but it seems really strange these objects are so big ... after renaming and taking care not to pass in 0 for the state, i ran a test:

package {
    import flash.display.Sprite;
    import flash.sampler.getSize;
    import flash.system.System;
    public class Main extends Sprite {
     public function Main():void {
      const count:int = 100000;
      var start:uint = System.totalMemory;
      var a:Array = [];
      for (var i:int = 0; i < count; i++) {
       a.push(new MyObject(1, 2, 0, 4, 5));
      }
      var mem:uint = System.totalMemory - start - getSize(a);
      trace("total of "+mem+" B for "+count+" objects, aprox. avg. size per object: "+(mem/count));
     }  
    } 
}

it yields:

total of 10982744 B for 100000 objects, aprox. avg. size per object: 109.82744

so that's quite ok ... i think the actual size should be 4 (for the bool) + 4 * 11 (for the ints) + 4 (for the reference to the string) + 8 * 3 (for the three floats (you have the adder somewhere over the count) + 8 for an empty class (reference to the traits objects + something else), giving you a total of 88 bytes ... which is, what you get, if you getSize the object ... please note however, that getSize will only give you the size of the object itself (as calculated here) ignoring the size of what strings or other objects your object references ...

so yeah, apart from that name you definitely should change, the problem must be somewhere else ...

back2dos
I renamed stuff for the post, it's not my exact code. I changed that. I also changed a number of variables that are relying on other files to be fixed values here which accounts for alot of the weirdness you see.When I run the Flex profiler I get get about 12000 now when I run it. I guess I was assuming the memory being used was Kb no b, since the memory monitor is tracking kb. Hmmm... so I guess things are in order and I was misreading what I was seeing in this case. Thanks for straightening me out. I've updated the post to note that the code has been modified.
grey