11 Replies Latest reply on Oct 12, 2011 12:06 AM by balint.magyar

    Flex 4.5 - s:Window - s:DataGrid - Memory Leak?

    thibs73 Level 1

      Hi All,

       

      We're trying to clean up Air windows and noticing some odd behaviour.

       

      Here is the case:

      We have a spark window that contains a spark datagrid, this datagrid is bound to a dataprovider which is a model in the main application.

      When we close the window, there is ALWAYS one instance of MyWindow that remains in memory.

      profiler.PNG

       

      And looking at the trace of the window does not give much information...

       

      Has anyone seen this before? Alex, you've helped us a lot before with memory issues, can you shed some light please?

       

      Thank-you!

       

       

       

      We have a simple test case: ( Didn't wire the close handler, so make sure to use the close button.)

       

      Main.mxml

       

      <?xml version="1.0" encoding="utf-8"?>
      <s:WindowedApplication 
                xmlns:mx="library://ns.adobe.com/flex/mx"
                xmlns:fx="http://ns.adobe.com/mxml/2009"
                xmlns:s="library://ns.adobe.com/flex/spark"
                xmlns:domain="domain.*"
                >
                  <fx:Script>
                          <![CDATA[
                                    import mx.core.Window;
                                    public var window:MyWindow;
        
                                    protected function openWindow(event:MouseEvent):void {
                                              window = new MyWindow();
                                              window.model = this.model;
                                              window.open();
                                    }
      
                                    protected function closeWindow(event:MouseEvent):void {
                                              window.close();
                                              window = null;
                                    }
                          ]]>
                </fx:Script>
        
                <fx:Declarations>
      
                           <domain:Model 
                                    id="model"
                                    />
        
                </fx:Declarations>
        
                <s:HGroup>
                          <s:Button 
                                    label="Open"
                                    click="openWindow(event)"
                                    />
                          <s:Button 
                                    label="Close"
                                    click="closeWindow(event)"
                                    />
                </s:HGroup>
        
      </s:WindowedApplication>
      
      

       

      Model.as

       

      package domain {
      
                 import mx.collections.ArrayCollection;
        
                [Bindable]
                public class Model {
                            public var collection:ArrayCollection = new ArrayCollection([ { name:'Adobe' }, { name:'Apple' } ]);
                }
      }
      
      

       

      MyWindow.mxml

       

      <?xml version="1.0" encoding="utf-8"?>
      <s:Window 
                xmlns:mx="library://ns.adobe.com/flex/mx"
                xmlns:fx="http://ns.adobe.com/mxml/2009"
                xmlns:s="library://ns.adobe.com/flex/spark"
                width="400"
                height="300"
                >
      
      
                <fx:Script>
                          <![CDATA[
                                    import domain.Model;
      
      
                                    [Bindable]
                                    public var model:Model;
                          ]]>
                </fx:Script>
      
      
                <s:DataGrid 
                          dataProvider="{ model.collection }"
                          />
        
      </s:Window>
      
      
        • 1. Re: Flex 4.5 - s:Window - s:DataGrid - Memory Leak?
          Flex harUI Adobe Employee

          I tried the test case, but everything seems to be working fine for me.  What version of Flex are you using?

          • 2. Re: Flex 4.5 - s:Window - s:DataGrid - Memory Leak?
            thibs73 Level 1

            Thanks for looking at this Alex.

             

            We are using 4.5.1 with Air 2.6 in Flash Builder 4.5.1.313231

             

            Is there a place where I can send an fxp or video?

             

            thanks

            • 3. Re: Flex 4.5 - s:Window - s:DataGrid - Memory Leak?
              Flex harUI Adobe Employee

              You should be able to attach an fxp by renaming it to zip and attaching to a post.  You may have to use the browser to do it.

               

              Try stripping down.  Does an empty window get collected?  A window with one button?

              • 4. Re: Flex 4.5 - s:Window - s:DataGrid - Memory Leak?
                Chris Velevitch Level 1

                Try setting window.model = null before closing it.

                • 5. Re: Flex 4.5 - s:Window - s:DataGrid - Memory Leak?
                  thibs73 Level 1

                  Hi Alex,

                   

                  We've tested with this and discovered that any datagrid in a spark window assigned a dataprovider results in always one instance of the window staying in memory.

                   

                  Here is what we tested:

                  Window with nothing in it = gets collected.

                  Window with datagrid and no dataprovider = gets collected.

                  Window with datagrid and no dataprovider but "model" is passed from main = gets collected.

                   

                  window with datagrid and local dataprovider (inline) = get pinned.

                  window with datagrid and passed "model" as dataprovider = get pinned.

                   

                  Tested this on two computers with FB 4.5.1

                   

                  Thanks again!

                   

                   

                   

                  I cannot see where I can post fxp sorry. So I am posting code adjustments here showing the window with just inline dp.

                   

                   

                  Modified MyWindow.mxml:

                   

                  <?xml version="1.0" encoding="utf-8"?>
                  <s:Window 
                            xmlns:mx="library://ns.adobe.com/flex/mx"
                            xmlns:fx="http://ns.adobe.com/mxml/2009"
                            xmlns:s="library://ns.adobe.com/flex/spark"
                            width="400"
                            height="300"
                            >
                              <s:DataGrid id="dataGrid">
                                      <s:ArrayCollection>
                                                <s:DataItem key="1000" name="Abrasive" price="100.11" call="false"/>
                                                <s:DataItem key="1001" name="Brush" price="110.01" call="true"/>
                                                <s:DataItem key="1002" name="Clamp" price="120.02" call="false"/>
                                                <s:DataItem key="1003" name="Drill" price="130.03" call="true"/>
                                                <s:DataItem key="1004" name="Epoxy" price="140.04" call="false"/>
                                                <s:DataItem key="1005" name="File" price="150.05" call="true"/>
                                                <s:DataItem key="1006" name="Gouge" price="160.06" call="false"/>
                                                <s:DataItem key="1007" name="Hook" price="170.07" call="true"/>
                                                <s:DataItem key="1008" name="Ink" price="180.08" call="false"/>
                                                <s:DataItem key="1009" name="Jack" price="190.09" call="true"/>             
                                      </s:ArrayCollection>
                            </s:DataGrid>
                    </s:Window>
                  

                   

                   

                  Modified main.mxml

                   

                   

                  <?xml version="1.0" encoding="utf-8"?>
                  <s:WindowedApplication
                            xmlns:mx="library://ns.adobe.com/flex/mx"
                            xmlns:fx="http://ns.adobe.com/mxml/2009"
                            xmlns:s="library://ns.adobe.com/flex/spark"
                            xmlns:domain="domain.*"
                            >
                              <fx:Script>
                                      <![CDATA[
                                                import mx.core.Window;
                                                public var window:MyWindow;
                                                protected function openWindow(event:MouseEvent):void {
                                                          window = new MyWindow();
                                                          window.open();
                                                }
                                                protected function closeWindow(event:MouseEvent):void {
                                                          window.close();
                                                          window = null;
                                                }
                                      ]]>
                            </fx:Script>
                              <fx:Declarations>
                                        <domain:Model
                                                id="model"
                                                />
                              </fx:Declarations>
                              <s:HGroup>
                                      <s:Button
                                                label="Open"
                                                click="openWindow(event)"
                                                />
                                      <s:Button
                                                label="Close"
                                                click="closeWindow(event)"
                                                />
                            </s:HGroup>
                    </s:WindowedApplication>
                  
                  
                  • 6. Re: Flex 4.5 - s:Window - s:DataGrid - Memory Leak?
                    Flex harUI Adobe Employee

                    I am unable to reproduce the issue.  Which version of AIR are you running?  Hitting the Garbage Collection button doesn’t have any effect?  What are the backreferences to MyWindow?

                    • 7. Re: Flex 4.5 - s:Window - s:DataGrid - Memory Leak?
                      balint.magyar

                      hello thibs, hello alex!

                       

                      I was facing exactly the same problem as thibs, and searching the issue on the web led me here. My application architecture is almost the same as thibs' except that i am not using s:Window as a container for the DataGrid, just a simple custom component based on s:Group just adding it directly to the main application. I can confirm the same behaviour, if the DataGrid has no dataProvider it gets collected in all other cases (local dataProvider in the custom component or dataProvider from the model) its not getting garbage collected. Because of not using the Window component, it seems to be sure, that it has nothing to do with Window itself, but with the DataProvider. In Profiler i see mx.binding:Binding (a Function below) items which keep the DataGrid and therefore the whole component in memory.

                      The interesting thing is, that if i remove my component containing the DataGrid with removeElement AND add a new instance of my component to the main application (with addElement), the previously pinned instance of the DataGrid/component gets collected, which is fine, but not really the expected result, as it would be that the DataGrid got collected after removed from the application.

                       

                      My environment:

                      OS: Windows 7 Home Premium (x64)

                      Flex SDK: Version 4.5.1 build 21328

                      Flash Builder: Flash Builder 4.5 Premium (build: 313231)

                      AIR: 3.0.0.4080 (debug: 2.6.0.19120)

                       

                       

                      bálint

                      • 8. Re: Flex 4.5 - s:Window - s:DataGrid - Memory Leak?
                        Flex harUI Adobe Employee

                        If the DataGrid gets collected when you create a second instance, then it isn’t the mx.binding:Binding that is keeping it in memory, unless you did some other work to somehow remove that reference.  Sometimes, things like that show up because they contain weak references to the object but something else is still holding on.

                         

                        Did you interact with the DataGrid?  Maybe the player’s focus mechanism is still referencing components inside the DataGrid.

                        • 9. Re: Flex 4.5 - s:Window - s:DataGrid - Memory Leak?
                          balint.magyar Level 1

                          hello alex,

                           

                          thank you for the quick reply! here is the source code of my components. the model only holds an ArrayCollection object named "dataProvider", where for the garbage collection it does not matter, if the ArrayCollection has any elements or not.

                           

                          The model is just an extended ArrayList - without any modifications - to be able to use it in MXML with "children" as the model's elements.

                           

                           

                          [Bindable]
                                    public dynamic class ModelBase extends ArrayList
                          

                           

                          The model is defined in the main application under the Declarations tag as:

                           

                          <models:ModelBase id="model">
                               <models:dataProvider>
                                    <s:ArrayCollection/>
                               </models:dataProvider>
                          </models:ModelBase>
                          

                           

                          And the code for the component.

                           

                          <?xml version="1.0" encoding="utf-8"?>
                          <s:Group xmlns:fx="http://ns.adobe.com/mxml/2009" 
                                               xmlns:s="library://ns.adobe.com/flex/spark" 
                                               xmlns:mx="library://ns.adobe.com/flex/mx" width="400" height="300" xmlns:models="dummy.models.*" >
                                    <s:layout>
                                              <s:VerticalLayout paddingBottom="10" paddingLeft="10" paddingRight="10" paddingTop="10"/>
                                    </s:layout>
                                    <fx:Declarations>
                                              <models:ModelBase id="model"/>
                                    </fx:Declarations>
                                    <s:List x="10" y="40" width="100%" height="100%" dataProvider="{this.model.dataProvider}" labelField="products_model"></s:List>
                                    <s:DataGrid dataProvider="{this.model.dataProvider}">
                                              <s:columns>
                                                        <s:ArrayList>
                                                                  <s:GridColumn dataField="products_model" headerText="Column 1"></s:GridColumn> 
                                                        </s:ArrayList>
                                              </s:columns> 
                                    </s:DataGrid>
                          </s:Group>
                          

                           

                          I just tested the cases where I once interacted with the DataGrid and then not. Also tried to set the dataProvider of the DataGrid to null before removing it, but all result in the same: the datagrid and my component are still in memory, until as mentioned if I create a new instance of the component and add it to the main application.

                          • 10. Re: Flex 4.5 - s:Window - s:DataGrid - Memory Leak?
                            Flex harUI Adobe Employee

                            Hard to tell from snippets.  I would recommend you make the simplest test case possible.  Use ArrayList instead of a subclass.  Get it down as close to 20-lines as you can and post it if it still shows leaks.

                            • 11. Re: Flex 4.5 - s:Window - s:DataGrid - Memory Leak?
                              balint.magyar Level 1

                              hello alex,

                               

                              as it is impossible to master this in 20 lines of code, i filed a bug with a test case to reproduce it

                               

                              http://bugs.adobe.com/jira/browse/SDK-31833

                               

                              bálint