# How To Dynamically Clear Memory?

**URL:** <https://forum.kirupa.com/t/how-to-dynamically-clear-memory/287253>\
**Category:** Uncategorized\
**Created:** [April 28, 2009, 6:41pm UTC](https://forum.kirupa.com/t/how-to-dynamically-clear-memory/287253 "2009-04-28T18:41:17Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paradise\_Wolf](https://avatars.discourse-cdn.com/v4/letter/p/85e7bf/32.png) [@Paradise\_Wolf](https://forum.kirupa.com/u/Paradise_Wolf)\
**Post date:** [April 28, 2009, 6:41pm UTC](https://forum.kirupa.com/t/how-to-dynamically-clear-memory/287253/1 "2009-04-28T18:41:17Z")

</div>

[COLOR=blue][FONT=Arial]The code below works but NOT properly. After few minutes, the flash animation slows down and also any other program that is running in the computer. I suspect that these routines flood the memory ( probably the stack memory because it uses recursion ) which then starts using slow virtual memory. [/FONT][/COLOR]

[COLOR=blue][FONT=Arial]There are two recursive processes initiated by two Timer types (Timer\_1 and Timer\_2 ) of addEventListener(). The functions that they call use a lot of “.draw()” and “.clone()” methods to manipulate memory-demanding bitmaps.[/FONT][/COLOR]

[COLOR=blue][FONT=Arial]For clarity sake I removed the lesser relevant part of the code.[/FONT][/COLOR]

[COLOR=blue][FONT=Arial]How to dynamically clean memory inside these routines and avoid them flooding computer’s RAM memory ?[/FONT][/COLOR]

[COLOR=blue][FONT=Arial]private function Animation( event:MouseEvent):void [/FONT][/COLOR]  
[COLOR=blue][FONT=Arial]{[/FONT][/COLOR]

[COLOR=blue][FONT=Arial] TargetClip.addChild(imgBg);[/FONT][/COLOR]

[COLOR=blue][FONT=Arial] Clip\_Area = TargetClip;[/FONT][/COLOR]  
[COLOR=blue][FONT=Arial] Source\_Clip = SourceClip;[/FONT][/COLOR]

[COLOR=blue][FONT=Arial] SpecialEffect ();[/FONT][/COLOR]

[COLOR=blue][FONT=Arial]}[/FONT][/COLOR]

[COLOR=blue][FONT=Arial]private function SpecialEffect ():void[/FONT][/COLOR]  
[COLOR=blue][FONT=Arial]{[/FONT][/COLOR]

[COLOR=blue][FONT=Arial] ClonedMovieClip = new MovieClip();[/FONT][/COLOR]

[COLOR=blue][FONT=Arial] Clip\_Area.addChild(ClonedMovieClip);[/FONT][/COLOR]

[COLOR=blue][FONT=Arial] var bitmapImage:BitmapData = new BitmapData(Source\_Clip.width, Source\_Clip.height, false, 0);[/FONT][/COLOR]

[COLOR=blue][FONT=Arial] bitmapImage.draw(Source\_Clip);[/FONT][/COLOR]

[COLOR=blue][FONT=Arial] …[/FONT][/COLOR]

[COLOR=blue][FONT=Arial] var Timer\_1:Timer = new Timer(5); //new Timer(10);[/FONT][/COLOR]

[COLOR=blue][FONT=Arial] Timer\_1.addEventListener(‘timer’, turnOnEffect1);[/FONT][/COLOR]  
[COLOR=blue][FONT=Arial] Timer\_1.start();[/FONT][/COLOR]

[COLOR=blue][FONT=Arial] var Timer\_2:Timer = new Timer(150);[/FONT][/COLOR]

[COLOR=blue][FONT=Arial] Timer\_2.addEventListener(‘timer’, Effect2);[/FONT][/COLOR]  
[COLOR=blue][FONT=Arial] Timer\_2.start();[/FONT][/COLOR]  
[COLOR=blue][FONT=Arial]}[/FONT][/COLOR]

[COLOR=blue][FONT=Arial]private function turnOnEffect1(ev:Object):void[/FONT][/COLOR]  
[COLOR=blue][FONT=Arial]{[/FONT][/COLOR]  
[COLOR=blue][FONT=Arial] var currentX = xPos / 2;[/FONT][/COLOR]  
[COLOR=blue][FONT=Arial] var currentY = yPos / 2;[/FONT][/COLOR]

[COLOR=blue][FONT=Arial] if ((Lasty != currentY) || (Lastx != currentX))[/FONT][/COLOR]  
[COLOR=blue][FONT=Arial] {[/FONT][/COLOR]

[COLOR=blue][FONT=Arial]…[/FONT][/COLOR]  
[COLOR=blue][FONT=Arial] OutputImage.applyFilter(Surface, new Rectangle(0, 0, Source\_Clip.width, Source\_Clip.height), Origin, Target);[/FONT][/COLOR]

[COLOR=blue][FONT=Arial] Buffer = SourceBitmap;[/FONT][/COLOR]  
[COLOR=blue][FONT=Arial] SourceBitmap = Bitmap1.clone();[/FONT][/COLOR]  
[COLOR=blue][FONT=Arial] }[/FONT][/COLOR]  
[COLOR=blue][FONT=Arial]}[/FONT][/COLOR]

[COLOR=blue][FONT=Arial]private function Effect2(ev:Object):void[/FONT][/COLOR]  
[COLOR=blue][FONT=Arial]{[/FONT][/COLOR]

[COLOR=blue][FONT=Arial] }[/FONT][/COLOR]
