15 Replies Latest reply on Sep 14, 2011 5:25 AM by thibs73

    Flex 4.5, Modules, Memory Leaks

    thibs73 Level 1

      Hi everyone,

       

      I've been painfully dealing with memory leaks with flex modules. I've implemented Tom's solutions found here and also read tons of posts especially Alex's post on the topic found here.

       

      After implemeting Tom's solution, the profiler reported the module unloading and I was happy about it.. until I checked with windows task manager...

       

      Why is it that when I test this with the installed Air app windows task manager does not report any memory released back to the OS, but the Flash Builder Profiler does? It is a pretty large module, I should see it being released in windows task manager....

       

      Anyone know about this?

       

      Alex, if you are around, it would be great to hear from you.

       

      thank-you!

        • 1. Re: Flex 4.5, Modules, Memory Leaks
          artguate Level 2

          Just because the Flash Runtime garbage collector does collect memory, it doesn't mean that it will give it back to the OS.  The runtime assumes that there is a reasonable chance that you will need that memory again, therefore rather than giving it back to the OS, it keeps it for itself so you can use it in your app.

           

          Quite a few people have complained about this behavior.  Feel free to join in the complain.

          Or create an issue in:

          http://ideas.adobe.com/air

           

          Air and Flash will give memory back to the OS, but it makes it's decision as to when and how much.

           

          I guess part of the resoning is that usually Flex Modules are hardly ever over 2 Megs, and in a system that has 2 gigs of RAM, who cares?  Particularly if it provides better perfomance.

          • 2. Re: Flex 4.5, Modules, Memory Leaks
            thibs73 Level 1

            Thanks for the speedy reply.

             

            I do not agree with this behaviour, and will be joining the others for the vote.

             

            However, there seems to be something else going on.

             

            The module is 6MB. It is loaded/unloaded several times by the user as the normal use of the app.

             

            When I use the profiler, I see the unloaded module get garbage collected and the app remains with 1 module at all times.

             

            This is not the case with windows task manager. Everytime the module gets unloaded and a new instance created, the memory just keeps going up.

             

            If the runtime is hogging the memory for possible future use, wouldn't it re-use the memory that has just been released by the GC and not allow the app to grow to over a GB of memory?

             

            That is the behaviour I don't understand.

             

            thanks again!

            • 3. Re: Flex 4.5, Modules, Memory Leaks
              artguate Level 2

              "If the runtime is hogging the memory for possible future use, wouldn't  it re-use the memory that has just been released by the GC and not allow  the app to grow to over a GB of memory?"

               

              Yes it should.

               

              But only if the GC has made it's runs yet.  Prior to Air 3, you could sort of ask for it, but it's not "guaranteed" to collect anything.  The GC is a complicated animal.  It tags things first, then collects them.

               

              Anyways, in Air 3, you have a bit more control of when it actually does get called.

               

              Take a look at

               

              flash.system.System.gc  // Available prior to Air 3

               

              and

               

              flash.system.System.pauseForGCIfCollectionImminent()  // new in Air 3

               

              http://help.adobe.com/en_US/FlashPlatform/beta/reference/actionscript/3/flash/system/Syste m.html#pauseForGCIfCollectionImminent%28%29

              • 4. Re: Flex 4.5, Modules, Memory Leaks
                Flex harUI Adobe Employee

                When you have a module like MyModule.mxml, the profiler will show you both a

                MyModule class and a MyModule_FlexModuleFactory class.  You need to see the

                MyModule_FlexModuleFactory instance count go to zero, otherwise the SWF will

                not be released from memory.

                 

                Once you get it to zero, the next test is to run in the debugger and make

                sure the debugger shows you the in the console for the module

                SWF.  If you don't see that, the SWF is still in memory.

                 

                Only then can you start worrying about whether the AIR has released that

                memory back to the OS or not.

                • 5. Re: Flex 4.5, Modules, Memory Leaks
                  artguate Level 2

                  Thanks, that's good to know.

                  • 6. Re: Flex 4.5, Modules, Memory Leaks
                    thibs73 Level 1

                    Hi Alex,

                     

                    Thank-you for your reply and pointing this out.

                     

                    I did infact confirm that the MyModule_FlexModuleFactory is sticking around when my module class is unloaded and GC.

                    When I inspect the factory with Object Reference, I see there is a mx.styles.StyleManagerImpl there.

                     

                    1 - I assume therefore there is something with styles still hanging on to this factory? (I have no styles declared in the module, all in the shell including a -theme for MXFTEText)

                    2 - What is GC Root?

                    3- I also noticed a bunch of other generated classes that are still around. All of them ending in WatcherSetupUtil. I assume these are also responsible for pinning my module? What are they?

                     

                    Thank-you again, much appreciated.

                     

                     

                    • 7. Re: Flex 4.5, Modules, Memory Leaks
                      Flex harUI Adobe Employee

                      100% of the time, WatcherSetupUtil is not pinning the FlexModuleFactory.

                       

                      90% of the time, StyleManagerImpl is not pinning the FlexModuleFactory.

                       

                      That's because these references are cycles and will go away once some other

                      thing finally dereferences something else in the SWF.  In the 10% case for

                      StyleManagerImpl, custom styles are at fault.  See this bug:

                      https://bugs.adobe.com/jira/browse/SDK-30550.

                       

                      -Alex

                      • 8. Re: Flex 4.5, Modules, Memory Leaks
                        thibs73 Level 1

                        Thanks again Alex.

                         

                        Just making sure that I follow, a little confused. I do have custom styles in the project. Are you saying this bug is affecting my FlexModuleFactory or that I should be tracking down my custom styles?

                        • 9. Re: Flex 4.5, Modules, Memory Leaks
                          Flex harUI Adobe Employee

                          It is possible that custom styles are affecting the unloading.  You can

                          implement the workarounds or monkey-patch in the bug and see if it helps.

                          There's still a good chance it is something else.

                          • 10. Re: Flex 4.5, Modules, Memory Leaks
                            thibs73 Level 1

                            Hi Alex,

                            Thanks again for your reply; I’ve created a simple project demonstrating my issue.

                             

                            As you can see in the code below, I am using all the recommendations for properly unloading a module. I can never get the Factory instance count to 0 when using embedded fonts in my stylesheet. If I remove the fonts from my stylesheet the count goes to 0 (After invoking GC). In fact there are 2 different Factories: styles_mx_core_FlexModuleFactory and _test_mx_core_FlexModuleFactory.

                             

                            When I look at the object references I notice there is a dictionary with some references that I am unsure of.  Take a look at the screen cap below.

                             

                            The concerning issue and question is: How can adding fonts in my css pin the module in memory?

                             

                             

                            Shell application:

                             

                            <?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"

                            >

                            <fx:Script>

                            <![CDATA[

                                 protected function loadModule(event:MouseEvent):void {

                                      var childDomain:ApplicationDomain = new ApplicationDomain(ApplicationDomain.currentDomain);

                                      styleManager.loadStyleDeclarations("styles.swf", true, false, childDomain);

                                      moduleLoader.applicationDomain = childDomain;

                                      moduleLoader.loadModule("test.swf");

                                 }

                                 protected function unloadModule(event:MouseEvent):void {

                                      textInput.setFocus();

                                      styleManager.unloadStyleDeclarations("styles.swf");

                                      moduleLoader.applicationDomain = null;

                                      moduleLoader.unloadModule();

                                 }

                            ]]>

                            </fx:Script>

                            <s:ModuleLoader

                                      id="moduleLoader"

                                      width="100%"

                                      height="100%"

                                      />

                            <s:HGroup

                                 bottom="0"

                                 >

                                 <s:Button

                                      label="load"

                                      click="loadModule(event)"

                                 />

                                 <s:Button

                                      label="unload"

                                      click="unloadModule(event)"

                                      />

                                 <mx:TextInput

                                      id="textInput"

                                      />

                            </s:HGroup>

                            </s:WindowedApplication>

                             

                            Module:

                             

                            <?xml version="1.0" encoding="utf-8"?>

                            <s:Module

                            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:Label

                                      text="This is my module"

                                      />

                            </s:Module>

                             

                             

                            Stylesheet:
                            @namespace s "library://ns.adobe.com/flex/spark";
                            @namespace mx "library://ns.adobe.com/flex/mx";
                            @font-face {
                            src:url("assets/font/DroidSansMono.ttf");
                            font-family: "DroidSansMono";
                            }

                             

                            instance.PNG

                             

                            • 11. Re: Flex 4.5, Modules, Memory Leaks
                              thibs73 Level 1

                              Hi again,

                               

                              Just wanted to note that I am noticing similar experiences with air windows. Every time the application opens a window, it is not garbage collected and the instance count goes up.

                               

                              Are there known memory issues with windows also?

                               

                              Are they related to the same issue I noticed with the font described in my reply above?

                               

                              Thank-you again

                              • 12. Re: Flex 4.5, Modules, Memory Leaks
                                Flex harUI Adobe Employee

                                Style modules with embedded fonts register their fonts and they will leak.

                                It is possible that that can somehow cause the other module to leak.  I

                                generally recommend that folks load embedded fonts via regular modules

                                instead of CSS modules.

                                 

                                I would put together a simple case for AIR windows and see if it is also

                                font related or not.

                                 

                                Dictionary keys are usually weak and do not cause leaks.  Dictionary values

                                are never weak and will cause leaks.

                                • 13. Re: Flex 4.5, Modules, Memory Leaks
                                  thibs73 Level 1

                                  Hi Alex,

                                   

                                  Thanks again. I am not sure what you mean by: "embedded fonts via regular modules instead of CSS modules". I am currently declaring the styles in the shell with a stylesheet...

                                   

                                   

                                  thank-you

                                  • 14. Re: Flex 4.5, Modules, Memory Leaks
                                    Flex harUI Adobe Employee

                                    In a module mxml file, use an fx:Style block to define the font and use

                                    ModuleManager to load the SWF instead of StyleManager.

                                    • 15. Re: Flex 4.5, Modules, Memory Leaks
                                      thibs73 Level 1

                                      Hi Alex,

                                       

                                      Thank-you for all your help on this one.

                                       

                                      Just a final note for people having the same issue. What we ended up doing is getting rid of the module, 90% of our memory leaks are gone. Staying away from modules from now on...