How to check processes and services consuming lots of battery - android

I have created an app which consuming 48 percent of battery in some device which is highest in power management task, but in some device its 5-6 %, i am running a service in background all the time which fetches the latitude and longitude and send it on server if user is logged in. but i have checked also check by logging out from app it still consumes 48 percent.
to fetch latitude and longitude o am using fused location api.
So please tell me how to resolve this issue of battery consumption in some phone and how to check which process and service is draining battery.

I agree with Lonni that the issue is the lat/long fetch. Given the scale of the power consumed, 48%, it's unlikely to be the GPS circuitry itself. I figure it's the CPU. Mobile processors are very power efficient unless they are kept active. Let me explain. An active proessor is the most power hungry device on a mobile device. So how can a processor be the biggest power hog while also being "power efficient"? By the processor being "power efficient", I mean that when the processor isn't doing anything, it goes into a very efficient lower power state where its power consumption can be over an order of magnitude less than when in the active state.
My guess is that you are keeping the processor in an active state. What you want is to keep the processor as idle as possible.
Here are my recommendations:
Use the longest interval possible between lat/long fetch. A common mistake is that more checks mean better response, but unnecessary checks generally don't improve response and just keep the processor active and consuming power for no good reason.
Never poll! Polling keeps the processor active which consumes power for no good reason. Put the processor to sleep.
Use interrupts to processes events. System libraries, such as sleep(), put the processor in an inactive state, and uses an interrupt to wake the processor back up.
Don't write your own routine if a library routine already exists. The OS/library writers are very aware of the importance of power efficiency and have written their code to be as efficient as possible.
Make your code run as fast as possible. Fast means more idle time, which drops the processor into a more efficient power state. For example, if you can get along with a lat/long check every 60 sec, and you can get your processing done in 10 sec instead of 30, you have 50 sec of idle vs 30 sec.
Use a good optimizing compiler and good optimized mobile libraries if possible. Good compilers create more efficient, faster running code. Good libraries not only run faster, they use power efficient techniques.
Use a thread pool if you are using a lot of threads. Creating and tearing down threads is costly.
Make sure you check your API specs on your devices. I can imagine that some OSs/drivers, e.g. GPS, will require the device to be explicitly turned off, while others will do so implicitly.
Here's some more information: Battery-safe coding
Aside: I already see the thought bubble: "Why are some devices using less power than others?" Some libraries are really smart, and anticipate bad programming practices and do workarounds. Others are dumb. The same with the OS, system libraries and thread scheduling.

My best guess is that the constant fetching of long and lat is draining the battery. If i remember correctly what I did a few years ago with positioning that was the case.
I would say that you should try and see the refresh-rate you want to have of your coordinates. Maybe it doesn't need to be refreshed more than 5 times per minutes. In that case you would save a lot of calls to the fetching of the coordinates and surely save your battery.
As for why it is different on some devices, I'm afraid I have no idea. Maybe the version of android used?
Edit: I don't know if Eclipse can do that and I don't think it can.
However, you may want to check this paper: http://www.usenix.org/event/usenix10/tech/full_papers/Carroll.pdf

Related

Android Power consumption of a method

For a research project I need to measure the power consumption of some functionality. So I would like to measure the power consumption of a method. For example: method computeSomething() needed x microampere-hours. Is this possible? If yes, how?
I tried to measure the remaining battery capacity in microampere-hours and the remaining energy in nanowatt-hours before and after execution with the BatteryManager. But this isn't accurate enough. Is that even possible to do it?
this is what i tried:
mBatteryManager.getLongProperty(BatteryManager.BATTERY_PROPERTY_ENERGY_COUNTER);
EDIT:
for sure it's the emulator which don't consume energy and that's why I get a consumption of 0. Is it possible to simulate power consumption with the android emulator or do I need a real phone?
Here's how I would measure the energy used (because that's what you want and not power).
Shutdown (or don't use) everything I can, including network, cell, USB, etc.
Disable any power saving features on the phone
Log any data you get on the phone's file system (vs through the USB or wireless)
Wrap the program in a large loop that makes its runtime at least a few seconds, perhaps a few minutes
Run the test program you just made many times (>=16 times for statistical significance)
Measure the phone at rest over the same amount of time and collect the energy used.
ANALYSIS: (and you need to do this)
Check on the statistical deviation (std dev) of both your function run and the "at rest" run. If the std dev is large, something else is going on that is using energy.
Find out the resolution of BatteryManager. Just because the field is labeled in nWatts doesn't mean the hardware measures it at that resolution (and it probably doesn't). The people who develop a data structure want that structure to be useful on a lot of hardware and for the foreseeable future. Thus they make the field measure in something very small (e.g. nWatts) even if most hardware can only measure at best, say, in tenth of watts.
If possible, use a hardware interposer between the battery and the phone to measure energy/power directly. This gets around the uncertainty in the implementation of BatteryManager.
Just to be safe, you might also find out how long of an integration window the BatteryManager uses for measuring power. These measurements usually involve a moving window for computing averages.
An emulator isn't going to give you any useful information. Emulators test functionality and little else. Even a rough measure of performance is very iffy.
I've developed a similar App before but is used to evaluate the power consumption of each Application. But as far as I know, the Google's battery static API evaluate power consumption according to each UID, both hardware and software. I think you can find more information by looking into the source code of Android, especially for BatterySipper.java and BatteryStatsHelper.java.
For emulator, Android system uses power_profile.xml as reference to calculate the power consumption. This file is usually modified by manufacturer. Maybe it's not included in emulator.

can I get the battery usage accurate enough to 0.1%

I want to calculate the power consumption of my app when it finished running on my android phone.
I know the api of BaterryManager.Level but it can only feed me the 1% usage of my battery.I must run my app as long as hours to get the battery level change in a obvious level.Can I get the 0.1% level of battery usage so I can calculate the power consumption accurate?
I'm not sure that you can get any useful information at that level. There are other activities going on in your phone that also consume power, even if nothing else is running, such as OS services. 0.1% is probably in the noise. Also, since your app consumes so little power, it is probably spending a lot of time in an idle state (blocked or sleeping). I suspect that even if you were to run it for hours, you are not measuring your app so much as background activity.
Here's my suggestion:
Find the part of your app that is likely to consume power, either directly (processing intensive) or indirectly (e.g. Wifi, GPS).
Compile and run that core part in its own test program.
Run as many of those test programs in parallel that you can.
Make sure you also get a power consumption baseline, meaning find out the power consumption of the phone when you are not running anything.
By the way, given that your app is already using little power, you many not have to worry about the power consumption.

How to measure battery usage for specific alarm manager?

I have an alarm manager and I check with my server every 30 minutes for new data, i want to measure battery usage of this specific task or another question how to measure battery usage of running each service and class in my application?
Whether checking every 30 minutes or every 2 hours, there shouldn't be a difference. (I'm assuming that the processing you are doing when you check is small.) Checking too often is a problem with regards to battery drain but only in situations where the interval between checks is <1 sec.
So if your question is, "What is the best interval to use?" then 30 minutes shouldn't be a problem.
If your question is, "I'm having a problem with battery drain and need to find out where it is," then this becomes a profiling question. And it gets much more complicated, especially when you are trying to view things from a class or even a light weight process (e.g. a thread) standpoint. You can do profiling at a class or thread level but it becomes very complicated unless you have hardware with special registers for recording power, current or voltage information.
If you only need to profile at a process level, there are plenty of good tools. A simple web search will suffice. Power consumption is closely tied to CPU usage. If you reduce CPU usage, say by making your program faster, you will likely also reduce energy consumption (i.e. power).

How is it possible that Google Fit app measures number of steps all the time without draining battery?

The Google Fit app, when installed, measures the duration you are walking or running, and also the number of steps all the time. However, strangely, using it does not seem to drain the battery. Other apps like Moves which seems to record number of steps pretty accurately declares that it uses a lot of power because of it constantly monitoring the GPS and the accelerometer.
I imagine several possibilities:
Wakes up the phone every minute or so, then analyses the sensors for a few seconds and then sleeps again. However it seems that the records are pretty accurate to the minute, so the waking up must be frequent.
Actually turns on the accelerometer all the time, and analyzes it only after the accelerometer measurement data buffer is full. However I think the accelerometer has a small buffer to store the latest measurements.
Use GPS to estimate the number of steps instead of actually counting it. However this should not be the case, since it works even indoors.
The app still feels magical. Counting steps the whole time without perceptible battery drain.
Thanks for asking this question!
Battery is one of our top most concerns and we work hard to optimize Google Fit's battery usage and provide a magical experience.
Google Fit uses a mix of sensors(Accelerometer, Step counter, Significant Motion counter), Machine Learning and heuristics to get the data right. Our algorithm is pretty similar to your 1st option plus a little bit of magic.
We periodically poll accelerometer and use Machine Learning and heuristics to correctly identify the activity and duration.
For devices with hardware step counters, we use these step counters to monitor step counts. For older devices, we use the activity detected to predict the right number of steps.
Our algorithms merge these activities, steps and sometimes location to correlate and further increase accuracy.
We do not poll GPS to estimate steps or detect activities.
-- Engineer on Google Fit Team.
On some very recent phones like the Nexus 5 (released in late 2013 with Android 4.4 KitKat), there is a dedicated low-power CPU core that can serve as a pedometer. Since this core consumes very little power and can compute steps by itself without the need for the entire CPU or the GPS, overall battery use is reduced greatly. On the recent iPhones, there is a similar microcontroller called the M7 coprocessor in the iPhone 5s and the M8 in the iPhone 6.
More information here:
https://developer.android.com/about/versions/kitkat.html
http://nexus5.wonderhowto.com/how-to/your-nexus-5-has-real-pedometer-built-in-heres-you-use-0151267/
http://www.androidbeat.com/2014/01/pedometer-nexus5-hardware-count-steps-walked/
having a 3 year old HTC OneX I can say that THERE IS NO DEDICATED HARDWARE, Google Fit just uses standard sensors in a very clever way. I come from Runtastic Pedometer: there is a clear battery consume when in use, it would be impossible to keep it on all the time as it needs the full accelerometer power. On the other side, if you stand still and shake the phone Runtastic will count the shakes, while Google Fit apparently does nothing... Still it works perfectly when you actually walk or run. Magic.
Google fit try to learn use pedo step pattern and try to create its own personal walking patterns and its clusters. This eliminates the need of having huge mathematics calculations on receiving sensor data every time. This makes Google fit more power efficient compared other software pedo apps. Having said that, there is compromise on accuracy factors here. Between power-accuracy trade off, google seems to be more aligned towards power factor here.
At this moment the most power efficient detection happens Samsung flagship & its other high end models. Thanks to Samsung's dedicated hardware chip! No matter how power efficient your software pedo algorithm be but its hard to beat dedicated hardware unit advantage. I also heard about Google's bringing dedicated hardware unit for Ped upcoming nexus devices.
It would seem like the solution would be device dependent, with devices where a co-motion processor or "wimpier" core is available for low power operations, that it would default to this once the buffer is full or similar condition. With devices where a low-power core is not available, it seems like waking the device could trigger a JIT operation that would/should finish by the time the app is called.
While the Nexus 5 does have a dedicated "low-power" pedometer built in. It isn't as "low power" as you might think.
My Nexus 5 battery life was decreased by about 25% when I had Google Fit Activity Detection switched on.
Also, the pedometer doesn't show up in the battery usage stats. Presumably, because it is a hardware thing.
I don't know for the other phones out there, but Google Fit was really draining my battery life on my Nexus 5. Disabling it definitely improved my battery life.

GPS position logging battery life

What is the best way to log GPS position (or data, to analyze) that wouldn't consume whole battery in few hours on Android? Is there some special way? Some of the tracking software available on market claim to be optimized for low battery usage.
No matter what exact approach to the code you use (e.g. service, some library, or code your self, etc, etc)... it will ALWAYS narrow down to one question:
How much resolution and how often updates are you requesting?
That's because no matter how it's coded, it will always relies on the same sensors (the GPS, wifi n tower triangulation) that is attached to the same physical hardware, and same battery.
So, general tips for better battery life is to get positions less often, to be ok with less accurate positioning, etc.
The Google Location Services (available on any device that uses Google Play) is a great time saver and it's also a very well optimized piece of code. If you want some example on how to use it feel free to check this app I did in a boring Sunday afternoon:
https://github.com/budius/photogpstag
https://play.google.com/store/apps/details?id=com.budius.photogpstag
In that I optimized for a person walking and taking photos, meaning every 3 or 5 minutes would be a good enough position and have a good lasting battery.
You can grab the source code and change the request parameters to higher accuracy/more often updates and see that the battery drain will increase.

Categories

Resources