Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determining XNA Game Lag

I'm currently creating a tower defense game using C# & XNA. The games runs fine and smoothly for a while but after playing for a good amount of time (after sufficient enemies/towers/bullets have spawned) the game seems to slow exponentially. Even when all instances are cleared the lag still persists.

At first I thought this might have to do with garbage collection (and perhaps it does) but to test it I wrote destructors to print when an object was collected and everything looks fine (all objects are collected). So I'm curious if anyone has any experience with lag in XNA and the possible causes of it?

EDIT: This is for PC

like image 507
Hanna Avatar asked Sep 07 '11 15:09

Hanna


2 Answers

There's two things that I've used to check out performance.

1) App Hub has a Performance utility that you can use to help determine what youc an improve.

2) This is a bit old now, but for the Xbox 360 this document helped me a lot.

Update: also I forgot about this Gamefest 2010 presentation. It also goes over a few things.

like image 192
scottheckel Avatar answered Oct 11 '22 15:10

scottheckel


If you're worried that garbage collections are impacting performance, one of the best tools you can learn to use is the CLR Profiler. This utility allows you to profile the heap allocations performed by your program, so that you can identify which exactly methods are generating garbage. Remember that a lot of non-obvious things can allocate onto the heap: concatenating strings, indexing dictionaries with enumeration keys, closures, delegates, etc. Even a little garbage, generated once per frame at 60+ frames per second, can quickly add up under the right circumstances.

That said, what you've described doesn't sound like a problem with garbage collection to me. The GC is generally quick enough, even during a complete collection, to only cause a few frames to be dropped -- in other words, you'll notice a minor, annoying jerk every so often, but not a persistent slowdown.

(Caveat: this only applies on the PC, which has a very sophisticated GC compared to other XNA platforms.)

You should try attaching a profiler to your code to identify which methods are taking the longest to complete; if your problem isn't related to GC, this may be informative. In the past, I've used EQATEC, although I've had issues with some of their more recent versions. You can try that one, or you can look around on Google for an alternative.

like image 39
Cole Campbell Avatar answered Oct 11 '22 14:10

Cole Campbell