Obecto Training Portal

Icon

sharing our knowledge

Optimizing Flex Applications

DOWNLOAD SLIDES
DOWNLOAD SAMPLE WITH MEMORY LEAKS

This is the talk we gave at the Bulgarian FlexCamp this year – the event was in Bulgarian, and so is the talk:

Basically optimizations affect two areas of non-functional properties of the software – memory consumption and performance. Since Garbage Collection in Flash affects both memory and performance, it is very important to try describe how the GC works as detailed as we can.

How the GC works?

The part with the GC is mostly taken from the presentation accompanying the Alex Harui’s Garbage Collection and Memory Leaks blog post. The important things to note in this topic are:

  • how the Flash memory allocation works – it prefers to allocate big chunks less often, instead of small chunks frequently
  • the GC is only triggered by allocation
  • the GC doesn’t run completely – there is no guaranty that all unused memory is going to be released (or at least it will not be released in a single pass of the GC)

The conclusion you must absolutely remember is that:

The GC is not predictable!

How to detect memory leaks?

Thanks to the Flex profiler now we can easily observe the memory consumption of a Flex or Flash application. We can interactively run the GC when we want and we can compare memory consumption during various stages of the application. The leaking memory consumption pattern is pretty clear. And so is the stable memory consumption.

Fixing Memory Leaks

Now that we now whether our application is leaking, we need to take a course of action to fill in the leaking spots. Again we can try using the profiler and its feature to detect loitering objects. When you browse through the list of such objects you need to pay special attention to the usual suspects:

  • Array
  • Object used as map
  • Dictionary with strong references
  • failure to remove event listeners

Which event listeners can cause leaks?

We’ll see why children adding listeners to their parents are causing leaks and why there is no such problem in the vice versa situation.

Unloading Third-Party Content

When unloading modules and third-party content be sure to:

  • free bitmap memory
  • stop video streams
  • stop audio streams
  • stop all MovieClips from animating
  • remove event listeners to global list of enterFrame, exitFrame, etc.
  • stop any downloads (http, sockets, FileReference)
  • clear any fonts from the font table

You can use Loader.unloadAndStop() (but only in Flash 10).

Optimization Rules of Thumb

The following are performance optimization rules:

  • always compile in strict mode
  • use typed data structures
  • use sealed classes instead dynamic classes
  • avoid globals when code is deeply nested
  • use vector<> instead Array (only in Flash 10)

Other optimization techniques

Important techniques to mention are:

  • object reuse, especially for renderers
  • avoid the setStyle() method
  • avoid having too many nested containers

LEARN MORE OPTIMIZATION TECHNIQUES AND PRACTICES