How to measure battery usage for specific alarm manager? - android

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).

Related

A large number unused wakelocks and performance

There is an application that every day creates about 100-200 new wakelocks for android. each wakelock is used once and after using it is no longer needed, but remains in the system. Thus there are a lot of unused wakelocks in the system. How do you think this is correct? or is it bad for system performance?
if you are wondering what kind of application it is and how this is possible, then see the link:
https://github.com/iNPUTmice/Conversations/issues/4012
Is it bad for system performance?
In terms of compute, probably no real effect since the app completes its work and yields. Especially if the app is in foreground during this time, and the work is relatively quick (roughly <1 min or so)
In terms of battery use, questionable. If its the highest wakelock level (screen + CPU), when the user turns off the screen, they are released. If its just the CPU wakelock, then they'll hang around and keep draining battery. Though later android versions and OEM optimizations may just remove wake locks after some time.
Is it bad practice? YES!
This undoubtedly drain power more than it should and gives the system the impression the app needs to keep either the CPU running for longer than required influencing OS incorrectly.
Also note that this is a generic question rather than a specific issue, so you may get a better answer from https://softwareengineering.stackexchange.com/

How to check processes and services consuming lots of battery

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

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.

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.

Android AlarmManager heavy battery drainer?

i just started working on android about 4 months back.
i created a project that uses AlarmManager, the flow is like this:
AlarmManager->service->wifi not available->AlarmManager continues->service.. and so on.
this alarm may be scheduled for days, hopefully if the device is not rebooted.
I required help in the Battery drainage part of the project. How may i know the rate at which my AlarmManager Project is draining the battery. I fear that if it draining a lot of battery, my app wont be useful.
There are several things which may consume a lot of battery power, such as the display screen, access and Wifi scanning, Bluetooth, GPS, live wallpapers and so on. Roaming around on forums and stuff, I see a lot of people complaining about short battery time, and it is actually easy to understand them, but if they take a closer look at all the resources you mobile is using constantly, they wouldn't be surprised at all. Personally, I've resigned to charge my phone twice a day, when not working with it.
Nevertheless, I have a couple suggestions for you:
I am not actually that familiar with how AlarmManager works, but I suppose you could decrease the frequency of scheduled alarms to save battery time. There are a lot of applications which start automatically, not sure they use this method, though!
Use JuiceDeffender (I think) and JuicePlotter. These are apps which can monitor and plot the battery consumption in time.
Make tests: In one day schedule the alarms, in another day, don't! Then analyse the battery level.
Perhaps the folks in android.stackexchange.com have a deeper knowledge on Android power consumption. Ask them out!
Cheers!

Categories

Resources