Highlighted

Anonymous function closure, event listener and GC, how they work?

Community Beginner ,
Aug 29, 2014

Copy link to clipboard

Copied

I'm experimenting with GC and getting results that I don't understand. The code below create 10 red circles and starts to move them along x-axis, one circle each second. The moving is implemented with Tween object, which keeps calling yoyo() each time the tween finishes. The moving should last forever, but running GC stops it. You see, that I don't keep references to the tween object, which is of course a very bad idea, but the purpose is to understand how GC works. As the tween's callback is anonymous function, the function closure keeps reference to the tween object and the as the tween object stores the function closure, this should not be GC'd. Right? Or, as I assume, the GC is clever enough to find these "islands"? But as my code often leaks memory, it can't detect all kinds of islands - where's the limit?

import flash.display.Sprite;

import fl.transitions.Tween;

import fl.transitions.TweenEvent;

import fl.transitions.easing.Regular;

import flash.utils.Timer;

import flash.events.TimerEvent;

var floatingElements:Array = new Array();

for (var i:int = 0 ; i<10 ; i++) {

  var s:Sprite = new Sprite();

  s.graphics.lineStyle(1, 0xff0000, 1);

  s.graphics.drawCircle(10, 10, 10);

  s.x = 50;

  s.y = 50 + 30 * i;

  addChild(s);

  floatingElements.push(s);

}

function floatElement():void {

  if (floatingElements.length > 0) {

  var num = floatingElements.shift();

  var floatTween:Tween = new Tween(num, "x", Regular.easeInOut, num.x, num.x+10, 2, true);

  floatTween.addEventListener(TweenEvent.MOTION_FINISH, function(tweenEvent:TweenEvent) {

  floatTween.yoyo();

  });

  }

}

var t1:Timer = new Timer(1000, 0);

t1.addEventListener(TimerEvent.TIMER, function(e:*) {

  floatElement();

});

t1.start();

var t2:Timer = new Timer(5000, 0);

t2.addEventListener(TimerEvent.TIMER, function(e:*) {

  System.gc();

});

t2.start();

TOPICS
Development

Views

159

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Anonymous function closure, event listener and GC, how they work?

Community Beginner ,
Aug 29, 2014

Copy link to clipboard

Copied

I'm experimenting with GC and getting results that I don't understand. The code below create 10 red circles and starts to move them along x-axis, one circle each second. The moving is implemented with Tween object, which keeps calling yoyo() each time the tween finishes. The moving should last forever, but running GC stops it. You see, that I don't keep references to the tween object, which is of course a very bad idea, but the purpose is to understand how GC works. As the tween's callback is anonymous function, the function closure keeps reference to the tween object and the as the tween object stores the function closure, this should not be GC'd. Right? Or, as I assume, the GC is clever enough to find these "islands"? But as my code often leaks memory, it can't detect all kinds of islands - where's the limit?

import flash.display.Sprite;

import fl.transitions.Tween;

import fl.transitions.TweenEvent;

import fl.transitions.easing.Regular;

import flash.utils.Timer;

import flash.events.TimerEvent;

var floatingElements:Array = new Array();

for (var i:int = 0 ; i<10 ; i++) {

  var s:Sprite = new Sprite();

  s.graphics.lineStyle(1, 0xff0000, 1);

  s.graphics.drawCircle(10, 10, 10);

  s.x = 50;

  s.y = 50 + 30 * i;

  addChild(s);

  floatingElements.push(s);

}

function floatElement():void {

  if (floatingElements.length > 0) {

  var num = floatingElements.shift();

  var floatTween:Tween = new Tween(num, "x", Regular.easeInOut, num.x, num.x+10, 2, true);

  floatTween.addEventListener(TweenEvent.MOTION_FINISH, function(tweenEvent:TweenEvent) {

  floatTween.yoyo();

  });

  }

}

var t1:Timer = new Timer(1000, 0);

t1.addEventListener(TimerEvent.TIMER, function(e:*) {

  floatElement();

});

t1.start();

var t2:Timer = new Timer(5000, 0);

t2.addEventListener(TimerEvent.TIMER, function(e:*) {

  System.gc();

});

t2.start();

TOPICS
Development

Views

160

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Aug 29, 2014 0

Have something to add?

Join the conversation