There was one thing I had intended to rant about last week that I just didn’t get to. Peo­ple need to chill the heck out about mul­ti­task­ing on the iPhone. I see com­ments every day either decry­ing Apple’s imple­men­ta­tion as not “real” mul­ti­task­ing, or help­less users ask­ing how to make sure back­ground tasks don’t grind them to a halt and kill their bat­tery. Both of which tell me that there’s still a HUGE mis­un­der­stand­ing out there about how mul­ti­task­ing in iOS 4 actu­ally works.

Let’s start with how it works on the desk­top, which is what most peo­ple con­sider “real” mul­ti­task­ing. This is more for­mally known as “pre­emp­tive” mul­ti­task­ing, in that the oper­at­ing sys­tem can pre­empt processes in favor of oth­ers to ensure that every­thing runs smoothly. Prior to the advent of mul­ti­core proces­sors, the CPU could still only do one thing at a time, so the oper­at­ing sys­tem had to decide which process got how many clock cycles. From the appli­ca­tion per­spec­tive, they just ran with­out any con­sid­er­a­tion for other appli­ca­tions, and could con­tinue ask­ing for as much CPU power as they could get even if they were not the fore­ground application.

As any­one who’s tried to rip a DVD or transcode a bunch of media can tell you, even with a rel­a­tively smart pre­emp­tive mul­ti­task­ing oper­at­ing sys­tem like mod­ern day Win­dows, Linux or OS/​X, back­ground tasks can still use so much horse­power that they slow your “active” task to a crawl. On a lap­top this is bad enough, but on a hand­held, this can be the dif­fer­ence between hav­ing a smart­phone and hav­ing a… rock. So clearly, a more sophis­ti­cated method was needed.

Or was it? The pre­emp­tive mul­ti­task­ing described above is essen­tially exactly how mul­ti­task­ing works on webOS. It is very sim­i­lar to how mul­ti­task­ing works on Android (which is a lit­tle bet­ter at man­ag­ing the resources of back­ground tasks, but not much). And peo­ple with webOS and Android devices — and yes, Win­dows Com­pact Embed­ded, for­merly known as Win­dows Mobile 6 — learn very quickly how to man­age their run­ning pro­grams and kill tasks that they don’t need run­ning in the back­ground. Very quickly, you get accli­mated to micro­manag­ing your phone, clear­ing mem­ory and clos­ing tasks and doing all sorts of dig­i­tal house­keep­ing that has noth­ing what­so­ever to do with what your phone is sup­posed to be actu­ally doing.

Apple wanted none of this. And for the first three iter­a­tions of the iPhone, they sim­ply didn’t pro­vide mul­ti­task­ing for third party appli­ca­tions. They sup­ported true pre­emp­tive mul­ti­task­ing for their own apps — iOS is built on the same core as OS/​X, so it’s always been capa­ble of it — but drew the line at apps that they could con­trol. For every­one else, it was one thing at a time.

Which hon­estly, didn’t work so bad. Some­thing I’ve noticed now that I have mul­ti­task­ing on my iPhone is that my atten­tion span is just as frag­mented as it is on my desk­top. Not being able to flit back and forth between apps with­out los­ing your place did enforce a cer­tain amount of focus. But anyway…

With iOS 4, Apple intro­duced their model of mobile mul­ti­task­ing. They didn’t invent mul­ti­task­ing, and no one is say­ing they did. But they did come up with a way to imple­ment mul­ti­task­ing that seems to avoid the prob­lems that desktop-​grade mul­ti­task­ing has on mobile devices. The prob­lem is that it’s also a lit­tle more nuanced, a lit­tle more com­plex, and a lot of peo­ple still haven’t fig­ured out what it’s actu­ally doing.

In iOS 4 when you dou­ble tap the Home but­ton, the screen slides up and you see a row of appli­ca­tion icons. THESE APPS ARE NOT ALL RUNNING AT THE SAME TIME. Depend­ing on the app, they could be doing one of three things.

For apps that haven’t been updated to take advan­tage of iOS 4, the icons in the task switch­ing area are exactly the same as the ones on the home screen. The app isn’t run­ning, then when you tap the icon, it starts up from zero. When you switch away from the app, it goes away, removed from mem­ory entirely until you call for it again. Need­less to say, these apps use absolutely no CPU or RAM when not in use, even when you see them in the task switch­ing area.

Apps that have been updated to sup­port iOS 4’s fast app switch­ing — but noth­ing else — behave a lit­tle dif­fer­ently. (To sup­port fast app switch­ing, all devel­op­ers have to do is recom­pile their app with the iOS 4 SDK. They don’t have to add a sin­gle line of code. So even­tu­ally, all but com­pletely aban­doned apps will sup­port this.) When you switch away from these apps, their process is sus­pended — using absolutely zero CPU — but they remain in mem­ory. This is the key rea­son even this most basic mul­ti­task­ing fea­ture isn’t sup­ported on the iPhone 3G with its ane­mic 128MB of RAM. On a 3GS with 256MB or an iPhone 4 with 512MB, lots of appli­ca­tions can hang out, sus­pended, with­out impact­ing the speed or bat­tery life of the device in any way. When you run the pro­gram again, either from the task switch­ing area or the home screen, the process is sim­ply unsus­pended and resumes exactly where it left off. On occa­sion, you will run so many apps that you start to run out of free mem­ory, in which case iOS will save a snap­shot of the process to stor­age and kill the process. It will take a lit­tle longer to resume the next time you run it because that process snap­shot has to be loaded into mem­ory from “disk”, but it’s still faster than start­ing the pro­gram from scratch.

There are six other dis­crete func­tions apps can call to do things in the back­ground. Sound can play in the back­ground, VOIP apps can lis­ten for an incom­ing call, GPS apps can keep tabs on your posi­tion, apps can con­tinue a spe­cific time-​consuming task like upload­ing a file, etc. In these cases, part of the pro­gram does con­tinue to run while this task is car­ried out, but there are two impor­tant things to remem­ber. When the app is done doing the thing it has a good rea­son to be doing in the back­ground, it sus­pends, just like apps that only sup­port fast app switch­ing, and the part of the app that remains run­ning in the back­ground is far smaller than the full application.

For exam­ple, when I’m lis­ten­ing to Pan­dora, I can switch away from it and let it play in the back­ground. When I’m doing this, the part of the app that streams the music is all that keeps run­ning. The rest of the app, the part with the user inter­face and menus and what­not, is sus­pended. It no longer takes up any CPU of its own. When I put a GPS app in the back­ground, it keeps track of my posi­tion and can even con­tinue to give me voice direc­tions, 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 micro­man­age your device to pre­vent mul­ti­task­ing from killing your phone in iOS 4. Yes, you can man­u­ally “kill” an app, ter­mi­nat­ing the sus­pended process and remov­ing it from RAM by tap­ping and hold­ing on the icon in the switch­ing area and then tap­ping the red minus sign, but you should almost never, ever have to. I’ve done this once, to force a Twit­ter app to pull tweets in the “gap” between updates. For just about every­thing else, you can just trust the device to kill off processes itself when it needs the RAM. You don’t have to do it your­self. (And yes, every once in a while I’ll notice an app “hitch” for a sec­ond when I’m load­ing it, telling me that some­thing else just got purged from mem­ory. It’s only notice­able because I’m look­ing for it, and doesn’t impede my use of the device in any way.)

It also means that the peo­ple com­plain­ing that the iPhone doesn’t sup­port “real” mul­ti­task­ing need to take a deep breath and get over it. Apple’s imple­men­ta­tion of mul­ti­task­ing is a com­pro­mise, yes, but it’s a very ele­gant, effec­tive com­pro­mise that pro­vides the ben­e­fits of full desktop-​grade mul­ti­task­ing at a frac­tion of the resource cost. Smart­phones are much more pow­er­ful now than they were just a few years ago, but until we have orders of mag­ni­tude faster proces­sors and big­ger bat­ter­ies, Apple’s ver­sion of mul­ti­task­ing makes more sense than allow­ing any app to do what­ever they want.