Copy link to clipboard
Copied
I performed a benchmark test memory consumption by three DisplayObjects - MovieClip, Sprite, and Shape.
Below are results and method used.
I just wanted to create instances. Test with adding instnaces to display list showed a slight difference in terms of memory consumption.
Observations:
Sprite uses from 16 to 22% with average 18% less memory than MoviClip.
Shape uses from 42 to 55% with average of 46% less memory than MovieClip.
Shape uses from 32 to 42% with average of 35% less memory than Sprite.
Results. Numbers in the first row are loop iteration values. Columns show bites consumed by objects and fraction of differences as compared to MovieClip performance.
50 | vs MC | 100 | vs MC | 1000 | vs MC | 5000 | vs MC | 10000 | vs MC | 50000 | vs MC | Avg% | |
MovieClip | 36864 | 69632 | 614400 | 3137536 | 6225920 | 31121408 | |||||||
Sprite | 28672 | 0.22 | 53248 | 0.24 | 512000 | 0.17 | 2650112 | 0.16 | 5255168 | 0.16 | 26259456 | 0.16 | 0.19 |
Shape | 16384 | 0.56 | 36864 | 0.47 | 335872 | 0.45 | 1761280 | 0.44 | 3567616 | 0.43 | 17358848 | 0.44 | 0.47 |
Shape vs Sprite | 0.43 | 0.31 | 0.34 | 0.34 | 0.32 | 0.34 | 0.35 |
Code:
var tf:TextField = new TextField();
addChild(tf);
var mem:uint = 0;
var i:int = 0;
var arr:Array = [];
var numIterations:int = 1000;
mem = System.totalMemory;
for (i = 0; i < numIterations; i++)
{
arr.push(new MovieClip());
}
tf.text = "m " + numIterations + " - " + String(System.totalMemory - mem);
Copy link to clipboard
Copied
here's another test under more realistic conditions that shows no significant difference between movieclip memory and shape memory:
//////////////////////////////////
var rad:int = 1;
var rct:Shape = new Shape();
rct.graphics.beginFill(0xff0000);
rct.graphics.drawRect(0, 0, 100, 100);
addChild(rct);
rct.x = stage.stageWidth * .5 - 50;
rct.y = stage.stageHeight * .5 - 50;
var container:Sprite = new Sprite();
container.x = stage.stageWidth * .5;
container.y = stage.stageHeight * .5;
addChild(container);
var sTimer:Timer = new Timer(10, 501);
sTimer.addEventListener(TimerEvent.TIMER, onTick);
sTimer.start();
function onTick(event:TimerEvent):void {
var s:MovieClip = new MovieClip()
//var s:Shape = new Shape();
s.cacheAsBitmap = true;
s.graphics.beginFill(0x000000, 1/255);
s.graphics.drawCircle(rad, rad, rad);
s.x = s.y = -rad;
container.addChild(s);
if (rad%100==1) {
trace(rad,Math.round(System.privateMemory/1024),Math.round(System.totalMemory/1024))
}
rad ++;
}
/////////////////////////////////
shape:
1 299376 32356
101 299388 40272
201 299388 77332
301 396980 180136
401 592848 376004
501 917356 697616
movieclip:
1 299924 32356
101 298384 40204
201 298384 78360
301 395376 179500
401 590748 374868
501 914668 695788
Copy link to clipboard
Copied
Wouldn't there be a processing overhead too? It would also be small of course, but as MovieClips have more properties there would be more things to be checked especially when adding to the display list...
Copy link to clipboard
Copied
This is getting even more interesting because different methodologies show different results.
The one I did - all the instances were created within the same loop. kglad demonstrated that when there is a period of time between instantiations difference is really small.
Actually, when I was doing it first I also observed different results depending on when memory is read. I suspect that runtime environment re-allocates and manages memory constantly. I am not sure but it maybe that in some time after instance is created it is, so to speak, reprocessed again. Sort of there is notion (ghost) of instance left and it is partially dispersed... Spooky... Needa think bout it...
Copy link to clipboard
Copied
it's not a time difference. here are the results using a for-loop instead of a timer loop:
movieclip:
1 224080 23768
101 224080 24144
201 224080 24512
301 224080 24876
401 224080 25232
501 224080 25600
shape:
1 262520 23832
101 262520 24192
201 262520 24540
301 262520 24884
401 262520 25220
501 262520 25568
shape:
1 264624 23868
101 264624 24228
201 264624 24576
301 264624 24920
401 264624 25256
501 264624 25604
movieclip:
1 263656 23872
101 263656 24248
201 263656 24616
301 263656 24980
401 263656 25336
501 263656 25704
Copy link to clipboard
Copied
Why do you think there is the difference between these two methods?
Copy link to clipboard
Copied
All things equal, method call in the loop on 1000 iterations takes over 100 ms longer than pure loop.
My main concern is that the approach of calling method inside the loop is not as straightforward for this test purpose. I understand loop cannot be paused in its purest execution. Method might be vulnerable. This may mean that, for instances, if Flash decides to kick in extra processing it may be able to do so inside the method but not in the loop.
I checked time between iterations. Pure loop is smoother - average period between calls is 25% less than when method is called. Standard deviation is 122 for methods call and 91 for pure loop.
Again, if there is any kind of post processing and redistribution few milliseconds is plenty of time to do that.
Copy link to clipboard
Copied
well, there are all sorts of things going on in the counter-point code i showed (which is your code).
my point is only that in real-world apps, there's no significant difference in memory utilization between the various display objects. i suspect all the other things going on in that code overwhelms any difference beween the displayobjects.
and that's the way it always seems when i check a real app: there's no memory benefit to using less flexible display objects than movieclips.
Copy link to clipboard
Copied
Hey guys..
Now THAT was some real usefull info!
Nice!!!
Best regards
Peter