November 25th, 2009

Memory Management: Use the Lightest Classes You Can.

David and I have been teaching Flex at a local college.  Recently David mentioned to a student that he should use a lighter-weight class for a mask than Canvas.  The student asked why.  This brings up a good point.

When you’re using various Flex framework classes, you need to stop and think.  Do I really need the class I’m using or can I get away with something smaller.  One good example is when you’re creating custom MXML components.  Many times a rookie developer will create an MXML component with a Container as the root component when all they really need to do is to make a UI Control the root component.

In the case of the question, when using a mask, a very lightweight class the Sprite could have been used if the student had not needed the mask to participate in the display list.  Otherwise, they could have used a custom class based on UIComponent, vs using the larger Canvas class.

Just to re-enforce David’s point, I created a little POC where I created three custom classes, one based on Canvas, one based on UIComponent, and one based on Sprite.  I did not create any custom code in these classes, they were simply used so that they’d be easier to call out in the Flex Builder Profiler.  Then I profiled the application and here’s a screenshot of the results.

As you can see, there can be significant memory impact when you use classes that are more robust than you need.

That being said, pay attention to the classes you use in your code, and always stop to think, do I really need to use this class, or can I get away with something that is lighter-weight?

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.

Written Jun

Flex Authority Vol. 1 Issue 2
InsideRIA
Fusion Authority Quarterly Update

Gamer Jun

Categories

Credits

FireStats icon Powered by FireStats