wake locks cause this

wake locks cause this

Wake locks are a mechanism the developer has to make sure the screen is on and/or the CPU is ramped-up.  The developer requests the wake lock, then acquires it to turn it on, and releases it to turn it off.

This sort of thing can become rather complicated in large apps with multiple threads and objects which may be garbage collected before they can release their wake lock, which is why so many apps in the market have problems with battery use due to these “leaked” wake locks.

Right, backing up a bit, wake locks are equated with battery use because of that ramped-up CPU.   Mobile CPUs are designed to go into power-sipping modes when the screen is off, which is ramping-down.  A wake lock held longer than necessary causes the CPU to wake up to full speed and power consumption,  leaving it there.

Previously, I had taken a conservative approach to the use of wake locks in Wifi Fixer.  Some operations were critical, I needed to make sure the CPU and radio were available, so with a bit of experimentation, I was able to acquire wake locks for just long enough, and release them with no “leaks”.

This resulted in moderate wake lock usage, which was acceptable.  But recently the battery use of apps has come under a great deal of scrutiny by users, in part thanks to an improved battery history in jelly bean, and terrific tools like better battery stats.   These tools have allowed me to look at Wifi Fixer and think maybe I can do better with wake locks, and thus battery use.

The new code works like this: it tries to do a network thing, and if it fails, then the wake lock is acquired, the network thing is tried again, and the wake lock is released.     But that’s not the only way wake locks can be conserved.

Since fairly early, I’ve had an alarm which made sure Wifi Fixer’s important wifi monitor service was running when it was supposed to, i.e. not killed by a task killer or by the system due to low memory.    The consequence of this is wake locks where I did not explicitly acquire any.    I am still working on this problem, but I expect to have it solved soon.

In any case, expect Wifi Fixer 1.0 to be easier on your battery.