[Galaxy Note 4 Original Android Development] gekkehenkie11:
As battery drains are a returning complaint, in pretty much every single forum, regarding stock ROMs and custom ROMs, I thought I'd do a quick crash course so people will understand how to find out what's draining their battery.

So, you guys might have heard the term 'wakelock' but might not know what it exactly is. A wakelock isn't anything bad in itself, quite the contrary. A wakelock is an Android feature for programmers. It allows programmers to ensure that the phone is not going to 'deep sleep' when the programmer needs the CPU to be running. A phone in deep sleep has an inactive CPU, which means it's doing NOTHING. So if your phone is in deep sleep, the CPU can't consume any battery cause it's simply not active. However there are a lot of situations where you dont want your phone to sleep. For example, when you are streaming bluetooth to your radio: while your screen might be off, you'll NEED the cpu to be active, otherwise your phone simply can't stream bluetooth (if your phone would go into deep sleep during bluetooth streaming, the music would stop). Or if you attach your phone to the USB port: if your phone would actually go into deep sleep while attached, you wouldnt be able to browse the SD card contents. Or you might want an app to keep the display awake, like if you are in your car and want to use the GPS to see where you are going. All this is being achieved with wakelocks. For example, once you connect your phone to an USB port, the kernel issues an ' USB wakelock'. This means the CPU will be running at least 300 mhz until the wakelock is released, which the kernel does once it sees that the USB cable is detached.

So, resuming, wakelocks are being used to ensure the CPU is not being turned off, because a process that is running needs the CPU to be alive.

However, this can go wrong. For example let's say the kernel code is wrong and it doesnt actually release the wakelock when you disconnect your USB cable. That would mean the wakelock would keep running indefinitely and that means your CPU will keep running at at least 300 mhz, which will drain your battery. Or, let's say your navigation app crashes and thus can't issue the 'release wakelock' command, again, that would result in your CPU being awake forever, until you reboot.

Your phone keeps track of the amount of wakelocks being issued. It counts how many time an app issue's a wakelock and for how long. All this info can be retrieved with an app like betterbatterystats. So let me explain how to use that app and what to look for.

1) Kernel wakelocks. These are the wakelocks as issued by the kernel. Above I gave the example of your USB keeping the phone alive. One wakelock you will see in the top will probably be "powermanagerService.display", this is simply the wakelock that is issued while your screen is on. Then there's wlan_rx_wake, that's your network adapter waking up your phone and/or keeping it awake. That's a whole topic in itself, I wrote a guide on it here: http://xdaforums.com/showpost.php?p=58987722&postcount=99. Another one that you will see a lot is "Powermanagerservice.wakelocks". This is not really a wakelock in itself, but it's the sum of the user space wakelocks:

2) Partial wakelocks (= user space wakelocks). These are wakelocks being issued by apps and services (so not the kernel).

Then there's the tab 'other', which shows you how long your phone is awake and how long it has been in deep sleep. So in short, this info should be enough to get you go hunting for problems. For example if you see some app is keeping your phone awake, you might want to uninstall that app or disable some buggy service.

So, in short, if you notice that your battery is draining, go check with betterbatterystats if your phone is being kept awake for some reason and then see what's keeping your phone awake !