Saturday, October 23, 2010

How to Improve Battery Life for the Samsung Epic 4G


I recently purchased a new Android phone, the Samsung Epic 4G from Sprint, and I love almost everything about it. However, when I first started using the phone battery life was absolutely horrendous. Even with light usage I was unable to make it through a full day on a single charge and I often had to plug the phone in and charge it several times per day. Now, though, I've done some research and experimentation and am happy to report that not only does my phone make it through the whole day on a single charge, but I also have upwards of 40% battery remaining when I go to bed at night. Now I can let the kids play Angry Birds without fear of my phone dying.
This post will explain how you too can achieve good battery life for your Samsung Epic or other Android phone, and do so without sacrificing the functionality you've come to depend on. (Note: while much of the advice in this blog post is specific to the Samsung Epic, there are some general strategies that apply to any Android phone, so you may find something of interest even if you do not own an Epic.)

A Note About Task Killers and Other Ineffective Practices

If you read a typical forum post from a poor user suffering from battery life problems, the post will usually start out with frustrated pleadings like
I've tried everything! I turn off Wi-Fi, live wallpaper, and use Advanced Task Killer to kill all running apps and services, but nothing works! Help!
Usually the helpful people trying to answer such pleadings offer further advice about turning off GPS and Bluetooth, or reducing screen brightness and timeout. While these techniques can provide incremental improvements to battery life, they do so at the cost of functionality of your phone. I do personally keep Bluetooth disabled when I'm not using it, but always leave wifi and GPS enabled (GPS does not consume any power just by being enabled, only when an app is actively using it). And of course, 4G data connections are known to be a serious battery hog so you probably don't want to leave that on all the time.
Automatic task killers, though, are most likely doing more harm than good and I do not recommend using them. Android manages tasks and processes quite well on its own and automatic task killers interfere with Android's normal mode of operation. Any time a process is killed, it means that it will consume more power next time it needs to be started. Furthermore, many of the tasks that are killed by these auto task killers are important components of your phone's functionality and having them killed in the background is only crippling your phone. I do keep a task killer app on my phone, but only use it when I suspect that an application is misbehaving, and can then individually kill the suspect app. Never let an automatic task killer indiscriminately kill apps or services in the background; doing so is just shooting yourself in the foot.
With that little lecture out of the way, let's look at some practices that are effective at enhancing battery life.

Disable the DRM Service

The Samsung Epic (and presumably other Samsung Galaxy S class phones) come with the Media Hub application for viewing movies and television programs. While this may some day address a gap in the Android platform, as it stands today the Media Hub application offers only a very limited set of programs for rent or purchase. Worse yet, though, the DRM service that comes with the application is a severe power hog. The biggest battery boosting technique you can perform for the Epic is to disable this service altogether.
To disable the DRM service, start from your Android home screen and then go to:
Menu → Settings → Applications → Running services
Once there you should see an entry for the Samsung DRM service. Click on this entry to disable the service, and voilĂ , you instantly have better battery life.
(Note: while you are there, take a look around and see if there are other services running that you do not need. I disabled a couple of other services on my phone, namely the Aloqa widget update service, and the SportsTap widget service since I don't use either of these widgets.)
You may come to find that this service spontaneously springs back to life. You could occasionally repeat this process and stop the service whenever you notice it is running, but why would you want to do that when your phone can do it for you? To permanently kill this service you can use a startup auditing application to make sure that it doesn't reincarnate. I installed the Startup Auditor app, which is a paid application but there are free apps that offer similar functionality. With Startup Auditor I was able make sure that the DRM service did not start automatically at boot time, and furthermore would never spontaneously start itself at all.
Startup Auditor Free
A boot monitor and startup monitor tool which displays a list of applications you may disable. Free version supported by ads.
Startup Auditor
A boot monitor and startup monitor tool which displays a list of applications you may disable. Full version eliminates ads and can disable unlimited.
Note that the Media Hub application probably will not function if you've disabled the DRM service. I do not personally use this app so this is no loss for me.

The Airplane Mode Toggle Trick

One of the biggest drains on a mobile phone battery is when there is a weak signal and the cellular radio frequently scans for a signal. Not much can be done about this when reception truly is weak or there is no signal whatsoever. However, there is an insidious bug in the current Epic firmware that "tricks" the phone into thinking it does not have a signal, and causing the power drain while the phone is scanning for a good signal&emdash;even though it already has one!
You can tell if you are affected by this bug by checking your battery usage:
(Menu) → Settings → About phone → Battery use → Cell standby
On this screen you will see a line that reads "Time without a signal". If you've been in a good coverage area but the percentage on this line is high (say 50% or so) then you are suffering from the bug described previously.
Now, how to fix it? Until Samsung or Sprint provides us with an update, there is a simple trick that works around this bug. All you have to do is put your phone in airplane mode. Hold down the power button on your phone until the pop-up menu appears and then select Airplane mode. Wait approximately 15 seconds, then repeat the process to take your phone back out of airplane mode. For reasons that are not clear to anyone, this airplane mode toggle will prevent the false weak signal bug and the battery drain that goes along with it.
One important thing to note is that this workaround does not survive a power cycle. You will need to repeat this process every time you restart your phone.

Prevent Wi-Fi Sleep

An oft-cited bit of advice is to disable wi-fi to preserve battery power. While this may be true for who are not frequently connected to wi-fi or who spend most of their time on the road, it is actually counter productive if you are usually within range of a wi-fi access point. This is because an active 3G connection consumes more power than an active wi-fi connection. To make matters worse, though, Android's default settings for wi-fi sleep policy are suboptimal. The default setting is to disconnect from wi-fi whenever the screen timeout has been reached, and this in turn causes a 3G data connection to activate. When you turn your phone screen back on your phone will then reconnect to wi-fi. This cycle of wi-fi off → 3G on → 3G off → wi-fi on consumes significant amounts of power. It would be better to leave the wi-fi connection active regardless of the state of the screen. You can do this by changing the wi-fi sleep policy:
(Menu) → Settings → Wireless & networks → Wi-Fi settings → (Menu) → Advanced → Wi-Fi sleep policy
Change the setting to Never. I work from home and am therefore usually in range of my wireless router. I speak from experience when I say that this change can make a significant difference in battery life.
What about the road warriors among you, though? If you are typically out and about and not always connected to a wi-fi access point, changing the global Wi-Fi sleep policy will probably prove detrimental, as your phone will constantly keep scanning for wi-fi connections even when it is otherwise sleeping. Fear not, there is a solution for you too, at least if you don't mind paying for the overall excellent Locale app and installing a couple of very useful plugins. Here is what you will need:
Locale
A powerful app allowing you to create situations and apply settings and other actions when the situation is active
KeepWiFi Locale
A Locale plugin that keeps wi-fi enabled when active
Locale Wifi Connection Plugin
A Locale Plug-in that allows you to set a condition based on what wifi network you're connected to, instead of within range of
With these tools in hand, here is how you can construct a Locale situation that keeps wi-fi enabled when you are connected to a preferred network, but then reverts to the default Android sleep policy when not connected to this network. (First, if you have already followed the above steps to set your sleep policy to Never, you should now set it back to the default value of When screen turns off.)
  1. Open Locale.
  2. Create a Defaults situation if you do not already have one.
  3. Go into the Defaults situation.
  4. Push Add Setting.
  5. Push Keep WiFi.
  6. Leave the value for KeepWiFi Service as Off, then use the back button on your phone to go back.
  7. Save the Defaults situation (Menu → Save).
  8. Push Add Situation.
  9. Enter a name for the situation (I named mine after my home wi-fi network).
  10. Push Add Condition.
  11. Choose Wifi Connection.
  12. Enter the SSID of your wi-fi network, the use the back button on your phone to go back.
  13. Push Add Setting.
  14. Choose Keep WiFi.
  15. This time change the KeepWiFi Service value to On, then use the back button on your phone to go back.
  16. Finally, save the new situation (Menu → Save).
Now whenever you are connected to your preferred wi-fi network, the wi-fi connection will be kept active, preserving power by not switching to 3G data connection. When you are not connected to this network the default wi-fi sleep policy will be used, preserving power by not constantly scanning for wi-fi networks even when the phone screen is off. Locale allows you to have the best of both worlds.

Other Miscellaneous Tips

The following are more well-known tips for extending battery life and since most people are probably already aware of them I don't feel I need to expound upon them very much, but I include them here for reference.
  • Disable 4G data connections whenever not actually in use.
  • Uninstall any apps that you do not use and do not plan to use.
  • Limit the frequency of background sync for things like Facebook, Twitter, email, etc.

Thursday, October 21, 2010

Terabyte Scale for JRuby and Rails

In this, my inaugural blog post, I'm going to explain how you can use Ehcache and Terracotta from your JRuby and Rails applications.  Ehcache is the most popular open source caching solution in the Java landscape and Terracotta's Enterprise Ehcache provides the ultimate in scalability allowing you to store over a terabyte of data in a fully coherent cache.  And now you can use it to provide unprecedented levels of scale to the Rails world.

A Little Bit of History

I work at Terracotta and one of the fun things we do there is to have a semi-annual "dev week", during which the whole distributed team gathers together in our San Francisco office to collaborate in person.  During our last dev week we had a product improvement competition and my entry into the competition was a set of Ruby Gems to provide integration between Ehcache and JRuby/Rails.  To do this, I built on top of the work done by Dylan Stamat, who originally created the JRuby Ehcache integration.  As with so many other open-source projects maintained by people in their free time, this project had been lying dormant for some time while Dylan was busy with other obligations.  I decided to pick up where Dylan left off and as a result of this there is a new set of Ruby Gems for working with Ehcache from JRuby, Rails 2, and Rails 3.

Caching in Rails has traditionally been done using memcached, either directly using the Memcache API or using the Rails caching API with a memcached backend.  While this has worked fairly well for most people, there are some limitations to memcached that have led some to seek alternative solutions.  If you limit yourself to solutions that work with the standard C implementation of Ruby ("Matz's Ruby Implementation") then memcached is likely your best bet.  However, if you are able to use JRuby, a whole new universe of caching solutions emerges, now including the very popular Ehcache.

Using Ehcache with JRuby

Ehcache JRuby integration is provided by the jruby-ehcache gem.  To install it simply execute:

jgem install jruby-ehcache

Ehcache configuration is done with the ehcache.yml YAML file.  The jruby-ehcache gem includes a default ehcache.yml file, but if you would like to customize it you can copy the ehcache.yml file bundled with the gem and place it in your $HOME/lib/config directory and then edit it as you see fit. (This is no longer true. See my blog post Ehcache for JRuby and Rails: Now with more flavor and fewer calories for information about recent changes.)

Now it is time to start using Ehcache.  Let's take a look at a simple example that uses the Ehcache::CacheManager to create a cache, and then puts and gets some date in the cache.

require 'ehcache'

manager = Ehcache::CacheManager.new
cache = manager.cache

cache.put("answer", "42", {:ttl => 120})
answer = cache.get("answer")
puts "Answer: #{answer}"
question = cache.get("question") || 'unknown'
puts "Question: #{question}"

manager.shutdown

Save this code as "jruby-ehcache-demo.rb" and execute it as follows:

jruby -rubygems jruby-ehcache-demo.rb

As you can see from the example, you create a cache using CacheManager.new, and you can control the "time to live" value of a cache entry using the :ttl option in cache.put.  Note that not all of the Ehcache API is currently exposed in the JRuby API, but most of what you need is available and we plan to add a more complete API wrapper in the future.

Using Ehcache in Rails Applications

To use Ehcache from a Rails application you must first install the correct gem for your Rails version.

jgem install jruby-ehcache-rails2 # for Rails 2
jgem install jruby-ehcache-rails3 # for Rails 3

Configuration of Ehcache is still done with the ehcache.yml file, but for Rails applications you must place this file in the config directory of your Rails app.  Note that you must use JRuby to execute your Rails application, as these gems utilize JRuby's Java integration to call the Ehcache API.

With this configuration out of the way, you can now use the Ehcache API directly from your Rails controllers and/or models.  You could of course create a new Cache object everywhere you want to use it, but it is better to create a single instance and make it globally accessible by creating the Cache object in your Rails environment.rb file.  For example, you could add the following lines to config/environment.rb:

require 'ehcache'
EHCACHE = Ehcache::CacheManager.new.cache

By doing so, you make the EHCACHE constant available to all Rails-managed objects in your application.  Using the Ehcache API is now just like the above JRuby example.

If you are using Rails 3 then you have a better option at your disposal: the built-in Rails 3 caching API.  This API provides an abstraction layer for caching underneath which you can plug in any one of a number of caching providers.  The most common provider to date has been the memcached provider, but now you can also use the Ehcache provider.  Switching to the Ehcache provider requires only one line of code in your Rails environment file:

config.cache_store = :ehcache_store

A very simple example of the Rails caching API is as follows:

Rails.cache.write("answer", "42")
Rails.cache.read("answer")  # => '42'

Using this API, your code can be agnostic about the underlying provider, or even switch providers based on the current environment (e.g. memcached in development mode, Ehcache in production).

If you'd like to see a complete example of an Ehcache-enabled Rails application, check out this demo that I wrote to show Ehcache in action:

http://svn.terracotta.org/svn/forge/projects/ehcache-rails-demo/trunk

Conclusion

With Ehcache plugged into your Rails application you have a whole slew of options to address all of those Enterprisey concerns.  With Terracotta's Enterprise Ehcache product you can have the ultimate in scalability by taking advantage of distributed caching or eliminating Java garbage collection pauses with BigMemory.  Plug in a Terracotta Server Array behind your clustered Rails application and you can store hundreds of millions of keys in your cache, and over a terabyte of data.

If you'd like to use Rails for your web app but your boss has concerns about scalability, Ehcache might just be for you.

You can read further documentation on the Ehcache web site, or grab the source from GitHub.