22 Replies Latest reply on Mar 24, 2009 10:31 AM by Ben Spink

    Huge Memory Leak, CPU Usage

    Ben Spink
      We are noticing in several of our apps a very bad memory leak. See the below code. Its called every 100ms to process any messages as we use this like a queue system. Overnight, not a single message was sent...as the app was just sitting idle on my screen. My server log confirm this, not a single post was done. However, memory usage jumped 300MB, and by the end of today, my browser is going to crash when it runs out of memory.

      The memory leak seems to be directly related to the setTimeout function, as that is the only thing being called here...there was nothing put on the queue all night.

      Specifically, Iw as at roughly 50MB of memory usage yesterday afternoon. After a bunch of testing, the memory usage was up to 175MB. Overnight we are now over 475MB. This is the internal memory used by Flash, and my Activity monitor confirms this.

      This issue happens on OS X, and Windows XP, Flash 9 and Flash 10. In 3 different application we have that do this sort of queue setTimeout loop.

      Anyone else seeing this?

      How can I work around this?

      Thanks,
      Ben
        • 1. Re: Huge Memory Leak, CPU Usage
          Ben Spink Level 1
          I should have done more searching...

          Apparently this is a known issue and people use a Timer to avoid this? That is bad...but I can make that work.

          --Ben
          • 2. Re: Huge Memory Leak, CPU Usage
            Ben Spink Level 1
            Turns out, using a Timer does not resolve the memory leak. Maybe its less, its hard to tell. But since my original post followup this morning, my flex app has gone from 28MB to 70MB.

            So event a timer firing an event every 100ms isn't a solution...?

            Help!
            • 3. Re: Huge Memory Leak, CPU Usage
              Level 7

              "Ben Spink" <webforumsuser@macromedia.com> wrote in message
              news:gomm0u$1vd$1@forums.macromedia.com...
              > Turns out, using a Timer does not resolve the memory leak. Maybe its
              > less, its
              > hard to tell. But since my original post followup this morning, my flex
              > app
              > has gone from 28MB to 70MB.
              >
              > So event a timer firing an event every 100ms isn't a solution...?

              enterFrame event handler?

              Or just use LCDS.


              • 4. Re: Huge Memory Leak, CPU Usage
                Ben Spink Level 1
                How would either of those resolve the issue?

                I'm just looking for a fast timer situation that is polling my queue for any requests that need to be sent.

                More importantly...why does this issue exist? Why hasn't this issue been fixed...it seems its been around for quite some time.

                I can't leave my browser open in the background if I have a flex app open. Its going to hang my browser and system...as its already done during a a live demo to a customer.

                There are other bugs, and I can excuse them, but something as basic as this creating a memory leak is really terrible.

                --Ben
                • 5. Re: Huge Memory Leak, CPU Usage
                  Level 7

                  "Ben Spink" <webforumsuser@macromedia.com> wrote in message
                  news:gonn46$afd$1@forums.macromedia.com...
                  > How would either of those resolve the issue?

                  AFAIK, there are no inbuilt memory leak problems with using an enterFrame
                  event handler. LiveCycle Data Services will push the information to you, so
                  you don't have to constantly try to pull, so to speak..

                  > I'm just looking for a fast timer situation that is polling my queue for
                  > any
                  > requests that need to be sent.
                  >
                  > More importantly...why does this issue exist? Why hasn't this issue been
                  > fixed...it seems its been around for quite some time.

                  It's possible that it's not Flex, but you. Are you constantly adding event
                  listeners without removing them? Are you removing objects from the stage
                  without removing all references to them?

                  > I can't leave my browser open in the background if I have a flex app open.
                  > Its going to hang my browser and system...as its already done during a a
                  > live
                  > demo to a customer.
                  >
                  > There are other bugs, and I can excuse them, but something as basic as
                  > this
                  > creating a memory leak is really terrible.

                  I wouldn't be so quick to blame Flex. You have to make sure you clean up
                  after yourself, or you can cause these types of issues with your code.


                  • 6. Re: Huge Memory Leak, CPU Usage
                    Ben Spink Level 1
                    And I wouldn't be so quick to blame me... :) I'm not new at this whole programming thing. I understand GC, and what is going on here.

                    You have the exact code snippet above that creates this leak. I have since modified it to not use the setTimeout function, but rather have a single timer function I made at application initialization who's timer event calls this one function. (I removed setTimout from this function, and added the timer event as an argument tot he function.)

                    I'll be back at my dev machine after it sat overnight, but letting this sit yesterday for 6+ hours resulted in it leaking memory still.

                    If you look in the forums, lots of people have memory leaks...lots of them are doing bad things, and don't realize why the leak is happening, but I'm doing an empty function. No activity, and it leaks memory.

                    Heck, I've got all sorts of other bugs, XML parsing bugs, moduleUnload / ModuleLoad bugs, etc. Those are serious...but this memory leak is ridiculous.

                    --Ben
                    • 7. Re: Huge Memory Leak, CPU Usage
                      Ben Spink Level 1
                      Using the Timer event does actually seem to prevent the leak. Overnight, the app did not gain memory...it settled at about 175MB from its use during the day. So there may be some other areas that also leak memory, as we do use setTimeout in some other places, but the places where its used at a short interval no longer leak memory after changing to using a Timer.

                      I restarted my browser this morning, and the app is currently polling XML messages every 10 seconds, and has been for several hours and is holding steady at 45MB used. So even not being idle, its still not leaking.

                      The Timer is the key, do not ever use setTimeout for a repeating loop!

                      --Ben
                      • 8. Re: Huge Memory Leak, CPU Usage
                        injpix Level 3
                        Nice. That's good you found a solution and posted it. I was looking at your code this morning for about an hour and did some digging around in the framework. Do you know of anything with setPropertyIsEnumerable ? Would it help you in this situation?
                        • 9. Re: Huge Memory Leak, CPU Usage
                          Ben Spink Level 1
                          I don't think that helps. Essentially what I want is a way to notify a waiting thread that it should process the items in the queue...but since flex doesn't have threads, there isn't a way to really do that. So I just settle for a fairly fast time event.

                          There might be a way to do this with an event handler on my object that fires off this event every time it notices it change...but since this timer method seems to work, I settle with it. Another day and overnight, and the app is at 114MB. In this case its been reloading a fairly large XML string every 10 seconds to fill a datagrid. Its been doing this for about 23 hours.

                          --Ben
                          • 10. Huge Memory Leak, CPU Usage
                            Ben Spink Level 1
                            Ok, following up to my own thread. This issue is not resolved or worked around.

                            Using a Timer does not resolve the issue. It only delayed it.

                            Over the weekend my flex app used 456MB of RAM. It was idle all weekend, doing absolutely nothing in the timer loop. All weened long, sendQueue was empty as I was not here to click the mouse to add an action onto the queue. One other timer was active, doing this same sort of things, but it was updating the current time display on the page. Additionally, the only reason I think it stopped at 456MB is that something internal in flex died. This morning I tried clicking some of my buttons on the app, and the requests were never sent. Further, I can see the time updater stopped too as the time reads 7:37 and the time is really 9:35 at the moment...and its been 7:37 for a long time now. Had it not stopped, I'm sure my browser would have crashed.

                            • 11. Re: Huge Memory Leak, CPU Usage
                              injpix Level 3
                              Have you profiled this app?
                              • 12. Re: Huge Memory Leak, CPU Usage
                                Ben Spink Level 1
                                No...we don't own the "Pro" version of Flex Builder as its $500 to upgrade. :(

                                Profiling would give us what was still in memory, but not why. From the above example...this is so basic, there is no reason for the leak. I can make a tiny Flex application to demonstrate the bug...someone else could analyze the results with Profiler...

                                I also know the time updater has no effect as other apps of ours have this same leak, and in those cases, they only have the main looping timer event checking for actions to submit.

                                --Ben
                                • 13. Re: Huge Memory Leak, CPU Usage
                                  Ben Spink Level 1
                                  Overnight, it leaked up to 547MB of RAM usage...it hasn't died this time (yet), the time is still ticking away and changing...

                                  This is really terrible. A Flex app cannot be used as a long running application. Its apparently meant only to be a toy app for a web page that is short lived... Its expected that the user will reload the page, or browse away from the page...?
                                  • 14. Re: Huge Memory Leak, CPU Usage
                                    quotto
                                    Have you tried Amy's suggestion of trying the enterFrame event instead of a timer? It defaults to 24 per second but can be set in the application tag.
                                    • 15. Re: Huge Memory Leak, CPU Usage
                                      Ben Spink Level 1
                                      Forgotten about it...trying it again. I will report back.
                                      • 16. Re: Huge Memory Leak, CPU Usage
                                        augur_guan
                                        Flex Memory management is also mystery for me. I used profile the application and I found that the used memory is 27M for 300 lines of record, but it's only 7M to display 3000 lines of record in a DataGrid. I don't know what is happening. Different memory management system or something wrong in Profile Tool?
                                        Sorry for using your master post to ask my own question.
                                        • 17. Re: Huge Memory Leak, CPU Usage
                                          augur_guan Level 1
                                          Flex Memory management is also mystery for me. I used profile the application and I found that the used memory is 27M for 300 lines of record, but it's only 7M to display 3000 lines of record in a DataGrid. I don't know what is happening. Different memory management system or something wrong in Profile Tool?
                                          Sorry for using your master post to ask my own question.
                                          • 18. Re: Huge Memory Leak, CPU Usage
                                            Ben Spink Level 1
                                            OK, letting it run another day with the enterFrame handler, still leaking memory. When I loaded the page, and left it sitting idle yesterday, browser was at 183MB of RAM usage. Now, a day later, I was out all day, no one touched my locked machine, only browser window open...we are at 295MB of RAM usage. Here is the modification I made:

                                            addEventListener(Event.ENTER_FRAME, onEnterFrame);
                                            private function onEnterFrame(event:Event):void
                                            {
                                            startSending();
                                            }

                                            That was it...

                                            So now what? Its leaking memory, regardless of what I do.
                                            • 19. Re: Huge Memory Leak, CPU Usage
                                              Ben Spink Level 1
                                              Actually, things are *much* worse then I thought. The enterFrame handler doesn't leak the memory...just having a flex application open does. I removed all loops, and left it sitting with 4 modules loaded in a tabbed navigator...idle.

                                              Nothing was doing anything, no setTimeouts, no Timers, or onFrameEnter events.

                                              I used 3 browsers, Safari 4 beta, Firefox 3.0.7, and IE 7.
                                              Start:
                                              IE: 64MB
                                              Firefox: 75MB
                                              Safari: 163MB

                                              20 hours later ended:
                                              IE:150MB
                                              Firefox: 567MB
                                              Safari: 252MB

                                              That is not a typo on Firefox...in all cases, the browser was in the background and not touched for 20 hours. Only one browser instance of each was open, with one single tab.

                                              Now I have to try a new version of the flash player...but this totally negates Adobe's claims that 99% of browsers have flash...well, 98.5% have a version of flash that is buggy and will leak memory, the other half of a percent might have a browser with a version of flash that doesn't leak as fast (and maybe it does...have to test)???
                                              • 20. Re: Huge Memory Leak, CPU Usage
                                                Ben Spink Level 1
                                                I'm going to let my test keep going overnight again, but with firefox sitting idle in the background, only one tab open, with the flex app completely idle, using the latest Flash 10 debug version installed this morning...

                                                Start:
                                                78MB

                                                3 hours later:
                                                140MB

                                                This is a completely idle flex app...and now the latest Flash 10. There is no animation, no events, no timers, no timeouts...what is stealing memory?

                                                --Ben
                                                • 21. Re: Huge Memory Leak, CPU Usage
                                                  injpix Level 3
                                                  Show us a simplified version of your code, because I am not sure what how your code is leaking. When you posted code on the 16th, it seems that the application isn't sending or receiving any objects across-the-wire. And it is still accumulating memory!? I don't see how that code can have a leak, unless I am missing something.
                                                  • 22. Re: Huge Memory Leak, CPU Usage
                                                    Ben Spink Level 1
                                                    I've given up on this issue. Apparently using a Timer is the solution, as the setTimeout will definitely cause a memory leak.

                                                    Firefox still leaks memory, 400MB overnight. However, just clicking anywhere in the flash window causes firefox to free up all the leaked memory within about 5 seconds...until more time passes and more memory is leaked.

                                                    IE and Safari leaked too, not sure if a click in them would have released the memory back or not though...

                                                    I started making an example app to demonstrate this issue, but it got to be a pain as there were dependent modules that had to be loaded in order to simulate everything in the same way.

                                                    Thanks,
                                                    Ben