How to configure Android's *internal* taskkiller

Search This thread

androcheck

Senior Member
Dec 7, 2009
236
413
john.zweng.at
Another - completly different - idea which just crossed my mind:
We could also write a normal app which uses normal autostart (BootCompleteReceiver) and writes the values at startup.

This way we could also provide some GUI to change the settings anytime we want (ok.. don't know if this makes sense.. :rolleyes:). Of course this would also work only on rooted phones, as we need to be root to write the minfree settings.

But just thinking.. don't have time at the moment.. Maybe someone else can do this, or I could do it in 1 or 2 weeks if people are interested.

so long.. hope I could help.. :)
 

booon

Senior Member
Mar 8, 2008
98
0
London
blagh.co.uk
The app idea is a good one, I was thinking that myself. means we could change the setting son the fly, without having to be by the computer to change if needed.

Also, I just added the command to kaguDroid 1.1.3 rom.

added to 99complete file found > /system/etc/init.d/99complete

added to the end using notepad++ (thanks for the recommendation)
pushed back to my hero, rebooted, checked that the settings were loaded at startup, (which they are) and everything is working great.

Today is the first day when my batter level has not concerned me.
 

booon

Senior Member
Mar 8, 2008
98
0
London
blagh.co.uk
ok, I went and tried something else.

Instead of "Hijacking" a file, i created a new one in the /init.d/ directory

I called it customemory (with no file extension) and inside it, put:

Code:
#!/system/bin/sh

echo "1536,3072,4096,21000,23000,25000" > /sys/module/lowmemorykiller/parameters/minfree

rebooted my hero and the settings are there. few!

so looks like we don't have to mod a file, just create our own in the init.d and call it what we want.
 
Last edited:

booon

Senior Member
Mar 8, 2008
98
0
London
blagh.co.uk
No probs. Like you, I am looking to find a way to make it easier to do.
and for me, the first mod of a file I did, stopped my hero from booting. (boo)

This way we don't break any files! :D which is always nice.
 

masterpfa

Senior Member
Mar 17, 2008
587
32
London UK
Thanks!

Hi!

To make changes permanent, you need to add the command "echo ..." to some init-script so that it gets executed automatically on startup.

STEP 1: MAKE A NANDROID BACKUP! (just to be on the safe side..)


STEP 2: Find an init-script, where to add the command.

I don't know which init-scripts are installed on other ROMs, so I can only speak for MoDaCo ROM. Here I have found the init-script "/system/init.d/ramzswap.sh" (which is normally used for enabling compcache). Take a look in "/system/init.d/" or "/system/etc/init.d/" what you find there. It depends on your ROM. In theory you can use any script which gets executed at startup. We just want to add one command to it.


STEP 3: add the command to your script
Code:
# Make the /system/ filesystem read-/writeable:
#
[B]adb remount[/B]

# Download the identified script to your computer
#
[B]adb pull /system/init.d/ramzswap.sh .[/B]   # <-- don't forget the dot

# Now the script file should be in your local working directory on your
# computer. Open it with an editor ([B]do not use notepad.exe[/B] under windows,
# as it cannot display linux-style linebreaks correctly). Under Windows 
# I can recommend notepad++ or use Wordpad if you don't have anything
# else. Under Linux every text editor should be ok.
#
# [B]Insert[/B] the "echo" command [B]at the end of the file![/B]
# For example:

[B]echo "1536,3072,4096,21000,23000,25000" > /sys/module/lowmemorykiller/parameters/minfree[/B]

# Save the file and copy it back to your device into the directory, where
# you pulled it from.
#
[B]adb push ramzswap.sh /system/init.d/[/B]

# Make it executable again:
#
[B]adb shell chmod a+x /system/init.d/ramzswap.sh[/B]

#
# Restart your phone.
# If you did everything right, and it boots normally, you can check
# if the script works by displaying the current settings:
#
[B]adb shell cat /sys/module/lowmemorykiller/parameters/minfree[/B]

# This should give you the 6 numbers you added in the echo command to 
# the init-script.

Give us feedback how it worked.

Maybe everone could tell us, which init-script he used on a particular ROM. Or maybe the ROM-cooks are interested to add this tweak directly to their ROMs. Feel free to use this knowledge! :)

Thanks for the instructions provided, I am currently using MCCR 3.2 and the file system I have on my phone is

/system/init.d/teknologist.sh . file

Following your instructions I use the following commands..

adb remount
adb pull /system/init.d/teknologist.sh .
teknologist.sh .
adb push teknologist.sh /system/init.d/
adb shell chmod a+x /system/init.d/teknologist.sh

The question I wanted to ask was the file extracted was as follows:

/system/xbin/insmod /system/lib/modules/tun.ko
echo "performance" > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor

Do I add the command directly after, i.e

/system/xbin/insmod /system/lib/modules/tun.ko
echo "performance" > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governorecho "1536,3072,4096,21000,23000,25000" > /sys/module/lowmemorykiller/parameters/minfree

via notepad++ or do i place the command on a separate line

/system/xbin/insmod /system/lib/modules/tun.ko
echo "performance" > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
echo "1536,3072,4096,21000,23000,25000" > /sys/module/lowmemorykiller/parameters/minfree
:confused:
 

androcheck

Senior Member
Dec 7, 2009
236
413
john.zweng.at
@masterpfa: You need to add the command in a new line! Just add a new line at the end of the file and place the command there.
Empty lines in between are no problem.
 

masterpfa

Senior Member
Mar 17, 2008
587
32
London UK
@masterpfa: You need to add the command in a new line! Just add a new line at the end of the file and place the command there.
Empty lines in between are no problem.

Yes, while waiting I had tried that alongs with a few other things, got there in the end.

Thanks for all your help, this is an awesome addition to the phone, I'm seeing great battery life and performance now.
:D
 

masterpfa

Senior Member
Mar 17, 2008
587
32
London UK
Another - completly different - idea which just crossed my mind:
We could also write a normal app which uses normal autostart (BootCompleteReceiver) and writes the values at startup.

This way we could also provide some GUI to change the settings anytime we want (ok.. don't know if this makes sense.. :rolleyes:). Of course this would also work only on rooted phones, as we need to be root to write the minfree settings.

But just thinking.. don't have time at the moment.. Maybe someone else can do this, or I could do it in 1 or 2 weeks if people are interested.

so long.. hope I could help.. :)

I think there would be great interest in such an app/tool especially seeing my efforts previously
:cool:
 

nemesys504

Senior Member
Oct 13, 2006
268
10
New York, NY
Love the idea of an app

Another - completly different - idea which just crossed my mind:
We could also write a normal app which uses normal autostart (BootCompleteReceiver) and writes the values at startup.

This way we could also provide some GUI to change the settings anytime we want (ok.. don't know if this makes sense.. :rolleyes:). Of course this would also work only on rooted phones, as we need to be root to write the minfree settings.

But just thinking.. don't have time at the moment.. Maybe someone else can do this, or I could do it in 1 or 2 weeks if people are interested.

so long.. hope I could help.. :)

I would to see this idea come to life and would donate some money for the time and development.
 

leinad

Senior Member
May 7, 2007
199
24
Rennes
Battery life

Hy all

One question I have regarding the way Android does manage memory (as explained in this thread).

I understand that having processes running in background even if they are no more used seems a goog idea in case the user wants to use them again : Since they are still running, they start instantly.

But what about CPU, and thus, battery life ?
Doesn't this prevent CPU to 'underclock' ?

Thank you for your answers.
 

craig0r

Senior Member
Sep 24, 2008
1,141
77
Toronto
Hy all

One question I have regarding the way Android does manage memory (as explained in this thread).

I understand that having processes running in background even if they are no more used seems a goog idea in case the user wants to use them again : Since they are still running, they start instantly.

But what about CPU, and thus, battery life ?
Doesn't this prevent CPU to 'underclock' ?

Thank you for your answers.

AFAIK, you're right. Using this method of memory management will result in fewer background processes, and thus lower CPU usage and better battery life. In theory. As far as underclocking goes, I'm not sure about the mechanics of this, so I can't comment on it.

@nemesys504: I'm working on the app right now. It's my first android app, so it's going kinda slow, but it's not a very complex app. I don't see it taking more than a day or two, and IMO, isn't worth donations. I'm just happy to help!
 

Bad4ss

Senior Member
Jan 12, 2009
508
23
Wolverhampton
HELP please

:confused:
Don't know if anyone can help.
I have MCR 3.1, I followed the instructions like you said but when I type:
> adb shell cat /sys/module/lowmemorykiller/parameters/minfree
all I get is:
1536,2048,4096,5120,5632,6144

I have checked the file in init.d:

# cd /system/init.d
cd /system/init.d

# pwd
pwd
/system/init.d

# cat ramzswap.sh
cat ramzswap.sh
/system/xbin/insmod /system/lib/modules/tun.ko
/system/xbin/insmod /system/lib/modules/lzo_decompress.ko
/system/xbin/insmod /system/lib/modules/lzo_compress.ko
/system/xbin/insmod /system/lib/modules/xvmalloc.ko
/system/xbin/insmod /system/lib/modules/ramzswap.ko disksize_kb=131072
/system/xbin/swapon /dev/block/ramzswap0
echo "10" > /proc/sys/vm/swappiness
echo "performance" > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
echo "1536,3072,4096,21000,23000,25000" > /sys/module/lowmemorykiller/parameters/minfree

As you can see the line with the new amounts are there but I get the original figures back when I check, like above.

I have checked the file is executable
# ls -l
ls -l
-rwxrwxrwx 1 0 0 535 Jan 27 19:38 ramzswap.sh
#

So after rebooting I can't see why the numbers have not changed.
I haven't disabled compcache as one of the posts said I don't need to. - If I do need to, to get it to work, please can you give instructions of how to disable compcache.
It seems like the old numbers are stored elsewhere. Can't see why.

Can you tell me if there is anything else I need to do to get the new numbers in? :confused:
 
Last edited:

intronauta

Senior Member
Aug 29, 2009
281
106
Comment this lines (#):

Code:
#/system/xbin/insmod /system/lib/modules/tun.ko
#/system/xbin/insmod /system/lib/modules/lzo_decompress.ko
#/system/xbin/insmod /system/lib/modules/lzo_compress.ko
#/system/xbin/insmod /system/lib/modules/xvmalloc.ko
#/system/xbin/insmod /system/lib/modules/ramzswap.ko disksize_kb=131072
#/system/xbin/swapon /dev/block/ramzswap0

why appears "cat ramzswap.sh" inside of ramzswap.sh? if it's not a cut&paste problem, delete it.

# cat ramzswap.sh
cat ramzswap.sh
/system/xbin/insmod /system/lib/modules/tun.ko
/system/xbin/insmod /system/lib/modules/lzo_decompress.ko
/system/xbin/insmod /system/lib/modules/lzo_compress.ko
 

Bad4ss

Senior Member
Jan 12, 2009
508
23
Wolverhampton
Comment this lines (#):

Code:
#/system/xbin/insmod /system/lib/modules/tun.ko
#/system/xbin/insmod /system/lib/modules/lzo_decompress.ko
#/system/xbin/insmod /system/lib/modules/lzo_compress.ko
#/system/xbin/insmod /system/lib/modules/xvmalloc.ko
#/system/xbin/insmod /system/lib/modules/ramzswap.ko disksize_kb=131072
#/system/xbin/swapon /dev/block/ramzswap0

why appears "cat ramzswap.sh" inside of ramzswap.sh? if it's not a cut&paste problem, delete it.

# cat ramzswap.sh
cat ramzswap.sh
/system/xbin/insmod /system/lib/modules/tun.ko
/system/xbin/insmod /system/lib/modules/lzo_decompress.ko
/system/xbin/insmod /system/lib/modules/lzo_compress.ko

Yeah sorry aboiut that, it is what happens when you adb shell, then start typing in commands, it repeats the command below then does what you ask it to. That second "cat ramzswap.sh" isn't in the file.

I have rebooted again and the changes still havent changed. I even tried clearing dalvik cache (don't know what it does) but that still hasn't fixed it.

How do I get the settings to take effect?:confused:
 

DeXa

Senior Member
Dec 9, 2006
60
2
Sofia
There's one thing I don't get. People say they have ~50MB free mem. and so on ... well how the hell they see that amount of mem? I don't get more than 5MB free mem. in any case!
 

Bad4ss

Senior Member
Jan 12, 2009
508
23
Wolverhampton
Yeah sorry aboiut that, it is what happens when you adb shell, then start typing in commands, it repeats the command below then does what you ask it to. That second "cat ramzswap.sh" isn't in the file.

I have rebooted again and the changes still havent changed. I even tried clearing dalvik cache (don't know what it does) but that still hasn't fixed it.

How do I get the settings to take effect?:confused:

I also put the # in front of all the lines you said to, but it's still showing the original figures.

Also has clearing dalvik-cahe ruiined anything on my ext2 apps2sd partition?

I would dearly love to see the benefits of these memory enhacemets as my phone slows down a lot and I have to keep killing tasks to speed it up.
 

Bad4ss

Senior Member
Jan 12, 2009
508
23
Wolverhampton
There's one thing I don't get. People say they have ~50MB free mem. and so on ... well how the hell they see that amount of mem? I don't get more than 5MB free mem. in any case!

I think those people use apps2sd, I used to have low storage remaining, now after having apps2sd enabled I have loads free.
 

Top Liked Posts

  • There are no posts matching your filters.
  • 86
    Hi!

    Note:
    Sorry this posting got very long. But it will tell you how to configure Android's internal taskkiller which may help getting your hero really speedy again.. :) Without using any taskkiller.



    Here the long story:
    I just was curious if already someone tried to play around with Android's internal low-memory task killer.

    We all know that Android uses a different way of handling processes. Instead of killing every process after its Activity ended, processes are kept until the system needs more memory. These processes usually should not harm the overall performance and should give speed improvements if you start an Activity again. That's the idea.

    But when does Android kill a process? And which process? As far as I understood android keeps a LRU (last recently used) list and starts killing the oldest unneeded process. This way it is much smarter than any of the taskkillers we see in the Market.

    Just for curiosity I started to investigate how this mechanism works. Please correct me if you think that I got something wrong:


    What I found out:
    ActivityManagerService.java tracks the "importance" of processes (is foreground, is running a service, ..) and reflects this importance by setting the "oom_adj" value of the process.

    (For info: "oom_adj" is a value of every process under Linux which gives the kernel a hint, which process it can kill in an oom [out of memory] situation. You can see this value on every Linux 2.6 system in the proc directory: /proc/[PID]/oom_adj ). The higher this value is set, the more likely this process gets selected by the kernel's oom killer.)

    It seems that on Android the current forefround application gets an oom_adj value of 0 and as soon it's not visible anymore it gets some higher value. I assume the concrete value is dependent by the processes' place in the LRU list.


    The out-of-memory killer in the standard Linux kernel only runs in one situation: when the available memory is critical low. However in the Android Linux kernel there is implemented a more fine-grained handling of low memory situations.

    I found the kernel source file "lowmemorykiller.c" (located in the kernel source tree under "drivers/misc/"; or look here for GIT source tree: http://tinyurl.com/lowmemkiller).


    This module seems to be more configurable than the kernel's standard out-of-memory killer as you can define more than one memory limit, when it should get active and you can tell it which oom_adj values it may kill.

    In other words:
    You can say "if free memory goes below XXXX then kill some process with oom_adj greater then YYY; if free memory goes even more below than ZZZ then start to kill some processes with oom_adj greater than XYXY. and so on.."

    So it's possible to define multiple memory criterias and matching processes which can be killed in these situations. Android seems to group running processes into 6 different categories (comments taken out of "ActivityManagerServer.java"):
    Code:
    FOREGROUND_APP:
        // This is the process running the current foreground app.  We'd really
        // rather not kill it! Value set in system/rootdir/init.rc on startup.
    
    VISIBLE_APP:
        // This is a process only hosting activities that are visible to the
        // user, so we'd prefer they don't disappear. Value set in
        // system/rootdir/init.rc on startup.
    
    SECONDARY_SERVER:
        // This is a process holding a secondary server -- killing it will not
        // have much of an impact as far as the user is concerned. Value set in
        // system/rootdir/init.rc on startup.
    
    HIDDEN_APP:
        // This is a process only hosting activities that are not visible,
        // so it can be killed without any disruption. Value set in
        // system/rootdir/init.rc on startup.
    
    CONTENT_PROVIDER:
        // This is a process with a content provider that does not have any clients
        // attached to it.  If it did have any clients, its adjustment would be the
        // one for the highest-priority of those processes.
    
    EMPTY_APP:
        // This is a process without anything currently running in it.  Definitely
        // the first to go! Value set in system/rootdir/init.rc on startup.
        // This value is initalized in the constructor, careful when refering to
        // this static variable externally.
    These 6 categories are reflected by 6 memory limits which are configured for the lowmemorykiller in the kernel.

    Fortunately, it is possible to configure the lowmemorykiller at runtime! :)
    (But only if you are root). The configuration is set in the file: "/sys/module/lowmemorykiller/parameters/minfree"

    So if you want to see the current settings, you can do:

    Code:
    # cat /sys/module/lowmemorykiller/parameters/minfree
    This should produce output like this (or similiar):
    Code:
    1536,2048,4096,5120,5632,6144

    These values are the 6 memory limits on which Anedroid starts to kill processes of one of the 6 categories above. Be careful, the units of these values are pages!! 1 page = 4 kilobyte.

    So the example above says that Anddroid starts killing EMPTY_APP processes if available memory goes below 24MB (=6144*4/1024). And it starts to kill unused CONTENT_PROVIDERs if available memory goes below 22MB (=5632*4/1024).


    So if you want to try if your Hero goes faster when fewer processes are running you can try to adjust these settings. For example if you practically do not want any empty processes you can set the corresponding value very high. For example, you can set the values like this:

    Code:
    # echo "1536,2048,4096,5120,15360,23040" > /sys/module/lowmemorykiller/parameters/minfree

    This example will tell Android to kill unused Content providers if less then 60MB is available and kill empty processes if available memory goes below 90MB.

    All other processes will stay untouched! Do you see the advantage compared to process killers?


    One word about durabilty:
    If you change the settings like this, they are NOT PERMANENT. They will be gone after the next restart of your phone. So you can try to play around a little bit. Please share your results if you find some improvements! :)

    To make this settings survive also reboots you need to somehow set this at startup. I am running Modaco's custom rom and added the command to the startup script /system/init.d/ramzswap.sh, but there may be other ways to do this.

    Currently I also disabled compcache on my Hero and set the lowmemkiller very aggressive, as it seems to me that this makes my hero very responsive.

    So these are my (current) settings:
    Code:
    echo "1536,3072,4096,21000,23000,25000" > /sys/module/lowmemorykiller/parameters/minfree
    (and compcache disabled)

    But play around.. I am glad about any feedback. :)
    Please also give feedback if I am wrong or missed something!

    Thx! :rolleyes:
    4

    This is a Hero thread and quite a few months old. Also FYI - taskillers are a fallacy. Increasing the amount of free ram will only serve to reduce the number of apps you can run simultaneously it will NOT speed up your phone.
    These settings could apply to all Android builds, as Android has a built-in task killer for memory management. And no, its not a fallacy (I mean the built-in Android task killer is not a fallacy). It does speed up the phone. The *free* RAM is not unused. Rather it's used by Linux to cache the file system, which does speed things up.

    You can see the real usage by doing:

    # cat /proc/meminfo
    MemTotal: 311340 kB
    MemFree: 3656 kB
    Buffers: 580 kB
    Cached: 92136 kB
    SwapCached: 0 kB
    Active: 194460 kB
    Inactive: 49624 kB
    Active(anon): 154344 kB
    Inactive(anon): 768 kB
    Active(file): 40116 kB
    Inactive(file): 48856 kB
    Unevictable: 2788 kB
    Mlocked: 0 kB
    SwapTotal: 0 kB
    SwapFree: 0 kB
    Dirty: 20 kB
    Writeback: 0 kB
    AnonPages: 154176 kB
    Mapped: 81012 kB
    Shmem: 956 kB
    Slab: 11380 kB
    SReclaimable: 2788 kB
    SUnreclaim: 8592 kB
    KernelStack: 3224 kB
    PageTables: 11800 kB
    NFS_Unstable: 0 kB
    Bounce: 0 kB
    WritebackTmp: 0 kB
    CommitLimit: 155668 kB
    Committed_AS: 6467652 kB
    VmallocTotal: 319488 kB
    VmallocUsed: 93716 kB
    VmallocChunk: 176132 kB

    In this case my Task Manager is reporting almost 100MB free but as you can see, 92MB of that is being used by the OS as cache, and the actual unused RAM is only about 4MB. Linux kernels use most of the "free" RAM for file system caching.
    2
    The difference of this method compared to task killers like "Automatic Task Killer" is that there is no separate application involved.

    There is no widget or taskkiller process which needs to be run.

    Instead you configure the Android kernel itself how to handle processes. While taskkillers need to be run regularly (automatically or by hand) to check memory and kill processes, the way I described this gets done complete automatically by the Android kernel, immediately when available memory goes under the configured limits.

    There is also no need for ignore-lists or something like this, as the Android kernel knows which applications it can kill and which not. Furthermore you can configure much more fine-grained when to kill which processes and the kernel is using the internal "last recently used" list and kills the least needed processes first.

    External task killer only can kill processes "blindly", they cannot see which processes are "empty" (not hosting an Activity) or which have been in the background for the longest time, and so on..


    When people tell you that taskkillers are evil, they mean that taskkillers interfere with Androids process management in a way this was never intended. The way I described you still let Android handling processes itself, but you just tell it to be more restrictive. :)

    So this is far less invasive into the Android system and (should :)) have less side-effects..

    But I'm still learning. I am a programmer who wants to understand things. And fortunately here we have the source to do so.. :cool:
    2
    These values can be directly edited in your initrc
    *APP_ADJ, *APP_MIN_ADJ, *ADJ, *PROVIDER_MEM, *SERVER_MEM and *APP_MEM
    2
    Seems a good time to remind everyone about my MinFreeManager again, an alternative to ratsons AMM.

    You can grab it from the marketplace too, it allows you to change the minfree values and nothing else, it simply makes the changes and quits so no running services or extra fluff (after all it's supposed to be freeing memory, not using it) & just takes up a few kb storage space, and is completely free so no ads :)

    Just an alternative...

    chart

    MinFreeManager 1.6