4 Replies Latest reply on Sep 20, 2012 1:11 AM by avbenten

    Button memory leak

    jozefchutka Level 1

      Hello, I am unable to GC spark Button (or any other component). Simpliest example looks something like this:


      <?xml version="1.0" encoding="utf-8"?>
      <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
                     minWidth="955" minHeight="600">
                  private function remove():void
                      button = null;
          <s:Button id="button" label="remove me" click="remove()"/>


      is there any way that Button get GCed after it is removed from stage (click on a buttom)?

      flex 4.6.0

        • 1. Re: Button memory leak
          Flex harUI Adobe Employee

          That might be expected.  Elements declared in MXML can have other generated code referencing those items.  The general rule is if you want destroy it dynamically, create it dynamically.

          • 2. Re: Button memory leak

            Memory leak? Yes ...

            Even if your application adds and removes UIComponents (such as Panels, Forms etc.) dynamically the used TotalMemory increases permanently. The explicit use of System.gc() does not changes anything in memory-usage.

            Use the following small example to understand the behaviour of the problem.


            <?xml version="1.0"?>

            <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"





                    import spark.components.Form;

                    import spark.components.FormItem;

                    import spark.components.HGroup;

                    import spark.components.Panel;

                    import spark.components.TextInput;

                    import spark.components.VGroup;


                    private var panel:Panel;


                    private function addRemovePanel():void {

                        if (myGroup.numElements == 0) {

                            panel = createPanel();


                        } else {


                            panel = null;




                    private function createPanel():Panel {

                        const panel:Panel = new Panel();

                        panel.title = "Panel"

                        const content:VGroup = new VGroup();

                        content.paddingBottom = 10;

                        content.paddingLeft = 10;

                        content.paddingRight = 10

                        content.paddingTop = 10;

                        const form:Form = new Form();

                        for (var i:int = 0; i < 3; i++) {

                            var formItem:FormItem = new FormItem();

                            formItem.label = "Label_" + i;

                            var textInput:TextInput = new TextInput();

                            textInput.text = "Text_" + i;





                        const group:HGroup = new HGroup();

                        for (var i:int = 0; i < 3; i++) {

                            var button:Button = new Button();

                            button.label = "Button_" + i;





                        return panel;






                    <s:VerticalLayout paddingBottom="10" paddingLeft="10" paddingRight="10" paddingTop="10"

                                      verticalAlign="middle" horizontalAlign="center"/>



                <s:HGroup verticalAlign="middle" horizontalAlign="center" width="100%" height="25">

                    <s:Label id="totalMemory"

                             text="{'Total memory: ' + (System.totalMemory / 1024) + ' KB'}"

                             render="totalMemory.text = 'Total memory: ' + (System.totalMemory / 1024) + ' KB'"/>

                    <s:Button id="gcButton"

                              label="force System.gc()"

                              click="System.gc ()"/>



                <s:VGroup id="myGroup" horizontalAlign="center" verticalAlign="middle" width="100%" height="100%"/>


                <s:Button label="Add/Remove" click="addRemovePanel();"/>


            • 3. Re: Button memory leak
              Flex harUI Adobe Employee

              What does the profiler show is holding on to the buttons?

              • 4. Re: Button memory leak
                avbenten Level 1

                hey harUI,


                if i understand your question in that way, that you want to know what the profiling-tool (i use the intellij-profiler) is showing if i push the add-remove-button continuously, than the answer is that i see even one more Panel etc. in memory and even if i forces the garbagecollection.


                For you information: i have opened a bug for that problem.