10 Replies Latest reply on Jun 5, 2007 10:04 PM by kglad

    Performance trouble


      I'm having a strange sort of performance problem with my game where it works fine for about 30 seconds, then my CPU fan kicks on and a few seconds later the performance suddenly degrades. I have it set up to run a level that loops the same pattern of objects in a tight interval, so there is nearly the same count of objects on screen in the same positions throughout the run. I did some timing tests with getTimer() and found that while there is no single bottleneck, everything takes approximately twice as long. In fact, I added a dummy loop that increments a variable 10,000 times each frame, and found that it takes about 7ms before the slowdown, but 15ms afterwards, which indicates that actionscript execution is slowing down altogether.

      My only guess as to what would cause this type of slowdown is if the garbage collector or some other internal mechanism were really working hard (maybe running in another thread interleaved with AS execution), but I was wondering if anyone else had seen this type of behavior before. It seems unusual to me that any actionscript I write is slowed down by this, rather than an isolated chunk taking longer, so perhaps somebody who knows more about Flash internals than me can help.

        • 1. Re: Performance trouble
          Level 7
          Never heard about it. Most of the time something isn't removed.

          Have you watched the memory use of the movie? Find it at "Tast-Manager"
          and Systemperformance (don't know the right name ion english windows).

          Take a look in the debugger and watch if some clips are not removed or
          behave strange. As I said most of the time you forget to remove clips,
          scripts or intervalls.
          • 2. Re: Performance trouble
            okself Level 1
            Thanks for your reply. That was my first thought too, however I don't think it's the problem for a few reasons:

            -I modified the MovieClip prototype so that onLoad and onUnload keep a counter of how many MovieClips are alive, and it stays within the 45-50 range, even after the program slows down. I don't use intervals or assign scripts at runtime.
            -The slowdown is really instantaneous. One frame takes ~30ms, then suddenly the next takes ~60. Forgetting to remove clips should cause gradual slowdown.
            -This might be a misunderstanding of how Flash works, but I don't think an increase in the number of objects around should slow down the dummy loop I described in my first post.

            I did check the task manager, and while the memory usage is fairly stable, the CPU usage jumps when the game slows down. Also, I noticed that it's taking up a lot of memory total, ~22 MB. An astounding 12 MB of that is from one big lookup table I have, which really shocked me - when I was guessing how much memory it would use, I based my guess off how much it would take in C (~1MB) and estimated maybe 100-200% overhead. Didn't think I'd be off by a factor of 12 :) Anyway, removal of the table make it take much longer for the program to slow down, about 2-3 minutes, but it still happens. I think this lends some credence to the garbage collection theory: the only thing that changed was the memory usage, since the table is just used to say how to use the draw commands, so the only logical effect on the program was that things were drawn in the wrong places.
            • 3. Re: Performance trouble
              kglad Adobe Community Professional & MVP
              there is a problem with the flash drawing api in as2 and as3. with each line/curve segment created, cpu usage increases. this effect is noticable once you get beyond 10,000 segments.
              • 4. Re: Performance trouble
                okself Level 1
                Well, I think you hit it on the head. I removed the draw calls, while still doing all the computation, and no slowdown. I'm off to try and find a workaround, thanks for your help :)
                • 5. Re: Performance trouble
                  kglad Adobe Community Professional & MVP
                  you're welcome. please follow-up in this forum if you find a work-around.

                  i tried many work-arounds in as2 with no resolution.
                  • 6. Re: Performance trouble
                    okself Level 1
                    hm...you said it was a problem with as2 and 3, I don't know if this is how you meant it but is there an as1 drawing API wwith no bug? Would it be possible to include a bit of as1 code in a Flash 8 doc and just pass the drawing points to it from as2 code?

                    Otherwise, I think I'm just going to use bitmap fills, which should work for the particular type of drawing I'm doing.
                    • 7. Re: Performance trouble
                      kglad Adobe Community Professional & MVP
                      the same issue exists using as1.

                      avoiding the drawing api altogether and using bitmaps to "draw" may be workable. it's something i have not tried. i'll run some tests today to see if that's a promising approach.

                      addendum: i just ran a quick test and using the bitmapdata class in as2: it is much faster than the drawing api and does not appear to suffer the slow-down with repeated use. more testing is needed.
                      • 8. Re: Performance trouble
                        kglad Adobe Community Professional & MVP
                        there's no slowdown after millions of "drawings" using the bitmapdata class!!
                        • 9. Performance trouble
                          okself Level 1
                          I don't think it will work out for me...my game is 3D, so the bitmaps need to be skewed and scaled and stuff, and while I got it working, it's very slow.

                          Also, I think the drawing may not be the cause of my problem here. There's still the old 50% slowdown after the game runs for a while with the bitmaps, and I think perhaps the reason I didn't see the slowdown when I stopped drawing was that without drawing there's virtually nothing for flash to do anyway. Also, I remembered that I have actually done something similar before, just using the drawing API, that didn't have similar problems: http://www.newgrounds.com/portal/view/288800 (now why the heck doesn't that slow down :P)

                          Anwyay, I think I might try just starting from scratch and putting it back together piece by piece. I've been hunting this bug for more than two days now, and I'm getting sick of it :P thank you for your help, though
                          • 10. Re: Performance trouble
                            kglad Adobe Community Professional & MVP
                            you're welcome. i'm sorry i wasn't able to help you.

                            but, if it's any consolation, this helped me.