setting variables in MXML while avoiding data binding?
I love MXML but am starting to notice some of its drawbacks. The main one I'm struggling with is its larger memory usage due to the need for binding. In the super simple code snippet below, which I've tested on Flex 4.0
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
<fx:Script>
<![CDATA[
import mx.events.FlexEvent;
[Bindable] private var xPos:Number = 20;
[Bindable] private var yPos:Num开发者_StackOverflow社区ber = 20;
[Bindable] private var testo:String = "hello";
[Bindable] private var colorVar:uint = 0x212122;
private const xPosConst:Number = 20;
private const yPosConst:Number = 20;
private const testoConst:String = "hello";
private const colorVarConst:uint = 0x212122;
]]>
</fx:Script>
<!-- <s:Label x="20" y="20" text="hello" color="0x212122"/>-->
<s:Label x="{xPos}" y="{yPos}" text="{testo}" color="{colorVar}"/>
<!-- <s:Label x="{xPosConst}" y="{yPosConst}" text="{testoConst}" color="{colorVarConst}"/>-->
</s:Application>
the variables for an mxml labels get set using either (a) magic numbers, (b) [Bindable] variables and (c) consts. Interestingly (to me at least) scenario (c) takes up more memory than (a) but less than (b) - I would have assumed it'd be equal to one of the two and was hoping it would be the same as (a) but I guess not.
My main question: is there any way to set variables in MXML that doesn't eat up memory? I'm aware that I can do this explicitly in AS and keep MXML as a bare bone structure but was wondering if there was an alternative path.
Secondary question: why memory usage in c different from a and b? I've looked up at the intermediate code and it seems like the const case still sets up some binding logic but not as much and have been wondering why that is.
thank you!
Well, it's kind of a loaded question. No matter what you do, if you add code, you're going to use memory. Now, 'eat up memory' is relative to what you're doing and I would love to see more information on the memory allocation of each use case.
As for why one uses more memory than the other is a fairly simple (I think). It has everything to do with how Flash manages objects. By default, Flash uses reference base variables, however primitives do not count and are duplicated over. For example:
var a:Object = {};
var b:Object = a;
// then a === b as in the same reference in memory (check it in debugger)
// However
var a:String = 'weee';
var b:String = a;
// Then a !== b since the String is duplicated in memory
The reason for this is because they wanted to keep Actionscript simple enough and not have the developers have to worry about memory references (like using the '&' in front of a variables in C or Java for a variable). But as you've pointed out, the caveat of this is the duplication of primitives. In the grand scheme of things, it's very minimal compared to everything else being created, plus I'm fairly sure you'll be using models and such to store your data as you should ;)
As for the 'magic numbers' placed inline, I believe the reason why that takes room is because it's now storing the String version of that value and the 'real' version (since mxml does the casting for you). As for the constants, I'm not exactly sure why that's using more memory; I would of imagined it would be exactly the same as the variables since it's duplicating both, but I could be because it needs to 'lock' that variable so nobody can change it.
I would recommend you try to do the same test but with Objects and models instead to see what you find out. I would imagine that a generic Object would use more memory than a Class, but it would be neat to see what you find :)
EDIT: Forgot to point out that it wouldn't really matter if it's MXML or pure AS, they should work exactly the same way (except for the 'magic numbers' one). Binding does add extra memory of course, but it's a tradeoff; less memory or the utility of binding. I would also suggest you try to do the same in pure Actionscript and compare it to it's mxml counterpart (using BindingUtils for binding in AS) and show those results as well.
精彩评论