18 Replies Latest reply on Feb 12, 2010 12:07 PM by Craig Swank

    Memory Leak Solved, then Unsolved

    Craig Swank

      Hello,

      I was working on a flex/air app at work, and discovered it had a memory leak:

       

      Picture 11.png

       

      I worked on fixing it, mostly on my home computer (i5 imac).  I read about weak references, learned a bunch about the garbage collection:

       

      Screen shot 2010-01-04 at 4.31.53 PM.png

       

      I was happy, because that was a lot of learning and it seemed to pay off.  Then I went back to my work computer (core 2 duo macbook pro), did an svn update on my flex/air app, and:

       

      Picture 12.png

       

      I went from happy to sad.  I thought it must be my adobe air version, so I updated both computers to the same air version (1.5.3.9120), but still have the same leak at work, and no leak at home.  I don't expect anyone to be able to suggest a fix for this, but I am wondering if anyone else has seen anything like this.

       

      I've only been using flex for about 5 weeks, and still feel I have a lot to learn, so maybe I'm missing something obvious that could be causing this problem.  Any suggestions, anyone?

       

      Thanks,

       

      Craig

        • 1. Re: Memory Leak Solved, then Unsolved
          Matt Le Fevre Level 4

          An obvious one i missed a while back was when one of my apps was trying to continously load images that were not at the location stated. I hadn't put in an expression to tell it what to do if one didn't exist, so it just kept firing off the requests and gradually reduced the browser speed to a snails pace before crashing.

           

          won't be doing that again.

          • 2. Re: Memory Leak Solved, then Unsolved
            Gaurav J Adobe Employee

            You should try to compare memory snapshots (via loitering objects) and see what objects are leaking..

             

            Also take a look at the profiler documentation: http://livedocs.adobe.com/flex/3/html/help.html?content=profiler_1.html

             

            Thanks,

            Gaurav Jain

            Flex SDK Team

            http://www.gauravj.com/blog

            • 3. Re: Memory Leak Solved, then Unsolved
              Craig Swank Level 1

              Thanks,

              I have read that doc and looked at the loitering objects, as you have suggested.  That is part of how I solved the problem in the first place (on my home computer, once again).  Looking again at loitering objects, on the work computer, it seems that the largest amount of memory is devoted to objects of type 'Class, ' which is taking up 47% of all memory in the app. I'm not sure what this means.  Are these held onto from making calls to static methods and variables?  I don't know.  I'll have to look at a memory snapshot on my home computer and figure out what the difference is.  Once again, though, I'm perplexed about why this is happening with the same code, and the same air environment.

               

              Craig

              • 4. Re: Memory Leak Solved, then Unsolved
                RWinscot Level 1

                Is there a possibility that your home / work environments are not on the same AIR / Flex Framework version? Also... some code at this point would be favorable and would best help us help you figure out what's eating up your resources.

                 

                Cheers,

                 

                Rick Winscot

                www.quilix.com

                • 5. Re: Memory Leak Solved, then Unsolved
                  msakrejda Level 4

                  Embedded (and non-embedded?) images are effectively compiled into custom classes, so it seems you may have stray image references somewhere. Also, could it be that you have a dirty svn working copy at either home or work that's causing the disparity?

                  • 6. Re: Memory Leak Solved, then Unsolved
                    Craig Swank Level 1

                    Thanks for the suggestions, Rick and msakrejda

                     

                    I've double checked the Air/Flex environments, and they are definitely the same.  I downloaded flashbuilder 4 beta two on both machines, and both air runtimes are 1.5.3.  I'm going to look at this very closely, though, as I'm running out of ideas.

                     

                    Unfortunately, I don't think posting code would be helpful.  There is a lot of it.  This is an app for people in the field in a certain industry to collect data about machinery that is being rebuilt.  The app generates a large number of forms based on xml that it retrieves from a server.  Each time the user navigates to a new form, a lot of stuff gets initialized and the memory jumps up a bit.  The amount depends on if the form contains images.  Before one of these forms is generated I make a removeAllChildren() call on the parent of the form.  I then call System.gc() twice.  This is what did the trick for me on the home computer (along with using weak references on addEventListener calls).  As soon as I learned about that gc call, the garbage collection started working.

                     

                    I don't know.  I know I am running air 1.5.3 on both machines, but I'm not completely sure they are 1.5.3.9120.  I guess I'l check that more carefully.

                     

                    I'm very sure that the svn is not corrupt, but when I'm at work again, I'm going to tar up the project and scp it home so I can do a diff and see if anything pops up.  So thanks for that suggestion, msakrejda

                     

                    I do have images in this app, but none of them are embedded.  Those also come from the server.  Every single place that one of my images that gets loaded from the server is contained in the form, or one of its children.  When I call removeAllChildren on the form, they should get removed when the garbage collector does a sweep.

                     

                    I'll be doing battle with this for as long as it takes, because this app is pretty important to our project.  I'll be sure to post when I figure it out.  I'm following all suggestions, so thanks for those.

                     

                    Craig

                    • 7. Re: Memory Leak Solved, then Unsolved
                      msakrejda Level 4

                      I don't suppose you have a non-debug Flash Player version at work? I

                      don't think you can force gc in a non-debug Player. You shouldn't need

                      to force GC anyway (it should happen at its own pace), but if your app

                      is ignoring a forced GC, that could be a reason for the disparity. In

                      the (second) leak screenshot you posted, memory usage only goes up to

                      ~30 MB, which is significant, but far from outrageous. If you keep

                      loading and unloading components, does this eventually crash due to

                      memory shortage?

                      1 person found this helpful
                      • 8. Re: Memory Leak Solved, then Unsolved
                        Craig Swank Level 1

                        Hmm, I didn't know about not being able to call System.gc() outside of a debug environment.  When I make a release build of my app, and watch it's memory usage with mac's Activity Monitor, the memory behavior is certainly different.  I'll have to look at it more closely to see what happens after a long period of use. 

                         

                        You are right about the total memory usage not being terribly large in the last profiler screenshot.  That is because I made some changes in the way the server handles uploaded images and gives them back to the app.

                         

                        I will now be reading about the System.gc() more carefully. Thanks msakrejda.

                        • 9. Re: Memory Leak Solved, then Unsolved
                          Craig Swank Level 1

                          According to http://livedocs.adobe.com/flex/3/langref/flash/system/System.html#gc()

                           

                          gc()method
                          public static function gc():void

                           

                          Forces the garbage collection process.

                          For the Flash Player debugger version and AIR applications only. In an AIR application, the System.gc() method is only enabled in content running in the AIR Debug Launcher (ADL) or, in an installed applcation, in content in the application security sandbox.

                           

                          So, all of the files that my form generator utilizes, qualify as 'content in the application security sandbox,' I believe.  That means that forced garbage collection calls should work on them, I think.

                           

                           

                          • 10. Re: Memory Leak Solved, then Unsolved
                            Flex harUI Adobe Employee

                            So, what does the profiler say?  Take two memory snapshots and compare them by hand.  Don't use Loitering Objects.  Clear all the filters and sort by name and compare the number of instances of everything in the list.

                             

                             

                            Alex Harui

                            Flex SDK Developer

                            Adobe Systems Inc.

                            Blog: http://blogs.adobe.com/aharui

                            • 11. Re: Memory Leak Solved, then Unsolved
                              Craig Swank Level 1

                              Thanks Alex.  When you take two memory snapshots, and compare them by hand, what exactly am I looking for?  Differences in the two lists?  Could you please explain why taking two snapshots is important?

                               

                              When I do look at a snapshot what jumps out is that 84% of the memory is in objects of type BitmapData.  The next highest item in the list is using 3.7% of the memory.  But most of those bitmapdata objects should be contained by the form, and that form should be released each time a new form is selected.

                              • 12. Re: Memory Leak Solved, then Unsolved
                                Craig Swank Level 1

                                So I did a diff of my flex project at work and at home, and get:

                                 

                                Suva:wind craig$ diff flex tmp/flex

                                Common subdirectories: flex/.FlexUnitSettings and tmp/flex/.FlexUnitSettings

                                diff flex/.actionScriptProperties tmp/flex/.actionScriptProperties

                                1c1

                                < <?xml version="1.0" encoding="UTF-8" standalone="no"?>

                                ---

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

                                diff flex/.flexProperties tmp/flex/.flexProperties

                                1c1

                                < <?xml version="1.0" encoding="UTF-8" standalone="no"?>

                                ---

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

                                Only in tmp/flex: .model

                                Common subdirectories: flex/.settings and tmp/flex/.settings

                                Common subdirectories: flex/.svn and tmp/flex/.svn

                                Common subdirectories: flex/bin-debug and tmp/flex/bin-debug

                                Common subdirectories: flex/libs and tmp/flex/libs

                                Common subdirectories: flex/services and tmp/flex/services

                                Common subdirectories: flex/src and tmp/flex/src

                                The one with working garbage collection is flex, and the one from work that leaks is tmp/flex.  It looks like I didn't check in .actionScriptProperties and .flexProperties.  They are different, with the non-memory leaking versions having standalone="no" and the memory leaking version has a hidden directory called .model.  I Changed the work version to match the home version, and cleaned it, restarted flash builder, and it still shows the leak behavior in the profiler.
                                Should I have checked these hidden files/directories into svn?  Also, does anyone know what standalone="no" means?

                                • 13. Re: Memory Leak Solved, then Unsolved
                                  Flex harUI Adobe Employee

                                  Yup, I turn off % display, then I compare the number of instances.  If you see two more bitmapdata instances than the previous snapshot, then you have to figure out why that is.

                                   

                                  Alex Harui

                                  Flex SDK Developer

                                  Adobe Systems Inc.

                                  Blog: http://blogs.adobe.com/aharui

                                  • 14. Re: Memory Leak Solved, then Unsolved
                                    Craig Swank Level 1

                                    Well, I had to go on a trip for work, so I installed flash builder on my personal macbook, and was about to dig into the profiler again.  I discovered that the garbage collection is working when I run the app on this computer as well.  So now garbage collection is working on two of the three computers I've tried this on.  I would really like to understand what is going on.  I really don't think the problem is anything with my code.

                                     

                                    When I actually deploy this app, we are going to install it on Windows computers that we are buying and distributing to a few folks around the country.  I'll be anxious to see if there is a memory leak on those computers.

                                     

                                    Yours in confusion,

                                     

                                    Craig

                                    • 15. Re: Memory Leak Solved, then Unsolved
                                      Flex harUI Adobe Employee

                                      If you compare snapshots where it isn't working, we might help you figure the odds that it will fail in the customer's hands.

                                       

                                      To prove your build environments are correct, simply copy the SWF from your personal macbook onto the machine that shows the problem.

                                       

                                      Alex Harui

                                      Flex SDK Developer

                                      Adobe Systems Inc.

                                      Blog: http://blogs.adobe.com/aharui

                                      • 16. Re: Memory Leak Solved, then Unsolved
                                        Craig Swank Level 1

                                        When I get back to my work computer on Monday, I will give the snapshots a thorough analysis (as thorough as I can give, that is), and also copy the SWF, as you suggest.  I'll let you know what I find.  Thanks again.

                                         

                                        Craig

                                        • 17. Re: Memory Leak Solved, then Unsolved
                                          Craig Swank Level 1

                                          I figured out what the problem was.  After looking at my profiler output I realized that my forms that I generate get garbage collected when the don't have an mx.controls.Image.  In the documentation for Image it says:

                                           

                                          "If you find memory problems related to Images objects, try explicitly setting the source property to null when you are done using the Image object in your application."

                                           

                                          I tried following this advice, and the result is garbage collection.  Hooray:

                                           

                                          Picture 2.png

                                          Thanks to the folks who tried to help me.  I need a beer.

                                           

                                          Craig

                                          • 18. Re: Memory Leak Solved, then Unsolved
                                            Craig Swank Level 1

                                            I fixed the memory leak, however, I never figured out why I was seeing working garbage collection on one computer, but not on the other.