4 Replies Latest reply on Jun 29, 2011 2:00 AM by lizsterine

    Spark DataGrid remaining in memory

    lizsterine

      I've found that the Spark DataGrid hangs around in memory once it renders items.  If the DataGrid is added to the stage but never is given a dataProvider, therefore not rendering items, the DataGrid will be garbage collected if removed from the stage and references deleted.

       

      BUT, if a dataProvider is set and the DataGrid renders items, removing it from the stage and nullifying references won't allow it to be garbage collected.  I even tried removing all items from the dataprovider and nullifying the dataProvider as well, the DataGrid still hangs around.

       

      Has anyone else experienced this? 

       

      Tests conducted using Flash Builder Profiler in version 4.5 and 4.5.1, both with same results.

        • 1. Re: Spark DataGrid remaining in memory
          Lee Burrows Level 4

          ive had no problems - sure you havent forgotten to remove a listener?

          • 2. Re: Spark DataGrid remaining in memory
            lizsterine Level 1

            Nope, no listeners hanging on.  Here's my simple test app:

             

            <?xml version="1.0" encoding="utf-8"?>
            <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
                              xmlns:s="library://ns.adobe.com/flex/spark" 
                              xmlns:mx="library://ns.adobe.com/flex/mx" 
                              minWidth="955" minHeight="600">
                 <fx:Declarations>
                      <s:ArrayCollection id="dp">
                           <fx:Object foo="bar" bah="schmo" />
                      </s:ArrayCollection>
                 </fx:Declarations>
                 <fx:Script>
                      <![CDATA[
                           private function btn_onClick(event:MouseEvent):void {
                                dg.dataProvider = null;
                                dg.validateNow();
                                vg.removeElement(dg);
                                dg = null;
                           }
                      ]]>
                 </fx:Script>
                 <s:VGroup id="vg" horizontalCenter="0" verticalCenter="0">
                      <s:DataGrid id="dg" dataProvider="{dp}" />
                      <s:Button label="remove grid" click="btn_onClick(event)" />
                 </s:VGroup>
            </s:Application>
            

             

            That DataGrid will garbage collect if I don't set the dataProvider.  If I do (as above), then the DataGrid will not be collected. (which I'm testing in the Profiler by clicking the "Run Garbage Collector" button)

             

            I'm not overlooking somthing here, am I?

            • 3. Re: Spark DataGrid remaining in memory
              Flex harUI Adobe Employee

              I think if you are using binding to assign the dataProvider, there is a

              bunch of binding code that doesn't let the DG go away.  MXML declarations

              within a single MXML file are not compiled to allow individual declarations

              to be cleaned up.  We expect the entire thing to be an encapsulation.

               

              If you created the DG and call vg.addElement, and assign the dataProvider

              then, I would expect it goes away when you remove it.

              1 person found this helpful
              • 4. Re: Spark DataGrid remaining in memory
                lizsterine Level 1

                Hmmm....I starting to think it might just be the way the new DataGrid works.  Though I don't believe the binding was the culprit, your suggestion lead me to a new discovery.  First, this is my test app:

                 

                <?xml version="1.0" encoding="utf-8"?>
                <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
                                  xmlns:s="library://ns.adobe.com/flex/spark" 
                                  xmlns:mx="library://ns.adobe.com/flex/mx" 
                                  minWidth="955" minHeight="600">
                     <fx:Declarations>
                          <s:ArrayCollection id="dp">
                               <fx:Object foo="bar" bah="schmo" />
                          </s:ArrayCollection>
                     </fx:Declarations>
                     <fx:Script>
                          <![CDATA[
                               import mx.events.FlexEvent;
                               
                               import spark.components.DataGrid;
                               
                               private var dg:DataGrid;
                               
                               private function btn_onClick(event:MouseEvent):void {
                                    if(!dg){     
                                         btn_remove.label = "Remove grid";
                                         dg = new DataGrid();
                                         vg.addElement(dg);
                                         dg.dataProvider = dp; 
                                    }else{
                                         btn_remove.label = "Add grid";
                                         vg.removeElement(dg);
                                         dg = null;
                                    }
                               }
                          ]]>
                     </fx:Script>
                     <s:VGroup id="vg" horizontalCenter="0" verticalCenter="0">
                          <s:Button id="btn_remove" label="Add grid" click="btn_onClick(event)" />
                     </s:VGroup>
                </s:Application>
                
                

                 

                When Profiling this app, I see the DataGrid sticking around in the profiler after removing it from the stage and clicking the "Run garbage collector" button.  I then push my button again to create another DataGrid.  I see 2 instances of a DataGrid in my profiler, but after running garbage collect it goes back down to one (and all suporting classes, such as itemRenderers and GridColumns etc go back down to the original instance numbers).

                 

                My conclusion is that once a DataGrid is created it shall forever remain in memory to be recycled at a later time, only if items for it have been created.  If anyone can confirm this theory, that would be nice.

                 

                Until then at least I have the knowledge that repeated instances of the DataGrid won't keep pushing up memory if more are created at later.