How Battery Doctor Works on iOS
On a recent trip to India, me and my friends were on the airplane talking about how we needed to free up storage space for photos. My friend showed me an app she uses called “Battery Doctor” which frees up hard drive space and memory for her. Memory Cleaner boosts your available memory and Junk Cleaner cleans up your disk so you have more storage space. Spotify itself was a big abuser of storage space and she said it should clear it out.
I immediately had a small freak, I asked how is that even possible? Let me explain: on iOS every app cannot affect the storage or memory of any other app. On a computer, you can do it because any app has a lot of control over the entire system. An app on your laptop can kill other apps, access almost every file on disk, etc. But this isn’t how it works on iOS. Every app should be in a “sandbox”, which is a way of saying that the app is contained it’s own little box given everything to play with but doesn’t know about any other kids in the playground.
But Battery Doctor works! I had to spend a little bit of time researching this and figured it would be an interesting write up.
Boosting Memory
Retrieving memory information is easy, iOS expoeses APIs to tell you how much physical memory a device has, how much is used, and how much is not used. The problem is how does Battery Dcotor clear out the memory of other apps? It can’t just say “Kill the Facebook app”, no other app can kill any other app.
What Battery Doctor does is take up all the memory. It allocates fake objects so that it triggers iOS process for clearing out memory.
Let me explain: As soon as you start running out of free memory, iOS will ask each app to clear out some memory. Some apps that take up a lot of memory (say Maps) will remove memory intense parts of the app (like the tiles on the map), but other apps might ignore it. If iOS still needs to clear more memory it will just start killing apps.
When you run Battery Doctor, it’s just hogging up memory so that iOS thinks it’s drastically out of memory. iOS will never run kill the current process that’s taking up memory if it’s currently open, so only other apps will get affected. This ensures they aren’t doing anything tricky that Apple wouldn’t allow for security reasons. You can tell if an app has been killed from memory and has to be reloaded because you’ll see loading screens, which mostly consist of a simple background and logo.
Runkeeper loading from memory:
Runkeeper after being killed:
Boosting Disk Space
Battery Doctor is transparent on what it’s doing here. The app mentions it will “Clean Up Cache”. Battery Doctor should not be able to access any other app’s disk but it somehow able to clear up space, how? It’s using the same mechanism as the boosting memory!
Any app has two places for storage: Documents and Cache. If you store anything in Documents this is critical data for the app running or user’s generated information. It’s where Podcast caches the podcasts, it’s where Kindle keeps the copies of books it’s downloaded, etc.
The Cache folder is a place where you can keep data that is useful for the app to run but not necessary. Facebook would store your cached Newsfeed here, Spotify might store the music it’s downloaded, etc.
When you run the Clean Up Cache command you’ll notice a small popup up saying “Storage Almost Full”:
This is a warning from iOS itself because what Battery Doctor is writing fake files to the disk. It’s making iOS think the disk is full and triggering an emergency response. This will make each app have to clear that Cache Folder.
Do they work?
The Junk Cleaning feature is cleaning out files that take up disk space but you can’t delete yourself. In Settings, you can’t clear the cache of any individual app and most apps don’t expose cleaning out their cache. Battery Doctor covers a gap that Apple has missed.
The only problem is that developers can cheat this Cache experience, they can mark their caches as not deletable by this process. In iOS 5.0 when this Cache folder concept was introduced, many developers complained about how their “offline” experience was tarnished by this process. The cleaning process would wipe out cached files unexpectedly and suddenly a user complains to the app developer that their files are gone. In iOS 5.0.1, Apple introduced a flag to mark files as Cached but not to be swept up.
Most apps don’t want to lose their offline data because it makes them look bad. Imagine if you cleared out all of Facebook’s cached data. When you opened the app it would have to reload everything: your feed, your events, etc. It would make Facebook look bad because it took longer. But imagine you did it on a slow connection or if you didn’t even have internet you would get nothing. Facebook could explain this, but I imagine it’s not worth the effort. You’ll notice that if you try to run Facebook app again after cleaning out the Cache, it will still loads your cached NewsFeed.
The memory process is hogwash, you don’t need to manage memory on iOS. All it’s doing it artificially clearing out things for you and if anything it’s making each app take longer to load next time. If you allow the app to load from memory it will take less time to start up.
Hopefully, this was a clear explanation of how Battery Doctor works. Obviously, this is my best educated guess since I don’t actually have Battery Doctor’s source code. I’m confident though that the main mechanism of how it works is accurate, I’ve been doing iOS apps for a few years now and know the limitations of the system and validated my ideas with testing.