Memory Optimization Flash Action Script 3

Hi, Every one here is another important article. Which i see really few have discussed, thats is memory optimization in Flash Action Script 3. After all improvements still garbage collection is a bit slower i think. Or I guess there is no garbage collection. For example if i declare var sound:Sound=new Sound(); now if i use new keyword to allocate memory. There is no way to de-allocate memory. Now if i call following line sound=new Sound(); This will cause a new memory to be allocated to the Sound Object and the old memory refrence will be lost but memmory will reside untill the end of applicatio. Now consider a Flash Media Player with a play list and each time a Sound object is Allocated new memory, than you can imagine the memory leak. Its strange there is no way to deallocate a memory after it is allocated by "new" keyword. Although there is delete keyword in flash actionscript 3. For example if i write delete sound; This statement will give Error that "1189: Attempt to delete the fixed property sound. Only dynamically defined properties can be deleted." Now sound object although it is dynamically created but it can’t be destroyed. Solution that i found is to create an ParentObject of object class and than create the sound object as a dynamic property of ParentObject. 1- Declare parent object var playerObject:Object = new Object(); 2- Create the sound object as dynamic property of this object. playerObject.sound = new Sound(); 3- Delete the memory allocated at the end delete playerObject.sound; This time delete case will deallocate memory allocated to the dynamic property "playerObject.sound" This strategy can be used any where, to avoid the memory leaks in flash actionscript 3 applications. I have used the Sound object example as i am now working on an article for the Flash MP3 Player. And i came across this problem during its development. For Questions You can post your feed back I would appreciate your comments. Your support will encourage me to post more and more aricles. If you are looking for live assistance on any projects you are well come. I plan to start an Action Script 3 training course for beginners. Those of you intrested may contact.

Did you enjoy this post? Why not leave a comment below and continue the conversation, or subscribe to my feed and get articles like this delivered automatically to your feed reader.

Comments

If you are looking for live assistance on any projects you are well come. I plan to start an Action Script 3 training course for beginners. Those of you interested may contact.

[Reply]

jeyaganesh Reply:

Yeah very helpful to manage the memory and find out the memory leakage . Thanks

[Reply]

hi admin,
that’s really a fantastic idea of start classes on AS 3.0. I will be the first one to join classes.

– best wishes
avn.rocky

[Reply]

I went through your articel, i m facing problem of memory leakage while rotating a 3D object on mouse move . It’s speed is getting slower after it starts running. Also i m interested in classes let me know the details please!

[Reply]

HI avn rocky:

you can send me an email to mbilal@imaginationdev.com

Thank you

[Reply]

hi Rushita

You may also email me at mbilal@imaginationdev.com

i will send you the timmings when i m online and also i will provide you with my skype information or msn

Thanks

[Reply]

Hello ADMIN,

I did an example using this approach and it didn’t work. The dynamic property is turned undefined but the memory allocated still have the same size (I’m using System.totalMemory to verify before and after).

What could be wrong?

Best Regards

[Reply]

hi , there must be some other memory leak as well . Also i feel you are not getting the reference of the object that you are trying to delete.

Thanx

[Reply]

Hello,

I ran across this site is because I am having some issues with a Kiosk that I am developing. Everything works great, the videos and the VRs, but after 6 hours of use, I start getting black and red screens where my movies should be. I have myContent.unload(); and removeChildAt(1); etc, but it still seems to be a memory issue. There are alot of SWF’s, FLV’s and sounds loaded and unloaded. Any help would be greatly appreciated.

Garrett

[Reply]

hi.
trying to cut and delete sound from dynamically created movie clips. I can get the visuals off, but the sound continues. it’s a fireworks display, and the explosions go on indefinately…

here is the code…can you help? thanks


hasta.visible=false;

var uno:MovieClip = new works();
uno.x=200;
uno.y=300;
uno.alpha=.9;
addChild(uno);

var dos:MovieClip = new works2();
dos.x=100;
dos.y=170;
dos.alpha=.8;
addChild(dos);

var tres:MovieClip = new works3();
tres.x=400;
tres.y=140;
tres.alpha=.9;
addChild(tres);

var timer:Timer=new Timer(15000);
timer.addEventListener(TimerEvent.TIMER, onTimer);
timer.start();
var mc:MovieClip = new MovieClip();
addChild(mc);

function onTimer(evt:TimerEvent):void {

var numItems:Number=5;
var stageWidth:Number=stage.stageWidth;
var stageHeight:Number=stage.stageHeight;
function initClips():void {
var c:MovieClip;
for (var i:Number = 0; i < numItems; i++) {
c = new works();
randomizeClip(c);
mc.addChild(c);
}
}
function randomizeClip(clip:MovieClip):void {
clip.x=Math.random()*stageWidth;
clip.y=Math.random()*stageHeight;

var c:ColorTransform = new ColorTransform();
c.color = (Math.random() * 0xFFFFFF);
clip.transform.colorTransform=c;
clip.alpha=Math.random();
}
function onEnterFrame(event:Event):void {
var c:MovieClip;
for (var i:Number = 0; i < numItems; i++) {
c=MovieClip(mc.getChildAt(i));
randomizeClip(c);
}
}
initClips();
addEventListener(Event.ENTER_FRAME, onEnterFrame);
two.visible=false;

}
/*var someSound:Sound=new blast;
var someChannel:SoundChannel = new SoundChannel();
someChannel=someSound.play();
someChannel.stop();*/
var detimer:Timer=new Timer(27000);
detimer.addEventListener(TimerEvent.TIMER, unTimer);
detimer.start();

function unTimer(evt:TimerEvent):void {
hasta.visible=true;
hasta.play();
removeChild(mc);
removeChild(uno);
removeChild(dos);
removeChild(tres);
uno = null;
dos= null;
tres=null;
timer.removeEventListener(TimerEvent.TIMER, onTimer);
detimer.removeEventListener(TimerEvent.TIMER, unTimer);
}

[Reply]

This method of doing things doesn’t seem to work.
I tested with the following code and the totalmemory doesn’t get reduced after calling delete.

var obj : Object = new Object( );
trace( ‘total memory before ‘ + System.totalMemory );
obj.sound = new Sound( );
trace( ‘total memory after ‘ + System.totalMemory );
delete obj.sound;
trace( ‘total memory after delete’ + System.totalMemory );
System.gc();
trace( ‘total memory after gc’ + System.totalMemory );

[Reply]

Hi,

You have any example for the memory leak. for the mp3 player or any idea about how to seamlessly concatenate (more than one) MP3 streams (files)?

waiting for your reply…

thanks
Bechar

[Reply]

sorry you have already give the example..great..thank..

[Reply]

Leave a comment

(required)

(required)