8 Replies Latest reply on Mar 6, 2008 1:09 AM by happyok

    SWFLoader Memory Issue

    c_infanti
      Hi guys,

      After some searching on the web can't seem to find an answer to this one...basically, I have an parent application that loads smaller applications using the SWFLoader component (called swfLoader). When you click on a drop down menu, it puts the name of the item on the menu into a string and runs the following function:

      public function loadContent(link:String):void
      {Application.application.swfLoader.load("content/" + link + ".swf");}

      However, what I have noticed (by monitoring using System.totalMemory) is that each time a new swf is loaded, memory usage goes up, until eventually the application crashes. I'm figuring I need to unload the swfs before loading a new one somehow, but no idea how. I tried putting this as the first line:

      Application.application.swfLoader.content.loaderInfo.loader.unload();

      ...but it gives me a "cannot reference a null object" error.

      Anyone have any ideas?

      Thanks!
      Chris
        • 1. Re: SWFLoader Memory Issue
          buabco Level 1
          HI try to be specially careful of removing every reference of the loaded SWF before loading a new one, this will make sure the garbage collector destroys it.

          Look specially into event listeners, since when you add one you are also adding a reference to the object itself.
          • 2. Re: SWFLoader Memory Issue
            c_infanti Level 1
            OK...I've made sure to explicitly remove any event listeners when I add them....is there any easy way to check for any other references to a loaded SWF?
            • 3. Re: SWFLoader Memory Issue
              buabco Level 1
              did it work? are you still having memory problems?
              • 4. Re: SWFLoader Memory Issue
                c_infanti Level 1
                Still having problems....after your advice I'm guessing there is still some reference to the previous .swf file floating around. Bascially everytime I load a new .swf in the SWFLoader it allocates new memory, but never seems to reclaim the memory from the previous swf.

                Is there any way to search for references to the previous swf through debug or something? I'm really at a loss here.
                • 5. Re: SWFLoader Memory Issue
                  c_infanti Level 1
                  For example...here's the complete code for one of the swfs I'm loading with the swfLoader:

                  public var masterRegion:String = Application.application.defaultRegion;
                  public var masterSchool:String = Application.application.defaultSchool;

                  // ActionScript file
                  import mx.events.DropdownEvent;
                  import mx.managers.PopUpManager;
                  import mx.core.IFlexDisplayObject;
                  import mx.utils.ObjectUtil;
                  import mx.events.ListEvent;


                  //Summary Tab*********************************************************
                  private var popXML:IFlexDisplayObject;

                  private var externalXMLSummary:XML;
                  private var loaderSummary:URLLoader = new URLLoader();
                  private var objRegionSummary:Object;


                  public function initSummary():void{
                  var request:URLRequest = new URLRequest("content/xmldata/SchoolOverview.xml");
                  popXML = PopUpManager.createPopUp(this, XMLWindow, true);
                  PopUpManager.centerPopUp(popXML);
                  PopUpManager.bringToFront(popXML);
                  loaderSummary.load(request);
                  loaderSummary.addEventListener(Event.COMPLETE, onCompleteSummary);
                  }

                  //Load initial data from XML
                  public function onCompleteSummary(event:Event):void{
                  loaderSummary.removeEventListener(Event.COMPLETE, onCompleteSummary);
                  var loader:URLLoader = event.target as URLLoader;
                  if (loader != null){
                  externalXMLSummary = new XML(loader.data);
                  PopUpManager.removePopUp(popXML);
                  //Call function to load Region Combo
                  cboxRegionSummary.dataProvider = externalXMLSummary.region.@name;
                  cboxRegionSummary.selectedItem = masterRegion
                  cboxRegionSummary.dispatchEvent(new ListEvent(ListEvent.CHANGE, false,false));
                  }
                  }

                  //Load School Combo
                  public function loadSchoolCombo(newRegion:Object):void{
                  objRegionSummary = newRegion;
                  cboxSchoolSummary.dataProvider = externalXMLSummary.region.(@name==newRegion).school.@name;
                  cboxSchoolSummary.selectedItem = masterSchool;
                  cboxSchoolSummary.dispatchEvent(new ListEvent(ListEvent.CHANGE, false,false));
                  }

                  public function sortCompareFuncValue(itemA:Object, itemB:Object):int {
                  return ObjectUtil.numericCompare(itemA.@value, itemB.@value);
                  }

                  public function sortCompareFuncPercent(itemA:Object, itemB:Object):int {
                  return ObjectUtil.numericCompare(itemA.@percent, itemB.@percent);
                  }

                  //Load Data Grid from school combo
                  public function loadDataFromCombo(newSchool:Object):void{
                  var xmldata:XMLList = externalXMLSummary.region.(@name==objRegionSummary).school.(@name==newSchool);
                  dataGridSummary.dataProvider = xmldata.overview;
                  dataGridEthnicity.dataProvider = xmldata.ethnicity;
                  dataGridGender.dataProvider = xmldata.gender;
                  dataGridGrade.dataProvider = xmldata.grade;
                  dataGridOther.dataProvider = xmldata.other;
                  dataGridAYP.dataProvider = xmldata.ayp;

                  textSummaryTitle.text = "School Overview for " + cboxSchoolSummary.selectedLabel;

                  }

                  As you can see, I remove the event listener. What else would I have to do here when loading a new swf to make sure this one is garbage collected?

                  Thanks!
                  • 6. Re: SWFLoader Memory Issue
                    c_infanti Level 1
                    And the mxml:

                    <?xml version="1.0" encoding="utf-8"?>
                    <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="initSummary()">
                    <mx:Script source="../as/SchoolOverview.as"/>
                    <mx:Style source="../css/kpi.css"/>
                    <mx:VBox height="100%" width="100%" id="vboxSchoolSummary" styleName="page">

                    <mx:VBox width="100%" height="5%">
                    <mx:Text text="School Overview" id="textSummaryTitle" styleName="controlBarBox"/>

                    </mx:VBox>
                    <mx:HBox height="90%" width="100%">
                    <mx:Spacer height="100%" width="10%"/>
                    <mx:VBox horizontalAlign="center" height="100%" width="40%" id="schoolSummaryTableBoxLeft">
                    <mx:HBox width="100%">
                    <mx:Text text="Region:" styleName="schoolSummaryDropdownText" />
                    <mx:Spacer height="100%" width="3%"/>
                    <mx:ComboBox id="cboxRegionSummary" toolTip="Select region name" styleName="dropdown">
                    <mx:change>loadSchoolCombo(ComboBox(event.target).selectedItem)</mx:change>
                    </mx:ComboBox>
                    </mx:HBox>
                    <mx:Spacer width="100%" height="2%"/>
                    <mx:Text textAlign="center" width="100%" text="Summary Information" styleName="subtitle"/>
                    <mx:DataGrid verticalScrollPolicy="off" width="100%" rowCount="4" id="dataGridSummary" showHeaders="false" styleName="schoolSummaryTable">
                    <mx:columns>
                    <mx:DataGridColumn dataField="@name"/>
                    <mx:DataGridColumn dataField="@value"/>
                    </mx:columns>
                    </mx:DataGrid>
                    <mx:Text textAlign="center" width="100%" text="Enrollment Breakdown by Ethnicity" styleName="subtitle"/>
                    <mx:DataGrid verticalScrollPolicy="off" width="100%" rowCount="7" id="dataGridEthnicity" styleName="schoolSummaryTable">
                    <mx:columns>
                    <mx:DataGridColumn minWidth="150" headerText="Ethnicity" dataField="@name"/>
                    <mx:DataGridColumn id="colEthValue" headerText="Total" dataField="@value" sortCompareFunction="sortCompareFuncValue"/>
                    <mx:DataGridColumn id="colEthPercent" headerText="Percent" dataField="@percent" sortCompareFunction="sortCompareFuncPercent"/>
                    </mx:columns>
                    </mx:DataGrid>
                    <mx:Text textAlign="center" width="100%" text="Enrollment Breakdown by Gender" styleName="subtitle"/>
                    <mx:DataGrid verticalScrollPolicy="off" width="100%" rowCount="3" id="dataGridGender" styleName="schoolSummaryTable">
                    <mx:columns>
                    <mx:DataGridColumn minWidth="150" headerText="Gender" dataField="@name"/>
                    <mx:DataGridColumn headerText="Total" dataField="@value" sortCompareFunction="sortCompareFuncValue"/>
                    <mx:DataGridColumn headerText="Percent" dataField="@percent" sortCompareFunction="sortCompareFuncPercent"/>
                    </mx:columns>
                    </mx:DataGrid>
                    </mx:VBox>

                    <mx:Spacer height="100%" width="6%"/>

                    <mx:VBox id="schoolSummaryTableBoxRight" height="100%" width="40%" horizontalAlign="left">
                    <mx:HBox width="100%" horizontalAlign="center">

                    <mx:Text text="School:" styleName="schoolSummaryDropdownText" />
                    <mx:Spacer height="100%" width="3%"/>
                    <mx:ComboBox id="cboxSchoolSummary" toolTip="Select school name" styleName="dropdown">
                    <mx:change>loadDataFromCombo(ComboBox(event.target).selectedItem)</mx:change>
                    </mx:ComboBox>
                    </mx:HBox>
                    <mx:Spacer width="100%" height="2%"/>

                    <mx:Text textAlign="center" width="100%" text="Enrollment Breakdown by Grade Level" styleName="subtitle"/>

                    <mx:DataGrid verticalScrollPolicy="off" width="100%" rowCount="10" id="dataGridGrade" styleName="schoolSummaryTable">
                    <mx:columns>
                    <mx:DataGridColumn minWidth="150" headerText="Grade Level" dataField="@name"/>
                    <mx:DataGridColumn headerText="Total" dataField="@value" sortCompareFunction="sortCompareFuncValue"/>
                    <mx:DataGridColumn headerText="Percent" dataField="@percent" sortCompareFunction="sortCompareFuncPercent"/>
                    </mx:columns>
                    </mx:DataGrid>
                    <mx:Text textAlign="center" width="100%" text="Enrollment Breakdown by Program" styleName="subtitle"/>
                    <mx:DataGrid verticalScrollPolicy="off" width="100%" rowCount="3" id="dataGridOther" styleName="schoolSummaryTable">
                    <mx:columns>
                    <mx:DataGridColumn minWidth="150" headerText="Program" dataField="@name"/>
                    <mx:DataGridColumn headerText="Total" dataField="@value" sortCompareFunction="sortCompareFuncValue"/>
                    <mx:DataGridColumn headerText="Percent" dataField="@percent" sortCompareFunction="sortCompareFuncPercent"/>
                    </mx:columns>
                    </mx:DataGrid>
                    <mx:Text textAlign="center" width="100%" text="AYP Status" styleName="subtitle"/>
                    <mx:DataGrid width="100%" rowCount="1" id="dataGridAYP" styleName="schoolSummaryTable" showHeaders="false">
                    <mx:columns>
                    <mx:DataGridColumn textAlign="center" dataField="@value"/>
                    </mx:columns>
                    </mx:DataGrid>
                    </mx:VBox>

                    <mx:Spacer height="100%" width="10%"/>
                    </mx:HBox>
                    </mx:VBox>
                    </mx:Application>
                    • 7. Re: SWFLoader Memory Issue
                      c_infanti Level 1
                      OK...so still having this issue. I took the step of reducing my 8 child swfs to just a canvas and nothing else...and still having the problem. Any ideas?
                      • 8. Re: SWFLoader Memory Issue
                        happyok
                        i find the same question