views:

61

answers:

1

We're currently building a Flex application using the PureMVC framework with a shell.swf which loads/unloads modules dynamically. We're experiencing issues with memory and we're looking to replace the shell.swf with a JavaScript-based loader.

Two questions:

  1. What would be a good framework to dynamically load the Flex components and allow the modules to communicate between each other?

  2. Would dynamically loading/unloading the modules via JavaScript take care of the typical memory problems associated with Flex/Flash applications?

+3  A: 

If you need separate Flash elements on a page to communicate with each other you'll probably end up using LocalConnection. That being said, you'll need to look into using Runtime Shared Libraries (RSL) for the Flex framework - otherwise each of your SWFs will have to contain it's own copy of the Flex framework.

Now, that being said, memory related issues don't really stem from the framework, instead they come from problems related to object references and possibly CPU hogging.

Flash's garbage collector only runs when it has time to do so, so if your app is spiking the CPU fairly constantly, the GC may never run. If you run your app in debug mode with Flex you can force the GC to run to see if that's the case.

Flash's GC is based on a mark-and-sweep concept. Objects that exist but don't have any references to them are first marked, and then later swept up bug the GC. This means if you leave references to "dead" objects around they will never get freed. A common culprit in this are events and event listeners. It's generally a best practice to always use weak keys (avoids making a reference that's counted by the GC) with addEventListener.

// don't do this
foo.addEventListener(Event.CHANGE, onChange);

// do this
foo.addEventListener(Event.CHANGE, onChange, false, 0, true);

Grant Skinner has an excellent series on resource management in AS3 you should check out as well.

Branden Hall