05-31-2012, 07:53 PM
|
#1
|
|
cheers
Join Date: Mar 2007
Posts: 92
|
memory leak?
hi,
is this a memory leak? Or iam missing something...
ActionScript Code:
import flash.events.Event;
import flash.text.TextField;
import flash.system.System;
var textField:TextField = new TextField();
textField.width = 200;
addChild( textField );
var arr:Array = new Array();
addEventListener( Event.ENTER_FRAME, enterFrame );
function enterFrame( event:Event )
{
textField.text = "Total memory: "+System.totalMemory;
arr = [ Math.random(), Math.random(), Math.random(), Math.random(), Math.random(), Math.random(), Math.random(), Math.random(), Math.random() ];
}
__________________
resistance is futile!!!
|
|
|
05-31-2012, 08:00 PM
|
#2
|
|
Senior Member
Join Date: Feb 2008
Location: West Palm Beach, FL
Posts: 3,770
|
no
you're creating new arrays over and over, and this is taking up more memory
the old array still exists... technically
but it's eligible for 'garbage collection', thing is gc doesn't just run constantly, it sits around and waits until some conditions of the program are met to require the need for garbage collection. This is usually when the current system memory usage reaches some large amount it considers to large.
You can see this happening by creating new TextFields every frame (I say text fields as they're pretty massive), but not adding them to the display list, and setting them null after creating. You'll notice the memory grow and grow and grow... but then stop growing and shrink down slightly and plateau out.
If you have complicated classes that do a lot of things, you usually should stop some of its complicated stuff before nulling it out. Otherwise they'll end up still running in the background... for instance if you listen to an ENTER_FRAME event in a class that inherits from Sprite... that ENTER_FRAME listener may fire for a long time even after you're done using it.
This is usually referred to as 'disposing', and some people have an 'IDisposable' interface described for that use. This basically just tells the object "hey, I'm done with you, stop your complicated nonsense, dump any auxiliary references, and prepare yourself for destruction... whenever that may come". The reason? Because you don't have control over when it actually gets destroyed.
__________________
www.lordofduct.com - come read my blog!
If you want to know how to program, take a math class, take a lot of math classes!
Last edited by lordofduct; 05-31-2012 at 08:04 PM.
|
|
|
05-31-2012, 08:23 PM
|
#3
|
|
cheers
Join Date: Mar 2007
Posts: 92
|
tnx for a quick reply
i thought that could be the case, but i tried it with plain Number and same thing happens, it takes a while but memory rises constantly and never drops
ActionScript Code:
import flash.events.Event;
import flash.text.TextField;
import flash.system.System;
var textField:TextField = new TextField();
textField.width = 200;
addChild( textField );
var number:Number = 0;
addEventListener( Event.ENTER_FRAME, enterFrame );
function enterFrame( event:Event ){
textField.text = "Total memory: "+System.totalMemory;
number = 0.012;
}
__________________
resistance is futile!!!
Last edited by podlium; 05-31-2012 at 08:31 PM.
|
|
|
05-31-2012, 08:37 PM
|
#4
|
|
Senior Member
Join Date: Mar 2009
Location: Sweden
Posts: 9,776
|
Number shouldn't cause any memory allocations.
Also, it is normal for memory to increase for a while. The garbage collector will not reclaim memory until there is a decent need for it to. You will see a sawtooth pattern in the memory consumption if the garbage collector is being run and reclaiming stuff.
__________________
Signature: I wrote a pair of articles about the timeline.
|
|
|
05-31-2012, 08:40 PM
|
#5
|
|
Senior Member
Join Date: Feb 2008
Location: West Palm Beach, FL
Posts: 3,770
|
your on the timeline, that can introduce some weirdness if the movieclip is running the frame script over and over again.
I just did it in a Document class and that didn't happen with 'number'.
Try putting a 'trace' at the beginning of the script and see if it's repeating itself.
If it is, try putting 'stop();' at the end of that script... and see if that trace stops and the memory stops growing.
If this all works out, it still isn't a memory leak as the old textfield and script (from looping over) isn't stuck in memory, it's just not garbage collected yet... it'll be garbage collected in time.
I'd do it myself, but this computer only has FlashDevelop on it
Here's that document class:
ActionScript Code:
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.text.TextField;
import flash.system.System;
/**
* ...
* @author dylan
*/
public class Main extends Sprite
{
private var _tf:TextField;
private var _num:Number;
private var _arr:Array;
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
_tf = new TextField();
_tf.width = 200;
addChild(_tf);
this.addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onEnterFrame(e:Event):void
{
_tf.text = "Total memory (mbytes): " + System.totalMemory / (1024 * 1024);
_num = 0.012;
//_arr = [ Math.random(), Math.random(), Math.random(), Math.random(), Math.random(), Math.random(), Math.random(), Math.random(), Math.random() ];
}
}
}
__________________
www.lordofduct.com - come read my blog!
If you want to know how to program, take a math class, take a lot of math classes!
Last edited by lordofduct; 05-31-2012 at 08:46 PM.
|
|
|
05-31-2012, 09:25 PM
|
#6
|
|
cheers
Join Date: Mar 2007
Posts: 92
|
Arghhh, whole day of coding got to have some bad affect  damn
yes, it was timeline weirdness, i had trouble with array and wanted to test it in a separate file, and added the code on the timeline... but i didnt know about this timeline s***, it doesn't loops i checked, but its working differently then external class, memory just keeps on rising... o men, need to remember not to test code on the timeline... its all good now, THANKS A LOT
__________________
resistance is futile!!!
|
|
|
| Thread Tools |
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT. The time now is 02:44 PM.
///
|
|