Share 'Performance' on Delicious Share 'Performance' on Facebook Share 'Performance' on Google Bookmarks Share 'Performance' on Twitter

  1. Source and SWF
  2. Initial memory usage
  3. Memory after 300 boxes added
  4. Memory after 200 boxes removed again
  5. Memory after all boxes removed again
  6. Leave a Comment

Performance

LifeCycle & Invalidation is performance and memory optimized. All references are removed entirely when you remove a component from LifeCycle. There is an example application that you may use (or modify and use) to track the memory usage of LifeCycle. You will need a profiler to do so. A profiler is shipped with FDT or FlashBuilder.

This page shows source code of the example, the SWF and different screenshots of the memory usage. The test application source code is also available on GitHub.

Source and SWF

MemoryTest.swf
  • Hit the A key to add 50 boxes.
  • Hit the R key to remove 50 boxes.
  • Hit the U key to set a new box color and validate all boxes at the end of the frame.
MemoryTest.as
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package lifecycle.lifecycle.memory {
  import flash.display.Sprite;
  import flash.events.Event;
  import flash.events.KeyboardEvent;
  import flash.text.TextField;
  import flash.text.TextFormat;
  import flash.ui.Keyboard;
  import org.as3commons.collections.ArrayList;
  import org.as3commons.collections.framework.IIterator;
  import org.as3commons.ui.lifecycle.lifecycle.LifeCycle;


  public class MemoryTest extends Sprite {
    private var _components : ArrayList;
    private var _tf : TextField;

    public function MemoryTest() {
      addEventListener(Event.ADDED_TO_STAGE, init);
    }

    private function init(event : Event) : void {
      removeEventListener(Event.ADDED_TO_STAGE, init);
      stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);

      MemoryGlobals.lifeCycle = new LifeCycle();
      MemoryGlobals.boxColor = Math.round(Math.random() * 0xCCCCCC);
      _components = new ArrayList();
     
      _tf = new TextField();
      _tf.defaultTextFormat = new TextFormat("_sans", 10, 0x555555);
      _tf.text = "Boxes: 0";
      addChild(_tf);
    }

    private function keyDown(event : KeyboardEvent) : void {
      if (event.keyCode == Keyboard.A) add();
      if (event.keyCode == Keyboard.U) update();
      if (event.keyCode == Keyboard.R) remove();
    }
   
    private function add() : void {
      for (var i : uint; i < 50; i++) {
        var component : MemoryBox = new MemoryBox();
        component.x = Math.round(Math.random() * (stage.stageWidth - 30));
        component.y = 30 + Math.round(Math.random() * (stage.stageHeight - 60));
        addChild(component);
        _components.add(component);
      }
      _tf.text = "Boxes: " + _components.size;
    }

    private function update() : void {
      MemoryGlobals.boxColor = Math.round(Math.random() * 0xCCCCCC);
      var i : IIterator = _components.iterator();
      while (i.hasNext()) {
        MemoryBox(i.next()).invalidate();
      }
    }
   
    private function remove() : void {
      for (var i : uint; i < 50; i++) {
        var component : MemoryBox = _components.removeFirst();
        if (component) {
          component.cleanUp();
          removeChild(component);
        }
      }
      _tf.text = "Boxes: " + _components.size;
    }
  }
}
MemoryBox.as
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
package lifecycle.lifecycle.memory {
  import flash.display.Sprite;

  public class MemoryBox extends Sprite {
    protected var _lcAdapter : MemoryBoxAdapter;

    public function MemoryBox() {
      _lcAdapter = new MemoryBoxAdapter();
      MemoryGlobals.lifeCycle.registerDisplayObject(this, _lcAdapter);
      _lcAdapter.invalidate();
    }

    public function invalidate() : void {
      _lcAdapter.invalidate();
    }

    public function update() : void {
      with (graphics) {
        clear();
        beginFill(MemoryGlobals.boxColor);
        drawRect(0, 0, 10, 10);
      }
    }
   
    public function cleanUp() : void {
      MemoryGlobals.lifeCycle.unregisterDisplayObject(this);
    }
  }
}
MemoryBoxAdapter.as
1
2
3
4
5
6
7
8
9
10
11
12
13
package lifecycle.lifecycle.memory {
  import org.as3commons.ui.lifecycle.lifecycle.LifeCycleAdapter;

  public class MemoryBoxAdapter extends LifeCycleAdapter {
    override protected function onValidate() : void {
      scheduleUpdate();
    }

    override protected function onUpdate() : void {
      MemoryBox(displayObject).update();
    }
  }
}
MemoryGlobals.as
1
2
3
4
5
6
7
package lifecycle.lifecycle.memory {
  import org.as3commons.ui.lifecycle.lifecycle.LifeCycle;
  public class MemoryGlobals {
    public static var lifeCycle : LifeCycle;
    public static var boxColor : uint;
  }
}

Initial memory usage

Memory after 300 boxes added

Memory after 200 boxes removed again

Memory after all boxes removed again

The number of memory objects after all boxes are removed again equals the inital number.



Leave a Comment

You have a question or have experienced an issue? Please post it in the forum: http://sibirjak.tenderapp.com/ in order to make the discussion available at a more central place.