2
votes

I have an iOS application that grabs frames from the devices camera and does some quite CPU intensive image processing. On the iPad 2, iPad 3 and iPhone 4s the application happily runs at 30 frames per second (fps). I recently tested it on an iPhone 5 and most of the time it runs at 30 fps. Occasionally i've noticed it drop to around 15 fps, something I've never experienced on the other devices (devices that supposedly have slower hardware). I've tried hard to track this issue down, and I've learn a bunch of stuff but not found an explanation. Here are some of my observations that might give clues to what is going on:

  • It only happens on iPhone 5
  • I cant recreate the problem when trying to profile the application on the phone
  • The application is running on multiple threads, the frame grabbing and and converting the image into my required format run on a separate thread to the intensive image processing. The frame grabbing thread never drops below 30fps, its only the image processing thread that drops. This means that my problem is not due to the cameras frame rate dropping for some reason (such as low light levels).
  • I tend to be able to recreate the problem by starting the app, hitting the iPhones standby button, hitting the home button and going back into the application. If I repeat this 3-6 times the problem often occurs (but not always).
  • Doesn't appear to be related the iOS 6.0, it works fine on a 4s with iOS 6.
  • When the problem has occurred, even killing the app and restarting it often doesn't bring the framerate back up (suggesting its an issue outside of my app related to the iPhone 5).
  • Connecting the phone to my computer and rebuilding the app always seems to resolve the frame rate issues.

This is very odd. Could it be that the iPhone 5 reduces the clock speed of the phone sometimes to help with battery life (in a way that iPads and the iPhone 4s doesn't)?

I'd love to hear from anyone who as had similar experiences.

My application shouldn't look worse on iPhone 5 it should look better!

Many thanks in advance,

Kevin

Update

I've done some more tests but have still not found the problem. This is what I tried.

  • I've removed the threading
  • I've removed some third parties I was using
  • Removed all logging
  • Tried to recreate in instruments and failed. The problem does not occur in instruments

I've run out of ideas. Love to hear from any one who has experience frame rate slowdowns on the iPhone 5? Does the iPhone 5 have a power save mode?

1

1 Answers

2
votes

The most likely cause is a race condition in your code that only gets tripped by the particular timings on iPhone 5. I'd look around for anywhere you have resource contention and make sure that you're handling them correctly, and especially anywhere that you perform timeouts (since you don't fully deadlock or crash).

You mention that you're running on multiple threads. Do you mean that you're using NSThread, performSelectorInBackground:, NSOperation, or GCD? Using NSThread directly is a common cause of threading and resource problems because it's harder to do things correctly with it than with NSOperation or GCD.

If you ever call performSelectorInBackground:, I would highly suspect that piece of code since this call is extremely easy to use incorrectly (and should pretty much never be used). The fact that leaving the app and re-launching it tends to cause the problem would especially make me suspect that you're spawning threads manually in view controllers (which is a common mistake). The problem with performSelectorInBackground: is that there's no easy way to know that you've already spawned a thread for this. So you can wind up with many more threads than you meant to.

As in all performance things, you should start with Instruments. Look particularly at the time profiler and the threading instruments.

One more somewhat common cause of this kind of problem: audit your use of NSLog. It is extremely slow and synchronous. Running it on multiple threads can significantly impact performance.