I was recently just messing around with ModuleLoader and noticed some funny 
things when using a stand alone player vs a browser.

Here is the code I am using

Main.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"; layout="absolute" 
addedToStage="init();">
        
        <mx:Script>
                <![CDATA[
                        import mx.events.ModuleEvent;
                        import mx.modules.ModuleLoader;
                
                        private var moduleLoader:ModuleLoader;
                        
                        private var loadTimer:Timer = new Timer(2000, 1);
                        private var unloadTimer:Timer = new Timer(2000, 1);
                        
                        private function init():void
                        {
                                
loadTimer.addEventListener(TimerEvent.TIMER_COMPLETE, ltc);
                                
unloadTimer.addEventListener(TimerEvent.TIMER_COMPLETE, ultc);
                                
                                loadTimer.start();
                        }
                        
                        private function ltc(event:TimerEvent):void
                        {
                                loadModule();
                        }
                        
                        private function ultc(event:TimerEvent):void
                        {
                                unloadModule();
                        }
                        
                        private function loadModule():void
                        {
                                if (!moduleLoader)
                                {
                                        trace("creating new module");
                                        moduleLoader = new ModuleLoader();
                                        addChild(moduleLoader);
                                        
moduleLoader.addEventListener(ModuleEvent.READY, moduleReadyHandler);
                                }
                                
                                moduleLoader.url = "Module1.swf";
                                moduleLoader.loadModule();
                        }
                        
                        private function 
moduleReadyHandler(event:ModuleEvent):void
                        {
                                trace("module loaded");
                                
moduleLoader.removeEventListener(ModuleEvent.READY, moduleReadyHandler);
                                
                                unloadTimer.reset();
                                unloadTimer.start();
                        }
                        
                        private function unloadModule():void
                        {
                                
moduleLoader.addEventListener(ModuleEvent.UNLOAD, moduleUnloadHandler);
                                moduleLoader.unloadModule();
                        }
                        
                        private function 
moduleUnloadHandler(event:ModuleEvent):void
                        {
                                trace("module unloaded");
                                
moduleLoader.removeEventListener(ModuleEvent.UNLOAD, moduleUnloadHandler);
                                removeChild(moduleLoader);
                                moduleLoader = null;
                                
                                loadTimer.reset();
                                loadTimer.start();
                        }
                        
                ]]>
        </mx:Script>
</mx:Application>


Module1.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Module xmlns:mx="http://www.adobe.com/2006/mxml"; layout="absolute"
        width="100%" height="100%">
        <mx:PieChart />
        
</mx:Module>

Basically this application continuously loads and then unloads the same module 
over and over. When running this app with the Flex Builder profiler I was 
pretty much what I expected, the memory usage when up util flash player decided 
it need to allocate more memory at which time it runs the garbage collection 
and freed up most of the memory in use. After letting the app run for a while I 
determined that garbage collector was working as I expected and the memory was 
staying within an expected range.

When I opened task manager I noticed a completely different thing, the browser 
was using more and more memory and never releasing any of it. I tried this app 
in ie, ff, and chrome and found the same to be true for all browsers. So being 
curious as I am I thought well lets try thi s in a stand alone flash player and 
what do you know it worked just as expected and the memory usage never got out 
of control.

Now I know that the code above is unrealistic but I was wondering if anyone 
could shed light on why the browsers do not release the memory even after the 
flash player has. Or is there something terribly wrong with this test code and 
is they why I am seeing this. Any insight would be great.

Reply via email to