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.
Related
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
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).
I want to monitor battery usage on a very granular level. Like what is the battery usage of each individual activity, or even more granular detail like what is the battery usage in running a for loop of my app. Is there any android app or developer tool using which I can do that on an app whose code I already have?
No, because your phone is not capable of measuring "battery usage on a very granular level".
The closest you will get is with a Qualcomm MDP device and their battery measurement software. Even then, the battery usage by process is somewhat guesswork, as the contributors to power drain (CPU, screen, radios, etc.) are shared by all running processes that use them. A few other off-the-shelf devices may also work as well, though I suspect that the battery measurement software will work a lot better on an MDP, as it has dedicated hardware for this stuff.
Getting finer-grained detail than that will be impractical. At best, with a lot of testing, you might be able to draw some conclusions comparing two algorithms, but for most such algorithms, you would probably be just as well off measuring how much CPU time they took, and extrapolate battery usage from there.
I'm guessing that you want to use algorithms that are very efficient to minimize power usage by your program. If this is true, you are going about it the wrong way. The power consumption by computational algorithms is trivial compared to (a) the usage of external peripherals such as blue tooth and wireless, and (b) preventing the processor from dropping into an idle state. To maximize the power efficiency of your app, you want it to be idle (meaning asleep, not spinning) for as long as possible.
I want an app that drains the battery by using the CPU resources in a controlled fashion. Her, by controlled fashion, what I meant to say is that let's say 'X units/ms' is the maximum amount of the battery drain rate and the 'Y units/ms' is the minimum amount of battery drain rate.
Now, I want to give an integer from 1 to 100 as an input to the program and my app generates a battery drain corresponding to its value. Assume, only this app is running on the system.
So, is there any way to do this?
Due to the differences in hardware and configuration, such an app would likely need to calibrate itself. That is, it should run power-consuming tasks while monitoring the battery, to estimate how much power those power-consuming tasks take.
So, there are two things needed:
Battery Monitoring
Android provides an Intent for getting battery information. There's an SDK tutorial1. Unfortunately, the granularity of the results will be limited, likely to each percentage point. This means you need longer calibration tests and your results (and thus drain) will be of limited accuracy.
Power Consuming Tasks
The CPU is generally not the biggest power consumer in a mobile device. Whether the LCD is on might affect the drain more than tying up all of your CPU cores. Radio hardware (3G/GPS/WIFI) can also produce a higher drain than the CPU. An LCD at max brightness will drain more power than an LCD on at min brightness. An AMOLED would drain less power than the LCD.
The performance of different tasks will vary greatly depending on the hardware being used. This is what necessitates calibration.
I'm currently developing with Android SDK, and because I'm building an application based on camera, I cannot use the emulator to test it.
This means that the following scenario happens quite frequently
I connect my phone to USB the battery is 50%
I disconnect it before it is fully charged, e.g. 80%
I have to leave, and I want to take my phone with me
I come back and connect again and so on...
I read that Lithium-ion batteries don't suffer the memory effect, but, on the other hand, I also read that one should wait until the battery is fully charged before disconnecting it from the USB.
So, I'm wondering if the way I'm using the phone now for debugging may damage or reduce the life of my battery
Any advice is appreciated
First of all, I'm not an expert in this subject.
Lithium-ion battery, as you say, does not suffer the charge memory effect but they have the "digital memory" (affects the accuracy % remaining battery) as reflected in this article (so you have to fully-discharge - fully-charge periodically the battery to avoid this):
Source: http://www.techrepublic.com/blog/five-tips/five-tips-for-extending-lithium-ion-battery-life/289
More info about it:
"Partial discharge on Li-ion is fine; there is no memory and the battery does not need periodic full discharge cycles other than to calibrate the fuel gauge on a smart battery"
So partial discharge is not a problem. The worst condition is keeping a fully charged battery at elevated temperatures
Source: http://batteryuniversity.com/learn/article/how_to_prolong_lithium_based_batteries
About the need to fully-charge in Lithium-ion batteries, I did find a source about it, but in Apple support you can find this:
"You can disconnect and use your iPod before the battery is fully charged. You don't have to wait until the battery is completely empty before charging it again. This is because lithium-ion batteries, such as those used in iPod, have none of the memory effects seen in nickel-based rechargeable batteries"
Source: http://support.apple.com/kb/ht1384
Conclusion: Your partial charges/discharges won't be noticeable. If you want to extend the battery life, follow the suggestions you can find in the first two articles (avoid hot, don't let fully discharge... etc).