I am experiencing what appears to be a periodic (~15 second interval) frame rate hitch in my app when running on a 2nd generation ipod touch. When it occurs the frame rate momentarily drops from 30fps to about 27.5. As the game I am working on relies on smooth controls, this hitch is rather disturbing.
I have tried disabling pretty much everything to isloate the cause, down to the point where I have nothing in motion and am only rendering a single piece of static geometry.
I also notice the same periodic hitch in the front-end screen which only has 2 guiText objects and a simple script to detect input.
Does anyone have any suggestions for what might be causing this, or how to track down the problem?
Why are your controls framerate dependent? Visually, why would anyone even notice a drop of that small an increment, for a short time; are you VBL syncing?
Most likely he’s using the frame rate tracker that’s posted on the wiki (or something similar) that polls the framerate over a short time, which gives him an average for that time period. He’s probably actually dropping to 15 fps during that time.
I’ve seen this quite a bit in my own game. Things are generally smooth, but every now and then some unexplained hitches occur. Garbage collection and heap allocations do a lot to make this occur, but I feel that there are some other, unexplained issues occurring as well.
Yes, I can confirm that I am using the frame rate tracker from the wiki which reports an average.
The controls are not frame-rate dependent, however the camera (which may be tracking the player for instance), will have a much larger displacement when this hiccup occurs, which is visually disturbing for the player.
As I am also seeing this hitch in the front-end with very little going on, (2xguiTextObjects and a simple script to get input and load the next scene), I am wondering if it is either:
a) something unusual in my project settings?
or
b) something systemic to unity iphone
Could you enable #define ENABLE_INTERNAL_PROFILER 1 in AppController.mm, try to “catch” moment when framerate drops and post results from XCode console.
From the top of my head I would suspect GarbageCollector. But it can be anything else First of all iPhone OS is not really a realtime OS, plus there are number of processes running in the background which can affect performance of your game. So drops from 30fps to occasional 20fps could be considered a norm.
In general your rendering code should be running frametime independently. You should multiply your camera movements by the Time.deltaTime.
NSTimers (like the timer that triggers rendering) are just events in the runloop, like touch events, and anything that can delay the runloop can delay the repaint.
ReJ: have you thought about using a more reliable timer, like maybe a CoreAudio timer? Not sure if that’s even possible, just a thought.
Thanks for looking into this. I setup a very simple test scene containing a camera and a textured plane. The problem still persists on a roughly 15 second interval.
I had these same “spikes” in my project, and it was related to having multiple levels in my game. For instance, if I loaded a ‘menu’ level first, and then from there loaded my ‘game’ level, the game level suffered from dramatic framerate spikes every 10-15 seconds or so, regardless of what was going on on screen. However if I just ran the game level first, everything was fine.
I eventually solved this by consolidating my menu and game into a single level, which was a major hassle, but it worked.
I’m seeing something similar to this and it’s driving me crazy. The frame rate hiccups from 30 to 15 and either some of the geometry or the sprites being managed by the SpriteManger drop out for a frame. It’s very disturbing and I wonder how to fix it.
PirateNinjaAlliance - right now I don’t have a GUI front end, I just load into the level I’m working on.
I’ve also seen these spikes and at the moment put this down to allocations, eg instantiating object. Boggybear in your profile the memory useGe is going up from 27 to 31 when the spike in main CPU occurs. Is it possible that a large alloc is occuring?
Curious. Your profile is very similar to what I have seen: an increase in heap allocation, a big peak in CPU and frametime, and a peak in the number of fixed updates. I put this down to various script behaviour in my game but as you only had a camera + plane, it makes it hard to attribute something to the performance hit.
I’m also not seeing the ~15 second interrupt.
I was going to suggest iPhone 3g protocol interruption but you’re also running on iPod touch which, I assume, doesn’t have the 3g protocol stack. So the only other thing I can think of is the garbage collector, which really should not be running during game play. For example, at the moment, with the profiler running and the game idling, I’m seeing a continuous increase in heap usage. I can only assume one of my scripts is doing that, otherwise there’s a deeper problem.
Edit: re: increasing heap usage, i.e. it’s leaking memory
Used heap is the amount of memory from its allocated part that the managed memory currently is using. The GC will not start to flush until that gets critically commonly.
Leaks would not appear in that statistic, they can only be found through Instruments which measures the real ram handling
Maybe “leak” is not the correct word in this sense, however, used heap memory is increasing by either 4096 or 8192 bytes every frame (where “frame” is the frequency of the output from the internal profiler). Therefore, something is allocating from managed memory. Over time, the GC will run and a big CPU hit will occur, e.g.
Here it shows that the heap has reached its maximum allocation size and the GC has been run to clear out the unused memory blocks, i.e. the “leaks”.
IMHO, the GC should not run during game play, it is not predictable and causes a large hit on frame rate. The GC should be performed under programmer control at timely points in the game, e.g. between level loads, with something like this in C#:
To do this however requires that allocations between GC runs do not exceed the maximum heap size, which in this case, because the engine is leaking, can’t happen.
I solved my issue and it was a problem with using the smooth follow camera script in a 2d game. I’m not entirely sure what was happening but while using instruments I noticed that it happened more often and I finally saw the camera “twist” in one of the hitches so I took off the script and the hitches disappeared. I honestly had no idea that’s what was going on and I’m glad I found at least the problem in my game but it took me disassembling the whole thing to find it. I was convinced it was just the way I was handling data and the garbage collector was the cause but I was wrong. I stopped using the smooth follow and just wrote my own. I hope this can help at least one person.
You guys mean the SmoothFollow2 camera from the wiki?
In what way did you change the smooth follow camera code? The wiki version is only using 5 functions. My hunch, is that one of those functions either has a bug or is doing a pointless allocation which overtime is causing the GC to run :? If that is the case, that is both interesting to know and also frustrating.