There was one thing I had intended to rant about last week that I just didn’t get to. People need to chill the heck out about multitasking on the iPhone. I see comments every day either decrying Apple’s implementation as not “real” multitasking, or helpless users asking how to make sure background tasks don’t grind them to a halt and kill their battery. Both of which tell me that there’s still a HUGE misunderstanding out there about how multitasking in iOS 4 actually works.
Let’s start with how it works on the desktop, which is what most people consider “real” multitasking. This is more formally known as “preemptive” multitasking, in that the operating system can preempt processes in favor of others to ensure that everything runs smoothly. Prior to the advent of multicore processors, the CPU could still only do one thing at a time, so the operating system had to decide which process got how many clock cycles. From the application perspective, they just ran without any consideration for other applications, and could continue asking for as much CPU power as they could get even if they were not the foreground application.
As anyone who’s tried to rip a DVD or transcode a bunch of media can tell you, even with a relatively smart preemptive multitasking operating system like modern day Windows, Linux or OS/X, background tasks can still use so much horsepower that they slow your “active” task to a crawl. On a laptop this is bad enough, but on a handheld, this can be the difference between having a smartphone and having a… rock. So clearly, a more sophisticated method was needed.
Or was it? The preemptive multitasking described above is essentially exactly how multitasking works on webOS. It is very similar to how multitasking works on Android (which is a little better at managing the resources of background tasks, but not much). And people with webOS and Android devices—and yes, Windows Compact Embedded, formerly known as Windows Mobile 6—learn very quickly how to manage their running programs and kill tasks that they don’t need running in the background. Very quickly, you get acclimated to micromanaging your phone, clearing memory and closing tasks and doing all sorts of digital housekeeping that has nothing whatsoever to do with what your phone is supposed to be actually doing.
Apple wanted none of this. And for the first three iterations of the iPhone, they simply didn’t provide multitasking for third party applications. They supported true preemptive multitasking for their own apps—iOS is built on the same core as OS/X, so it’s always been capable of it—but drew the line at apps that they could control. For everyone else, it was one thing at a time.
Which honestly, didn’t work so bad. Something I’ve noticed now that I have multitasking on my iPhone is that my attention span is just as fragmented as it is on my desktop. Not being able to flit back and forth between apps without losing your place did enforce a certain amount of focus. But anyway…
With iOS 4, Apple introduced their model of mobile multitasking. They didn’t invent multitasking, and no one is saying they did. But they did come up with a way to implement multitasking that seems to avoid the problems that desktop-grade multitasking has on mobile devices. The problem is that it’s also a little more nuanced, a little more complex, and a lot of people still haven’t figured out what it’s actually doing.
In iOS 4 when you double tap the Home button, the screen slides up and you see a row of application icons. THESE APPS ARE NOT ALL RUNNING AT THE SAME TIME. Depending on the app, they could be doing one of three things.
For apps that haven’t been updated to take advantage of iOS 4, the icons in the task switching area are exactly the same as the ones on the home screen. The app isn’t running, then when you tap the icon, it starts up from zero. When you switch away from the app, it goes away, removed from memory entirely until you call for it again. Needless to say, these apps use absolutely no CPU or RAM when not in use, even when you see them in the task switching area.
Apps that have been updated to support iOS 4’s fast app switching—but nothing else—behave a little differently. (To support fast app switching, all developers have to do is recompile their app with the iOS 4 SDK. They don’t have to add a single line of code. So eventually, all but completely abandoned apps will support this.) When you switch away from these apps, their process is suspended—using absolutely zero CPU—but they remain in memory. This is the key reason even this most basic multitasking feature isn’t supported on the iPhone 3G with its anemic 128MB of RAM. On a 3GS with 256MB or an iPhone 4 with 512MB, lots of applications can hang out, suspended, without impacting the speed or battery life of the device in any way. When you run the program again, either from the task switching area or the home screen, the process is simply unsuspended and resumes exactly where it left off. On occasion, you will run so many apps that you start to run out of free memory, in which case iOS will save a snapshot of the process to storage and kill the process. It will take a little longer to resume the next time you run it because that process snapshot has to be loaded into memory from “disk”, but it’s still faster than starting the program from scratch.
There are six other discrete functions apps can call to do things in the background. Sound can play in the background, VOIP apps can listen for an incoming call, GPS apps can keep tabs on your position, apps can continue a specific time-consuming task like uploading a file, etc. In these cases, part of the program does continue to run while this task is carried out, but there are two important things to remember. When the app is done doing the thing it has a good reason to be doing in the background, it suspends, just like apps that only support fast app switching, and the part of the app that remains running in the background is far smaller than the full application.
For example, when I’m listening to Pandora, I can switch away from it and let it play in the background. When I’m doing this, the part of the app that streams the music is all that keeps running. The rest of the app, the part with the user interface and menus and whatnot, is suspended. It no longer takes up any CPU of its own. When I put a GPS app in the background, it keeps track of my position and can even continue to give me voice directions, but the part of the app with the map no longer updates.
What does all this mean? It means that you don’t have to micromanage your device to prevent multitasking from killing your phone in iOS 4. Yes, you can manually “kill” an app, terminating the suspended process and removing it from RAM by tapping and holding on the icon in the switching area and then tapping the red minus sign, but you should almost never, ever have to. I’ve done this once, to force a Twitter app to pull tweets in the “gap” between updates. For just about everything else, you can just trust the device to kill off processes itself when it needs the RAM. You don’t have to do it yourself. (And yes, every once in a while I’ll notice an app “hitch” for a second when I’m loading it, telling me that something else just got purged from memory. It’s only noticeable because I’m looking for it, and doesn’t impede my use of the device in any way.)
It also means that the people complaining that the iPhone doesn’t support “real” multitasking need to take a deep breath and get over it. Apple’s implementation of multitasking is a compromise, yes, but it’s a very elegant, effective compromise that provides the benefits of full desktop-grade multitasking at a fraction of the resource cost. Smartphones are much more powerful now than they were just a few years ago, but until we have orders of magnitude faster processors and bigger batteries, Apple’s version of multitasking makes more sense than allowing any app to do whatever they want.