[Android General] T.J. Bender:
A Comprehensive (but not by any means definitive) Guide to Wakelocks

OP edit, January 12, 2016: To say that I'm proud of what this guide grew into would be an understatement, given that drawing it up was a weekend project for me using my old Galaxy S2 Skyrocket and either ICS or JB.

Take that last little bit into account here. It was done over three years ago on an S2-variant phone using Android 4.0 or 4.1. A lot of the specifics here are no longer going to be 100% correct, even if the principles remain true. If you're a rookie de-wakelocking your phone for the first time ever, please take what you see in the OPs with a grain of salt and ask questions in the thread before making any drastic system changes or mucking around in a terminal emulator.

Thanks for stopping by, and I hope you know a little bit more about your phone when you're done here than you did coming in! :)

-TJ

----

Wakelocks suck. If you're trying to maximize your battery life, you know this already. Some wakelocks are happy, friendly things, but many are silent leeches, sucking away your battery life while you remain blissfully unaware of what's happening. The goal of this guide is to list some of the more common wakelocks you'll encounter and how to wipe them off of your system. As you encounter ones not on this guide, let me know and I'll add them to the list. For some reference, I typically have light to moderate use of my phone. Before de-wakelocking, I was getting maybe 24 hours on one charge. After de-wakelocking, I was up to 36. Then I bought the Galaxy Nexus Extended battery, and on that battery's third cycle, I went two and a half days on a single charge. Impressive, right?

First off, you have to understand the difference between kernel wakelocks (KWL) and partial wakelocks (PWL). KWLs are wakelocks caused at the kernel or hardware level. Some of these are benign, and some of them are vampires. The only way to solve them is to change how your phone behaves. You'll see examples of that below.

PWLs are wakelocks caused by an app. The solution to these, more often than not, is to freeze (or, in the case of Google apps, uninstall) the offending app. Before you do so, and this is critical, go in via Settings -> Apps, force close the app, and wipe its cache and data. If you don't, you'll almost certainly cause yourself more headaches about 30 seconds after killing the app.

Second, you need to know the tools involved in wakelock hunting. The first is Better Battery Stats. Google "better battery stats XDA" and the dev's post will come up; they give this app away to XDA members. That said, if BBS helps you out, show the dev some love and buy the paid version. It's only $2.89, and the dev will have more than earned it from you by the time you've finished de-wakelocking yourself. The other tools you'll need are (hopefully) this guide, and, of course, the Internet search engine of your choice. I won't cover everything simply because I haven't seen it all.

Two final notes before the guide: do not go wakelock hunting right after installing a new ROM or clean-wipe reinstalling your current one. New ROMs cause the phone to go nuts for a little while, as things decache and little behind-the-scenes tweaks are made. Wait one full battery cycle (100% to 0%, which you're probably doing to calibrate after a clean ROM install anyway) before trying this, or you'll drive yourself nuts. Also, remember that solving one wakelock will often create another, especially early in this process. That's normal and to be expected. God does not hate you, your ROM of choice is not crap, your phone is not glitched, and a clean install while your current ROM is still settling in will only make things worse.

So, how do you track these wakelocks down with BBS? This is a really complicated procedure, so make sure you're with me. First, open BBS. Then, see the dropdown menu at the top that probably says "Other" right now? Tap it, and then you'll see "Kernel Wakelocks" and "Partial Wakelocks" below. That was obscenely difficult, right?

Something to note with BBS: it seems to have a weird "counting" bug. While testing a ROM with BBS, I finally began to question why my PWL time would be considerably higher than my true wakelock time. "True wakelock time," simply put, is time awake - time awake with screen on. If your phone is awake for 45 minutes and the screen is on for 40, you have five minutes of true wakelock time.

So why does BBS say that you have 90 minutes of PWLs when you only have a true wakelock time of 45 minutes? Well, I have two theories to account for that. One, BBS counts wakelocks independently, even if they occur at the same time. For example, if AlarmManager wakes your phone up for two minutes and AudioOut_1 wakes your phone up for 30 seconds during that same time, BBS will register 2m30s of PWLs even though the device was only held awake for 2m total. Now throw 30s of wlan_rx_wake (KWL) in there, and BBS is registering 3 minutes of wakelocks when there were only two. Throw in the notion that you had the screen on for 30 seconds during that time frame, and suddenly you're showing 3m of wakelocks when really there was only 90 seconds of true wakelock time to begin with. BBS has effectively doubled the reported wakelock time, and thrown off your stats. Now, throw in several dozen mini-wakelocks happening at once with the screen on, and you can easily see, say, 6h of PWLs on a 24h run, even if your true wakelock time is only 45m. It seems that Android's battery screen in settings reports this the same way, so my advice to you is to use the true wakelock time when cleaning up and attack KWLs and rogue apps. Some PWLs (AlarmManager) will always be high.

The second theory is that alarms are given a minimum reporting duration; i.e., five seconds per alarm for the sake of demonstration. Now, let's say you have Facebook, Twitter, Google+, LinkedIn, Gmail, Email, News & Weather and BBC News all set to sync every 30 minutes. They'll each set off an alarm at the same time, and suddenly your five seconds of alarm reporting time becomes 40 seconds every half hour. Now, let's say you have 35 alarms going off every half hour (not out of the realm of possibility with Google Services Framework). 35 x 5 = 175 seconds of time awake (in the five seconds per alarm scenario), which is now 350 seconds per hour of wakelocks reported, even if the true wakelock time was only ten seconds. Just something else to keep in mind when attacking PWLs.

One more note on BBS and how it handles KWLs: There is no such thing as a 0-KWL cycle, regardless of what the BBS reporting screen says. If BBS is showing 0 KWLs, tap the menu button, then "More", then "Raw Kernel Wakelocks" to see them. KWLs are required to boot the phone. In essence, the only phone that has 0 KWLs since it was last charged is a phone that hasn't been turned on since its last charge.

There are a couple of other features of BBS that we'll make extensive use of later, but there's one you need to know right now. Tap your phone's menu button to get the BBS menu up. Tap on "More". See the button that says "Set Custom Ref."? You'll need it--you'll need it a lot.

Also, anytime new information about a certain wakelock or wakelock family comes to light elsewhere in the thread, I'll be sharing that information here. If it helps you out, please visit the post that gave us that information and thank the poster. I'll link to each post twice, once in the wakelock description and once at the bottom of the OP, so no excuses about not being able to find it!

Last, but certainly not least: modifying your system in any way, including altering or deleting processes needed to resolve wakelocks, can have unpredictable results. Use caution and make backups of your apps and data, as well as nandroid backups, frequently while finding and eliminating wakelocks. Any modifications you make are done at your own risk, and I assume no responsibility for any damage you may do to your phone while cleaning out wakelocks.

With that said, we'll get started with the KWLs, as they're the trickiest to get rid of. Use the guide below to identify your wakelock, what is causing it, and how to get rid of it.

KWLs in post #2

PWLs in post #3