Extending battery life in your developer laptop
Nowadays, laptops are the number one choice for the developers and very few uses desktops at home or at the office. There is no doubt that they provide the mobility and the flexibility that you can carry around all your development work, notes and other files where ever you go. Moreover, the laptops now come with equivalent and better configuration than the desktops. This has also helped in popularizing the remote workplaces and working from a different environment ( read “Coffee shops”). But with all these, one of the major issues faced by the developers is the lack of battery backup available in the machines. In this post, we are going to see how to extend the battery life for the development machine.
Why does a developer laptop deliver less battery life?
Most of the laptops boast power-efficient processors and advertise a minimum of 5 to 6 hours of battery backup. But when we ( “developer” ) starts working on it, we will notice that we are not even getting 2 hours of battery life out of this. Following are the major reasons why there is a surprising difference.
- The advertised battery life is just under standard conditions ( don’t miss the “*” symbol they put with battery life ). This includes some standard usage like browsing, word processing, music etc
- The workload and behaviour of a developer system are totally different. We would be running IDE’s ( That needs power even for smart autocompletion), VM’s, deployed services locally, and different infrastructure services running ( Database, Queue, Cache, Docker )
- Connected Bluetooth devices
- Unknown services running in the system which were previously started for a different project still eating up CPU.
- finally, the inevitable compiler and deployment process which is very CPU intensive.
How to improve the battery life of a development system.
Let’s see what are the things that we can control to improve battery life. Most of these things are already tried out on my system and has found some substantial difference in the battery life before and after. Also please note that my system is a Thinkpad t470 running Ubuntu 16.04. Some of the commands and tools would be Linux specific, but you would be able to find alternatives for your respective operating system ( Windows or Mac ).
For the eager ones, below are quick tips you can follow:
- Monitor and analyze the usage
- Disable the docker service and bring down the docker network interfaces.
- Turn off the Bluetooth radio device when not in use.
- Use VM’s for running services that are not frequently used.
- Manage the number of tabs opened in browsers.
- Disable the graphics card if not used to gaming or scientific modelling.
- Quit the programs not used frequently
- Reduce the brightness in low lit conditions
- Restart the system once in a while to get rid of any residual processes
- Bonus tip for full day power during meetings.
Yes, first and foremost, you need to know what is happening in the system to identify what is hogging the system. We are developers and we need to think and act like one. So the first step is to make sure that we have some resource monitoring in our system.
- If you are using a Linux based system, you may run top / htop for getting information on the current process. Similarly, Windows users can use the Task Manager.
- Use some indicators in the panel/dash. These are readily avaialble in Mac and Ubuntu. I use a gnome shell extension ( system-monitor) to display the CPU, Mem etc
Here the blue indicates the CPU usage and I have trained my eyes to keep a watch on these and if there is a spike in the normal idle state, I would do a check in the top command to see what is eating up my processing power.
- powertop: This is a very handy tool available in Linux which show the process / hardware device and their actual battery usage.
We can see that in the above screenshot the maximum battery usage is by the Wifi adapter ( which is normal ). If I had some other battery hogging applications this would show those and also the “Tunables” tab gives me some configuration that I can try to fine tune the usage.
2. Docker service
There is no doubt that Docker has revolutionized the way applications are deployed. Its very common that you would be running a few containers now or then in your system to support some of the services. This requires you to have the docker service running. Now the problem with docker is that, the containers may be only required during the respective service lifecycle and may be destroyed, but the docker service will be running in the system and can lead to some battery drain.
To understand how this happens, I ran the docker service ( without any containers ) and then ran the powertop. Following is the screenshot of the result
You can see that there are two entries ( docker0, docker_gwbridge). These are some virtual network interfaces created by the docker service to make the container communication possible with the host and among themselves. But you can see that these are discharging a Watt of battery each. That is some pretty high usage for a service we are not frequently using. So we can try the following:
- Stop the docker service when not used. But this may not remove the interfaces from consuming power.
- Down the interfaces
$> sudo ifconfig docker0 down
$> sudo ifconfig docker_gwbridge down
3. Bluetooth Radio
The Bluetooth is similar to Wifi and is a hardware interface that will consume energy when turned on ( even if no devices are connected ). If you are someone who cannot live without music streaming to your Bluetooth headset, you may skip this.
For others, it’s better to turn off the Bluetooth interface when not in use. This can be done by going to the Bluetooth settings in the respective Operating system. Consider the screenshot below from the powertop command when Bluetooth is running:
4. Use VM’s for running services
Most of the modern system architectures would contain a datastore ( Mongo, Couch, MySQL etch), Cache ( Hazelcast, Redis, Gemfire), Queue ( RabbitMQ, Kafka ) etc. It’s very common to have all these services running in the local development system. The problem with this is that these are system-wide services when installed and would keep running in the background and also start automatically during the system startup. And the worst part is that, with microservices, each service may have its own choice of support services and this would keep the system really busy.
The easiest way I have found is to install VirtualBox and create a VM ( preferably server edition of any Linux flavor ) and install the services inside the VM. Following are the benefits you would have out of this method.
- VM’s in Virtualbox are easy to create and they can be hibernated to disk and started up instantly ( No need to shutdown and restart )
- You may organize the services into VM’s so that you can start a particular VM containing the stack for a particular service you are working on.
- The VM may take more memory than the individual services running in the local system, but once you are done with your task, you may turn them off and there is no service running in the background.
- You can configure the VM’s to have static addresses and can access them using same IP irrespective of you network ( like you are using localhost )
I am following a similar setup where I have a Ubuntu 16.04 VM containing Hazelcast, RMQ, Mysql , Docker, ELK stack installed. I start the VM when I am working on the service and will stop the instance when I am done.
If you are interested in the VirtualBox setup with a static address ( with internet connectivity across networks ), click here to see the post.
5. Keep a tab on the “tabs”
You can see here that, ironically, I have WordPress taking up high CPU while creating this post.
It’s a best practice to close the tabs once we are done with the “research” for a particular problem. Consider that the “Close all tabs to the right” is invented for us. Use it and make sure that there are no unnecessary tabs running. In chrome, you could use “Shift + Esc” key to bring up the task monitor and see which tabs are resource intensive.
6. Disable the graphics card ( If applicable )
Most of the modern laptops are equipped with a Graphics card and they are a real battery hog. The day-to-day tasks never require a graphics card and everything can run smoothly over an integrated graphics card. Unless you are an avid gamer or someone whose work is related to mathematical or Machine learning models, you may turn off the graphics card.
I have Nvidia graphics card and the battery life was half when I had the graphics card turned on. I switched to the Intel Integrated graphics and was able to extract extra juice. The graphics card drivers would be having an option to switch the graphics mode.
7. Quit the programs that we don’t use
This is something similar to the case of the Chrome tabs, we will be having many windows open during different sessions and it wouldn’t hurt to close them off when they are no longer needed.
For eg: You may have referred a document on a change request. But it will be there open in the system even after the change has gone into production. Though it may seem harmless, any program requires some CPU and Memory to survive.
8. Reduce the brightness
Most of the people tend to use the default brightness of the system and is unaware that backlight of the display is the top energy consumer and that the brightness is directly proportional to the energy consumed. If you are working in a low lit condition, it would be better to reduce the brightness which can be easy on eyes as well on the power consumption.
Please note that using a dark theme does not help in reducing power consumption. They are only applicable for AMOLED screens in mobile devices.
9. Restart the system once in a while
Modern systems are very efficient in hibernating and waking up and this has left us with a habit of keeping everything running. For many people, restarting a development system is like moving to a different house. It requires a lot of time and effort to keep the system back to the same state. If we follow some of the steps above ( like having VM’s for services and managing a minimum discipline with open windows ), this wouldn’t feel very irritating.
How much ever the modern OS claim to be process optimized, there are residual processes and locks on programs in the background. They will only be cleared once we restart the system. I would recommend a fresh start at least at the start of the week.
10. Bonus tip for a day-long battery ( Business / Client meetings )
There are situations wherein you are on the move and is attending some meetings where you would be only taking notes or presenting some design. If my day does not demand CPU intensive task, one thing I do is to have the system fully charged on the day before and then restart and keep fresh with only a text editor or excel open. This would allow me to have the system run efficiently on battery power for almost the entire day without any need to run searching for a power outlet.
We have seen some of the tips that can be applied to any of the operating systems and environments. These steps would definitely help in increasing the battery backup and moreover, a less cluttered system would give a better experience as well.
Let me know your thoughts and queries on the same.