Battery-Safe Coding

Battery-safe coding

I'll assume you mean your application. In my experience, the major consumers of energy are, where #1 is most important:

  1. CPU usage
  2. 4G
  3. WiFi
  4. Bluetooth
  5. Memory

Whether 4G or WiFi is worse depends upon your usage, e.g. whether you are talking with a poor signal over your cellular network or are streaming video over your WiFi. GPS depends upon if you are using it. If you are getting turn by turn directions, it'll turn your phone into a little heater and drain your battery very quickly.

Minimizing 4G, WiFi, and Bluetooth usage is pretty straight forward. I not sure it's possible to reduce the energy used by memory in any practical way.

CPU usage is the greatest potential energy hog because it can continuously suck down electrons all of the time. Thankfully, modern processors shut down when doing nothing, i.e. idling. This is called dropping into an idle / C-state. As you can guess, a cell phone is doing nothing pretty much most of the time.

There are ways you can write your program to minimize CPU energy usage. Actually, a better way of saying this is that there are ways you can defeat these energy saving features by writing your program wrong. If the CPU goes to sleep to minimize power then waking it up increases energy usage. Another factor to consider is how long the CPU is asleep. The longer you can leave the processor idle, the deeper a sleep state it can enter, and deeper sleep states use less power.

So what do you need to do to minimize your CPU usage? You want to use the CPU less, or to say it another way, have your program get done whatever it’s doing faster. Also, increase the length of time that your program is idle.

Now let's take a look at some concrete things you can do:

  1. Have your program do whatever it's doing as fast as possible. Do this by finding the fastest algorithm and implementing it in the most efficient way possible. To say it another way, optimize.

  2. Minimize checking on events. The more you check on whether an event occurred, the more you wake up the processor, the less likely it can drop into a really deep sleep state. Do this by figuring out the maximum interval that you have to check for some event while still maintaining performance.

What is Power Save Mode in IntelliJ IDEA and other Jetbrains IDEs?

It's a setting to stop the IDE from automatically performing the full range of battery-hungry code inspections in the background as you type.

You should find that with powersave turned on, syntax errors will still get highlighted, but iffy code constructs e.g. missing docblocks, assignment inside conditional statements etc will not (assuming you have those inspections enabled). This means that you can save battery power when using your laptop and choose to run the inspections only occasionally by turning power save mode off for a minute or two, or using Code->Inspect code... to see what needs attention.

See Settings->Inspections to find out whether you can live without the specific inspections, which will depend on what language you are using. You can choose to run them individually on demand if you like using Code->Run inspection by name... if there are specific ones you need. More info here.

Naturally, this also applies to RubyMine, PHPStorm, PyCharm, WebStorm & AppCode as well.

Android - How to slow down battery Drain

There is no exact solution to minimize battery usage - it depends on your code, and it's all about minimizing CPU & Network usage. Basically you already have powerful Profiler in your Android Studio. It shows CPU, RAM, network & battery usage on dashboards and you can find most heavy part of your code using it. More you optimize your code, more effectively app will use battery.

Sample Image

Sample Image

Sample Image

How to check if Android Lollipop battery saver is on

Take a look at power manager

It has a method isPowerSaveMode() that will tell you if battery saver is on

Example code:

PowerManager powerManager = (PowerManager)
getActivity().getSystemService(Context.POWER_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP
&& powerManager.isPowerSaveMode()) {
// Animations are disabled in power save mode, so just show a toast instead.
Toast.makeText(mContext, getString(R.string.toast), Toast.LENGTH_SHORT).show();
}


Related Topics



Leave a reply



Submit