Tweak Android for Performance, and increase battery by Saving Power Usage with Sysctl

Search This thread

defconoi

Senior Member
Oct 31, 2008
3,183
6,077
SCHUYLKILL HAVEN
www.android-unleashed.com
Here I will post tweaks as I find them to optimize Android.

When echoing in the following sysctl settings they do not remain after reboot, so we can adb remount then create a sysctl.conf file on our pc's with these settings, then ./adb push sysctl.conf /etc/ then the kernel should apply the settings every reboot

For examples of a sysctl.conf use google: Sysctl.conf Examples

Open a Terminal and Execute these commands as root

Tweak virtual memory to conserve power when running on battery:
Code:
echo 0 > /proc/sys/vm/swappiness #can be ignored if you dont use swap
echo 0 > /proc/sys/vm/dirty_expire_centisecs #can be ignored if you dont use swap
echo 0 > /proc/sys/vm/dirty_writeback_centisecs #can be ignored if you dont use swap
echo 60 > /proc/sys/vm/dirty_background_ratio
echo 95 > /proc/sys/vm/dirty_ratio

Tweak virtual memory for running on Charger

Code:
echo 0 > /proc/sys/vm/swappiness #can be ignored if you dont use swap
echo 3000 > /proc/sys/vm/dirty_expire_centisecs #can be ignored if you dont use swap
echo 500  > /proc/sys/vm/dirty_writeback_centisecs #can be ignored if you dont use swap
echo 10   > /proc/sys/vm/dirty_background_ratio
echo 40   > /proc/sys/vm/dirty_ratio

Detecting apps/services that hog the battery/cpu:
Also while on battery, you should stop all applications that don't really stay idle when you're not using them, if you want to find which apps arent really staying idle you can:
Code:
strace -p $(pidof yourapp) # for all your running applications 
ps aux | awk '{print$10,$11}' | sort -n # will list all running softs sorted by used cpu time

By default when Linux runs out of memory it goes through the process list and starts killing off processes that it thinks are using too much memory. This sysctl makes Linux kill the runaway process that made the system run out of memory instead, which is usually what you want.(this will also reduce cpu time and increase battery usage).
Code:
sysctl -w vm.oom_kill_allocating_task=1

Credit: ccpp0
vfs_cache_pressure to 10 , gives more mem to cache fs dentry and inode to save cpu
At the default value of vfs_cache_pressure = 100 the kernel will attempt to reclaim dentries and inodes at a "fair" rate with respect to pagecache and swapcache reclaim. Decreasing vfs_cache_pressure causes the kernel to prefer to retain dentry and inode caches. Increasing vfs_cache_pressure beyond 100 causes the kernel to prefer to reclaim dentries and inodes.
Code:
sysctl -w vm.vfs_cache_pressure=10


I'll be adding more tweaks for sysctl and basic cmd line tweaks as I find them, also these tweaks would be good integrated into roms, apps, and a power-save widget if anyone wants to get creative.
 
Last edited:

david1171

Senior Member
Apr 29, 2009
3,202
166
a few questions

do i enter this into the terminal emulator? also does this only need to be entered in one time? thank you in advance for any help.
 

defconoi

Senior Member
Oct 31, 2008
3,183
6,077
SCHUYLKILL HAVEN
www.android-unleashed.com
do i enter this into the terminal emulator? also does this only need to be entered in one time? thank you in advance for any help.

yes you can enter it in via terminal emulator or ./adb shell, the echo'd settings are not permanent and will reset after reboot, so use the settings to test, then follow the directions to create a /etc/sysctl.conf file to make the settings permanent after you feel comfortable with the settings(if you notice improvement)
defcon
 

free-sidux

Senior Member
Feb 22, 2009
203
13
Wow, this is cool, thank you!

Could you maybe add some more comments, I don't really understand what the single commands do. Or some place, where the function of e.g. "dirty_expire_centisecs" is explained?

One more precise question though: If I don't have a swap file on sd, does the tweaking of the swappiness still change anything?

Greetings!
 

ccpp0

Member
Nov 6, 2008
14
0
Wow, this is cool, thank you!

Could you maybe add some more comments, I don't really understand what the single commands do. Or some place, where the function of e.g. "dirty_expire_centisecs" is explained?

One more precise question though: If I don't have a swap file on sd, does the tweaking of the swappiness still change anything?

Greetings!


there is no swap space on G1, so settings related to the swap can be ignored.

echo 0 > /proc/sys/vm/dirty_expire_centisecs
echo 0 > /proc/sys/vm/dirty_writeback_centisecs

never writes out dirty pages ?
 
Last edited:

defconoi

Senior Member
Oct 31, 2008
3,183
6,077
SCHUYLKILL HAVEN
www.android-unleashed.com
Wow, this is cool, thank you!

Could you maybe add some more comments, I don't really understand what the single commands do. Or some place, where the function of e.g. "dirty_expire_centisecs" is explained?

One more precise question though: If I don't have a swap file on sd, does the tweaking of the swappiness still change anything?

Greetings!

http://tinyurl.com/nlb9ws

Note: The value of vm.swappiness=0 states that no swap space will be used at all, basically it can be ignored if you dont use swap though
 
Last edited:

tshizzle

Senior Member
Jun 14, 2007
78
0
how do i create a Sysctl.conf file with this script?
i tried the link of examples but all i could find was info about it.

everything else works but when i try
sysctl vm.block_dump=1

i get sysctl: error: 'vm.block_dump=1' is an unknown key

any ideas?
 

defconoi

Senior Member
Oct 31, 2008
3,183
6,077
SCHUYLKILL HAVEN
www.android-unleashed.com
actually ya know what I think we can just disable sysctl all together in menuconfig->Filesystems->Pseudo Filesystems->Proc->Sysctl Support when compiling a new kernel and setting these tweaks right in the kernel.
Says in menuconfig"As it is generally a good thing, you should say Y here unless
building a kernel for install/rescue disks or your system is very
limited in memory
."
So I think we can gain some memory disabling this since we dont really need it
 
Last edited:

defconoi

Senior Member
Oct 31, 2008
3,183
6,077
SCHUYLKILL HAVEN
www.android-unleashed.com
disable /dev/mem

Also I think we can disable the Memory Device Driver, /dev/mem, it says it can be disabled for security conscious and can be disabled on memory tight systems, which we are definitely memory tight plus there's just no valid app that uses /dev/mem on actual memory.
Other popular users of /dev/mem are rootkits and the like.
(note: mmap access of memory via /dev/mem was already not allowed since
a really long time)

People who want to use /dev/mem for kernel debugging can enable the config
option.
 

jroid

Senior Member
Jan 8, 2009
1,365
77
how do i create a Sysctl.conf file with this script?
i tried the link of examples but all i could find was info about it.

everything else works but when i try
sysctl vm.block_dump=1

i get sysctl: error: 'vm.block_dump=1' is an unknown key

any ideas?

I have the same exact questions that u do
 

defconoi

Senior Member
Oct 31, 2008
3,183
6,077
SCHUYLKILL HAVEN
www.android-unleashed.com
Unix98_ptys

Also do we really need UNIX98_PTYS enabled?

We can also use this to conserve memory if we dont need this as well...

menuconfig says:
"Unix98 PTY support UNIX98_PTYS

A pseudo terminal (PTY) is a software device consisting of two
halves: a master and a slave. The slave device behaves identical to
a physical terminal; the master device is used by a process to
read data from and write data to the slave, thereby emulating a
terminal. Typical programs for the master side are telnet servers
and xterms.

Linux has traditionally used the BSD-like names /dev/ptyxx for
masters and /dev/ttyxx for slaves of pseudo terminals. This scheme
has a number of problems. The GNU C library glibc 2.1 and later,
however, supports the Unix98 naming standard: in order to acquire a
pseudo terminal, a process opens /dev/ptmx; the number of the pseudo
terminal is then made available to the process and the pseudo
terminal slave can be accessed as /dev/pts/<number>. What was
traditionally /dev/ttyp2 will then be /dev/pts/2, for example.

All modern Linux systems use the Unix98 ptys. Say Y unless
you're on an embedded system and want to conserve memory."
 

jroid

Senior Member
Jan 8, 2009
1,365
77
this isn't working. i created the dir sysctl.conf in /system then tried adb pushing it but says no such file or dir. then i tried in adb shell cp /system/sysctl.conf /etc
it says cp: omitting sysctl.conf is a directory"
couldnt i just create it directly in /etc?
like this mkdir /system/etc/sysctl.conf
 

Top Liked Posts

  • There are no posts matching your filters.
  • 5
    Here I will post tweaks as I find them to optimize Android.

    When echoing in the following sysctl settings they do not remain after reboot, so we can adb remount then create a sysctl.conf file on our pc's with these settings, then ./adb push sysctl.conf /etc/ then the kernel should apply the settings every reboot

    For examples of a sysctl.conf use google: Sysctl.conf Examples

    Open a Terminal and Execute these commands as root

    Tweak virtual memory to conserve power when running on battery:
    Code:
    echo 0 > /proc/sys/vm/swappiness #can be ignored if you dont use swap
    echo 0 > /proc/sys/vm/dirty_expire_centisecs #can be ignored if you dont use swap
    echo 0 > /proc/sys/vm/dirty_writeback_centisecs #can be ignored if you dont use swap
    echo 60 > /proc/sys/vm/dirty_background_ratio
    echo 95 > /proc/sys/vm/dirty_ratio

    Tweak virtual memory for running on Charger

    Code:
    echo 0 > /proc/sys/vm/swappiness #can be ignored if you dont use swap
    echo 3000 > /proc/sys/vm/dirty_expire_centisecs #can be ignored if you dont use swap
    echo 500  > /proc/sys/vm/dirty_writeback_centisecs #can be ignored if you dont use swap
    echo 10   > /proc/sys/vm/dirty_background_ratio
    echo 40   > /proc/sys/vm/dirty_ratio

    Detecting apps/services that hog the battery/cpu:
    Also while on battery, you should stop all applications that don't really stay idle when you're not using them, if you want to find which apps arent really staying idle you can:
    Code:
    strace -p $(pidof yourapp) # for all your running applications 
    ps aux | awk '{print$10,$11}' | sort -n # will list all running softs sorted by used cpu time

    By default when Linux runs out of memory it goes through the process list and starts killing off processes that it thinks are using too much memory. This sysctl makes Linux kill the runaway process that made the system run out of memory instead, which is usually what you want.(this will also reduce cpu time and increase battery usage).
    Code:
    sysctl -w vm.oom_kill_allocating_task=1

    Credit: ccpp0
    vfs_cache_pressure to 10 , gives more mem to cache fs dentry and inode to save cpu
    At the default value of vfs_cache_pressure = 100 the kernel will attempt to reclaim dentries and inodes at a "fair" rate with respect to pagecache and swapcache reclaim. Decreasing vfs_cache_pressure causes the kernel to prefer to retain dentry and inode caches. Increasing vfs_cache_pressure beyond 100 causes the kernel to prefer to reclaim dentries and inodes.
    Code:
    sysctl -w vm.vfs_cache_pressure=10


    I'll be adding more tweaks for sysctl and basic cmd line tweaks as I find them, also these tweaks would be good integrated into roms, apps, and a power-save widget if anyone wants to get creative.
    1
    This is obviously an older thread... has anyone actually come up with a system file for this so it'll run at bootup? Will it work with CM7.1.0?
    Devs have been doing this kind of stuff for a long time with init.d scripts that run on boot, they are found in system/etc/init.d