2

I'm writing a game, and while it's performance is good most of the time, it sometimes slows to a crawl. Normal profilers haven't been a help in this, as they record the whole time my game is running, and the important info in the laggy portions get diluted over all of the smooth sections. Are there any tools or libraries that can help me isolate the problem?

RCIX
  • 35,702
  • 48
  • 141
  • 204
  • This could be due to garbage collection. Are you creating and releasing a lot of objects? – Brian Rasmussen Aug 20 '10 at 07:55
  • Actually, i think i am. I implemented a performance view, and what happens is my memory used will tick up constantly and be shoved down a couple megabytes every couple of seconds or so, and over time the base memory consumption will scoot up. – RCIX Aug 20 '10 at 08:10

2 Answers2

3

You're almost certainly being hit by garbage collection.

And yes there is a tool for fixing that. You want the CLR Profiler. This will show you exactly where you're allocating memory.

On Xbox the garbage collector will run after every 1MB allocated and it is slow. The Windows GC is much more forgiving, but can still cause frame-rate jitter.

The best way to avoid garbage collection stutters is to not allocate anything while your game is running. Do your allocations during loading screens where the user won't notice.

I recommend reading this post and this post on Shawn Hargreaves' blog.

Andrew Russell
  • 26,137
  • 7
  • 54
  • 104
  • Ok, i got some data from the profiler and i'm waiting for it to load, but in the meantime are there any other profilers available that won't turn my game into a literal slideshow? – RCIX Aug 20 '10 at 10:35
  • By its very nature, profiling is going to bog down your game a bit. But even at that, it won't change your allocation profile ... so you can see where garbage is being created – Joel Martinez Aug 20 '10 at 10:51
  • @Joel: that's true, but this isn't "a bit": this is going from (at most points) 60FPS to 1 frame every 3 or so seconds. Very hard to control to get a decent run. – RCIX Aug 21 '10 at 00:16
  • After i fixed a constant creation of Texture2Ds, i'm seeing a total of 593 objects finalized over a 30 second run, and the profiler says that none of my objects were being tossed away. My problem also isn't solved :/ – RCIX Aug 21 '10 at 00:41
  • @RCIX: 593 objects finalized seems very high. But that's not what you want to look at anyway. The button you're after is `Time Line`. Ideally it will plateau after your game has started. Use the Allocation Graph (particularly through "Show Who Allocated") to narrow down further. Also: The CLR Profiler logs allocations and GCs. If you're not allocating during your game, then it should have very little effect on your framerate. – Andrew Russell Aug 21 '10 at 01:28
  • I think i've isolated the problems, and i'm going to be using some tasteful pooling of objects to try and cut down on the GCing. If that doesn't fix it, it would be totally awesome if you could take a look at the log itself :) – RCIX Aug 21 '10 at 02:46
  • @RCIX: I was going to reply and tell you that, when I did a quick test run of the profiler on my current game to formulate my last reply, it produced a whopping, unsendable 1.5GB log file. But actually I tried compressing it just now - it's plain text and so 7-Zip reduced that to a few MB. – Andrew Russell Aug 21 '10 at 03:25
1

You say you're going from a frame taking 16ms to 3000ms. So in a long frame, it is spending (3000 - 16)/3000 = 99.5% of its time doing something you didn't expect.

During those 3 seconds, how can you find out what it's doing?

Just pause it a few times, and check the call stack.

The chance you won't see what it's doing on each pause is 0.5%

If it's in the garbage collector, this should confirm it. If it's doing something else, it will also tell you.

Mike Dunlavey
  • 38,662
  • 12
  • 86
  • 126
  • More properly, what happens is the game slows down to 2-5 frames a second, but my update time taken doesn't show up that way. The slideshow (several seconds between frames) is only when i memory profile it. – RCIX Aug 21 '10 at 20:36
  • @RCIX: nevertheless, pause it when it's being slow, and you'll see the problem. (5fps = 12x slowdown, so chance of catching it on each pause is 92%.) People aren't used to this concept, but if you try it will come to seem natural. The reasons why, in excrutiating detail, are here: http://stackoverflow.com/questions/1777556/alternatives-to-gprof/1779343#1779343 – Mike Dunlavey Aug 22 '10 at 00:23