views:

144

answers:

1

in an attempt to see and hopefully understand actionscript's garbage collector, i've set up a sample project that loop-tweens the value of a pixel bender parameter on stage.

my first concern was the amount of memory that was being used at launch (~26 MB). while i like to believe i'm cautious about memory by removing event listeners and nulling useless objects for the garbage collection as much as possible, i also believe i don't fully grasp where, why and when it works.

the trace of the total system memory displayed a steady rise starting from around 26 MB until around 28 MB after about a minute later (or so). suddenly it's plummeted down to 25 MB only to continue rising once again. this seems to cycle over and over.

here are some questions that come to mind: 1. is there a general time delay for the garbage collector? 2. does it activate after a certain amount of memory has been allocated? 3. can objects be explicitly removed immediately without relying on the garbage collector? 4. what is an acceptable range for memory usage when running flash?

attached is my code.

import fl.transitions.*;
import fl.transitions.easing.*;

var shader:Shader;
var shaderFilter:ShaderFilter;
var motionTween:Tween;
var filterParameter:Number = 0.0;
var loader:URLLoader = new URLLoader();
var phase:Boolean = false;

loader.dataFormat = URLLoaderDataFormat.BINARY;
loader.addEventListener(Event.COMPLETE, dataLoaded);
loader.load(new URLRequest("myBoringFilter.pbj"));

function dataLoaded(e:Event):void
 {
 loader.removeEventListener(Event.COMPLETE, dataLoaded);
 shader = new Shader(e.target.data);
 shaderFilter = new ShaderFilter(shader);
 flower.filters = [shaderFilter];

 tweenLoop(null);
 }

function tweenLoop(e:TweenEvent):void
 {
 if (motionTween != null)
  {
  motionTween.removeEventListener(TweenEvent.MOTION_CHANGE, updateFilter);
  motionTween.removeEventListener(TweenEvent.MOTION_FINISH, tweenLoop);
  motionTween = null;
  }

 phase = !phase;

 if (phase == true)
  {motionTween = new Tween(this, "filterParameter", Regular.easeOut, filterParameter, 100.0, 2.0, true);}
  else
  {motionTween = new Tween(this, "filterParameter", Regular.easeOut, filterParameter, -100.0, 1.0, true);}

 motionTween.addEventListener(TweenEvent.MOTION_CHANGE, updateFilter);
 motionTween.addEventListener(TweenEvent.MOTION_FINISH, tweenLoop);
 }

function updateFilter(e:TweenEvent):void
 {
 shader.data.amount.value = [filterParameter];
 flower.filters = [shaderFilter];

 //Update Memory
 trace("System Total Memory: " + System.totalMemory);
 }
+1  A: 

This is generally the most common resource for this question:

http://www.gskinner.com/blog/archives/2006/06/as3_resource_ma.html

Matt W
thanks for the link
TheDarkInI1978
Just a note: that article is very old and doesn't (entirely) apply to AS3.
David Wolever