Thursday, January 20, 2011

Sysctl tweaking for faster, longer lasting Android

If your ROM supports sysctl, you can do lots of cool things.  (Well even if your ROM doesn't you can as long as you're rooted with busybox, but it takes a bit more work).

In this post, let's focus on three settings: vm.dirty_ratio, vm.dirty_backgroud_ratio, and vm.vfs_cache_pressure.

vm.dirty_ratio and vm.dirty_background_ratio control how often kernel writes data to "disk" (well in our case the microSD card).  When your apps write data to disk, Linux actually doesn't write the data out to disk right away, it actually writes the stuff to system memory and the kernel handles when/how the data is actually going to be flushed to disk.

I dunno - how much difference can it actually make?  We're not talking about traditional hard drives with spindles, we're talking about solid state stuff.  I suppose there could be a slight savings in battery life and increase in performance if you delay the data flush as much as you can.

I ended up with:
vm.dirty_ratio = 90
vm.dirty_background_ratio = 70
Now vm.vfs_cache_pressure is much more interesting. File system cache (dentry/inode) is really more important than the block cache above, so we really want the kernel to use up much of the RAM for them.  The default value is 100, and what you want to do is lower that value to tell the kernel to favor the file system cache and not drop them aggressively.

You can also take it a step further and set it to 1 (lowest possible without being dangerous).  With the value of 1 the kernel will drop cache only when it's completely out of memory.
vm.vfs_cache_pressure = 1
The problem is if you lower the value too much, after a day or two your device may start getting sluggish because the amount of RAM available to the applications continue to shrink to the point where they are starved for memory.  So I came up with a solution to manually drop caches nightly!
echo 3 > /proc/sys/vm/drop_caches
# tells kernel to drop all file system caches
And what I do to ensure that the caches are dropped nightly is to run the following sl4a scripts via tasker.  Cron would be better but busybox on android doesn't seem to support it yet.

(It also reverts the dirty settings because I don't care if my phone writes to disk aggresively while i'm sleeping and it's being charged.)
su -c 'echo 3 > /proc/sys/vm/drop_caches;\
sysctl -w vm.dirty_background_ratio=3;\
sysctl -w vm.dirty_ratio=15'
And the following script at wakeup time:
su -c 'echo 3 > /proc/sys/vm/drop_caches;\
sysctl -w vm.dirty_background_ratio=70;\
sysctl -w vm.dirty_ratio=90'
Finally, here's what my sysctl.conf file looks like:
# cat /etc/sysctl.conf
# shouldn't matter - no swap is used
vm.swappiness = 0

# try to keep at least 4MB in memory
vm.min_free_kbytes = 4096

# favor block cache
vm.dirty_ratio = 90
vm.dirty_background_ratio = 70

# extremely favor file cache
vm.vfs_cache_pressure = 1

# reboot when OOM happens
vm.panic_on_oom = 2

# wait 5 sec before rebooting when OOM
kernel.panic = 5

# currently experimenting
kernel.shmmax = 268435456
kernel.shmall = 16777216
I've been running at the above settings for a few days now, and the phone has been faster than ever with zero ill effects.  Your mileage may vary of course.

UPDATE: Read my follow-up post!


  1. Newbie at running scripts here.

    How do I set up and run the two scripts you listed? Does the phone (Droid X w/ Liberty 1.0) run Cron?

  2. Well, I don't run the purge anymore, but I used to run it in Tasker via Sl4a. You can find Tasker on android market and google sl4a. I might write up a post about sl4a - it's a pretty cool tool.

  3. Just out of curiosity, why don't you purge anymore? Is it not needed?

  4. I really believe that manual purge is not required for an average user. The kernel will purge on its own when it thinks it needs to free memory. If you run a lot of memory hungry apps, I guess a periodic purge could help. Having said this, I'm thinking about doing a periodic purge now that I got crond running (my latest post).

  5. So on a droid x low voltage 1.1ghz stable, what would you recommed the VFS cache pressure? I've read 20, I've read 50, and 100. I have mine set to 20 at the moment. Here are the rest of the settings. Sorry for the dumb question, I've been reading about this all day after updgrading from liberty 1.5 to 2.01.

    min free kbytes - 4096
    dirty ratio - 90
    dirty background ratio - 55
    vfs Cache pressure - 20

    The most gaming I do is stupid zombies. I use the dx mostly for social and podcast type stuff.

  6. Mat, I'd say 20-30 is probably safe without have to do regular cache flushes.

  7. Dude SWEET, I run 5 on my vfs_cache_pressure.. Sprint SGSIII ...
    I think the drop cache script is sweet. I'll use "Script Manager"..
    Other than that my friend we are on the same page. Please dont mind the backlink, it for my computer repair business! ;) Thanks

    Christopher B.
    Digital Rx Computer Repair

    Digital Rx - Computer Repair Services & PC Repair Services of Sarasota Florida