0

I build a simple Flex view state based video application on Flash Builder 4.5. There are 4 states. Everytime I go from State1->State2->State3 or State1->State2->State4, the memory in internet explorer increases, but is not released. What am I not deallocating?

Do i have to manually deallocate every single element and its event listeners for each state?

I have version Flash Player 10,2,153,1 installed.

Source Code download is here if you want to build the app and run.

here's the core code:

<?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"
               width.State1="1024" height.State1="768"
               width.State2="1024" height.State2="768"
               width.State3="1024" height.State3="768"
               width.State4="1024" height.State4="768">

    <fx:Script>
        <![CDATA[
            protected function button1_clickHandler(event:MouseEvent):void
            {
                // TODO Auto-generated method stub
                currentState='State2';
            }

            protected function button2_clickHandler(event:MouseEvent):void
            {
                // TODO Auto-generated method stub
                currentState='State3';
            }

            protected function button3_clickHandler(event:MouseEvent):void
            {
                // TODO Auto-generated method stub
                currentState='State1';
            }

            protected function button4_clickHandler(event:MouseEvent):void
            {
                // TODO Auto-generated method stub
                currentState='State2';
            }

            protected function button5_clickHandler(event:MouseEvent):void
            {
                // TODO Auto-generated method stub
                currentState='State4';
            }

        ]]>
    </fx:Script>

    <s:states>
        <s:State name="State1" />
        <s:State name="State2"/>
        <s:State name="State3"/>
        <s:State name="State4"/>
    </s:states>
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
    <s:Button includeIn="State1" x="405" y="175" width="227" height="73" label="Music List"
              click="button1_clickHandler(event)" fontSize="30" itemDestructionPolicy="auto" />
    <s:Label includeIn="State1" x="440" y="61" fontSize="30" text="Main State" itemDestructionPolicy="auto"/>
    <s:Label includeIn="State2" x="409" y="34" fontSize="30" text="Classical Video" itemDestructionPolicy="auto"/>
    <s:Button includeIn="State2" x="328" y="151" width="369" height="91"
              label="Beethoven - &quot;Für Elise&quot;" click="button2_clickHandler(event)"
              fontSize="30"
              itemDestructionPolicy="auto"
              />
    <s:VideoPlayer includeIn="State3,State4" x="0" y="67" width="1024" height="530" autoPlay="false"
                   loop="true" scaleMode="stretch" source="assets/Beethoven__Für_Elise.mp4"
                   source.State4="assets/FOUR_SEASONS_VIVALDI_LANDSCAPES_AND_MUSIC.mp4"
                   itemDestructionPolicy="auto"
                   />
    <s:Button includeIn="State3,State4" x="2" y="14" width="240" height="45" label="Return to Main"
              click="button3_clickHandler(event)" fontSize="30"
              itemDestructionPolicy="auto"
              />
    <s:Button includeIn="State3,State4" x="338" y="629" width="348" height="63" label="Return to classical"
              click="button4_clickHandler(event)" fontSize="30"
              itemDestructionPolicy="auto"
              />
    <s:Button includeIn="State2" x="326" y="287" width="371" height="81"
              label="Vivaldi – “Four Seasons”" click="button5_clickHandler(event)" fontSize="30"
              itemDestructionPolicy="auto"
              />
</s:Application>

View from the detecting loitering objects(http://help.adobe.com/en_US/flashbuilder/using/WSe4e4b720da9dedb5510654d812e4d126514-8000.html#WS6f97d7caa66ef6eb1e63e3d11b6c4d0d21-7ee8) is here loitering objects view

View from the object references(http://help.adobe.com/en_US/flashbuilder/using/WSe4e4b720da9dedb5510654d812e4d126514-8000.html#WS6f97d7caa66ef6eb1e63e3d11b6c4d0d21-7eef) from the topmost Object in the 'loitering objects view' is here Object references view

I checked the documentation for "Controlling caching of objects created in a view state" and set the itemDestructionPolicy="auto". It still is caching the objects and memory is not being released as seen in the Task Manager.

Wladimir Palant
  • 56,865
  • 12
  • 98
  • 126
iceman
  • 4,211
  • 13
  • 65
  • 92
  • Is it constantly increasing? Like if you are changing states often does it never get collected or is it just through those states. GC will not run unless it needs to run, and what forces it to run is when more objects need allocated it should clean up the memory. – Mike Sep 30 '11 at 16:37
  • its not constantly increasing, thats a serious memory leak. Its only when you change states very frequently through an interaction. When does the GC run-at what limit? – iceman Oct 02 '11 at 10:42
  • http://livedocs.adobe.com/flex/3/html/help.html?content=profiler_6.html Here is a link to the GC for flex. Basically it will run when it needs to, I have found sometimes that means as it is near the max memory allowed for the program then it will perform large garbage collection as you allocate new objects. – Mike Oct 03 '11 at 12:58
  • thanks Mike; the new link is http://help.adobe.com/en_US/flashbuilder/using/WS6f97d7caa66ef6eb1e63e3d11b6c4d0d21-7ee7.html . what strikes is most is "So, when you remove all references to an object, the garbage collector does not necessarily deallocate the memory for that object. That object becomes a candidate for garbage collection." on a 1 GB machine for example, when would the GC kick in? – iceman Oct 04 '11 at 12:42
  • If you watch your profiler as you use the application you should actually be able to see it happen. I personally don't know a lot about the internals of it, but from what I have gathered from my own research it will use as much memory as it can basically before it will actually perform the collection. And as you pointed out just removing all references to the object makes it a candidate for GC, since the collector only runs when new objects need memory the GC will clean up enough candidates to create the space needed. Also something to note: you can not programmatically force GC in your code. – Mike Oct 04 '11 at 13:09
  • @mike:Thanks for your suggestions. a blog(http://jvalentino.blogspot.com/2009/05/flex-memory-issue-3-garbage-collection.html) says:"Forcing garbage collection is not the silver bullet to all of your memory problems." The code I've uploaded is pretty basic view state code. Have I missed something totally in the code? – iceman Oct 04 '11 at 13:57
  • @Mike; I've added the code. Do you see something terribly missing? – iceman Oct 05 '11 at 06:04
  • I don't see anything wrong with this, there isn't a whole lot that should be taking up your memory. Are you having issues with it slowing down significantly after long usage? Have you profiled it for an extended period of time and watched the memory? Is it not freeing memory? – Mike Oct 05 '11 at 13:31
  • @Mike: I have watched the memory grow to 300 mb after 15 to 20 mins of usage, but its not like a continous leak. Only happens on interaction. I haven't seen it decrease. Till 300mb, the system runs fine as I have 2gb of RAM. – iceman Oct 06 '11 at 11:19

0 Answers0