How to configure Android's *internal* taskkiller

Search This thread

booon

Senior Member
Mar 8, 2008
98
0
London
blagh.co.uk
How do I edit the minfree file? I have tried looking for a guide on using adb but haven't found anything apart from how to root my hero. (which is already done)

I also looked in the new sticky wiki, but nothing there.

Do I make these changes via adb? or something else... a little heads up would be great. Just point me in the right direction and I will be fine.

I am keen to get my hands into this but its all a little alien right now.

Thanks.
 

androcheck

Senior Member
Dec 7, 2009
236
413
john.zweng.at
@booon: Like all entries under /sys/ "minfree" is kind of a virtual file. It looks like a file but is used to set or get parameters for the kernel. Therefore all changes there will be gone after a reboot.

How to edit:

Code:
# Step 1:
# On your computer in a command line window open a shell via adb:
#
[B]adb shell[/B]

# Step 2:
# View the current content of minfree (i.e. the current settings)
# ('cat' just prints the content of any file to the console)
#
[B]cat /sys/module/lowmemorykiller/parameters/minfree[/B]

# Step 3:
# Overwrite the 'file' with new values, using echo:
# (of course this is no real file, so the values don't get written, 
# instead you simply adjust the kernel's current parameters)
#
[B]echo "1536,2048,4096,21000,23000,25000" > /sys/module/lowmemorykiller/parameters/minfree[/B]

After this, use your phone some time, test if it makes a difference..
Play around with the last 3 numbers (see categories for details). The higher you set them, the earlier these processes will be killed.

After a restart of the phone all the settings are gone. So you are safe to experiment.

TODO: Step 4: Explain how to set this permanently by adding to a startup script.
 

booon

Senior Member
Mar 8, 2008
98
0
London
blagh.co.uk
@ androcheck

Thanks for your help, it's very much appreciated.

I executed the commands, now gonna have a play and see what happens.

Thanks again, and keep up the good work.

Ben.
 

HomerSp

Retired Recognized Developer
Jan 5, 2010
621
591
Stockholm
It's pretty easy to make the settings permanent (by editing /system/init.d/ramzswap.sh). Here's how I did it:
Code:
adb remount
adb pull /system/init.d/ramzswap.sh .
then edit ramzswap.sh (which now should be in your current working directory) and add
Code:
echo "1536,3072,4096,21000,23000,25000" > /sys/module/lowmemorykiller/parameters/minfree
to the end of the file. Save and do
Code:
adb remount
adb push ramzswap.sh /system/init.d/

Note: This probably only works on MoDaCo
 
Last edited:

HomerSp

Retired Recognized Developer
Jan 5, 2010
621
591
Stockholm
I'm not sure if ramzswap.sh exists on any other rom besides MoDaco, maybe someone knows a more generic init file that exists on all systems? (I can't check right now as I'm not at home)
 

nuumuun

Senior Member
Jun 10, 2008
459
74
Google Pixel 6 Pro
Google Pixel 6
make the changes permanent

this minfree tweaking is genius! :cool:

this is what i did to make the changes permanent (on aosp-2.1-jnwhite-r4 ROM):
Code:
adb remount

adb pull adb pull /system/etc/init.d/99complete .   <-- don't forget the dot :p

[I]added[/I]
echo "1536,2048,4096,5120,15360,23040" > /sys/module/lowmemorykiller/parameters/minfree
[I]to the end of 99complete[/I]

adb push 99complete /system/etc/init.d/.

adb shell chmod 755 /system/etc/init.d/99complete

adb reboot
i guess you could use any of the script files in the init.d folder or even create a new one

thanks again for this tweak!! :D
 
Last edited:

mach2005

Senior Member
Wow, what an improvement.

I'm running without compcash/ramzwap and tweaked de memorykiller to: 1536,2048,4096,5120,15360,23040.

Free RAM is about 70 mb all the time.

LOOKS LIKE I HAVE A PROCESSOR UPGRADE! LIGHTNING FAST!

(I have about 15 apps installed)
 
Last edited:

leinad

Senior Member
May 7, 2007
199
24
Rennes
These values can be directly edited in your initrc
*APP_ADJ, *APP_MIN_ADJ, *ADJ, *PROVIDER_MEM, *SERVER_MEM and *APP_MEM
Hy all.
I'm currently using "AOSP-2.1-jnwhiteh-r4", and I discovered something weird :

In the file init.rc, I found the following lines :
Code:
# Define the memory thresholds at which the above process classes will
# be killed.  These numbers are in pages (4k).
    setprop ro.FOREGROUND_APP_MEM 1536
    setprop ro.VISIBLE_APP_MEM 2048
    setprop ro.SECONDARY_SERVER_MEM 4096
    setprop ro.BACKUP_APP_MEM 4096
    setprop ro.HOME_APP_MEM 4096
    setprop ro.HIDDEN_APP_MEM 5120
    setprop ro.CONTENT_PROVIDER_MEM 5632
    setprop ro.EMPTY_APP_MEM 6144

I also found the following line :
Code:
    write /sys/module/lowmemorykiller/parameters/minfree 1536,2048,4096,5120,5632,6144
So it seems that changing the numbers associated with every constant is not enough ...
 

denvro

Senior Member
Sep 28, 2007
85
8
Compcache

Why do I have to disable Compcache? I thought this was ment to speed up the phone. :confused:

Why can't compcache and the oom killer work side-by-side?
 

androcheck

Senior Member
Dec 7, 2009
236
413
john.zweng.at
Why do I have to disable Compcache? I thought this was ment to speed up the phone. :confused:

Why can't compcache and the oom killer work side-by-side?

Sorry, this was a misunderstanding. You do not have to disable compcache. This was just what I did for testing. Of course you can use it while also tuning lowmemkiller.

I was just thinking about this:
The kernel's lowmemkiller gets active when the available memory goes below these defined limits. As compcache acts as a swap device to the kernel and I am not sure if the kernel includes this additonal swap memory when calculating the available memory I just wanted to be sure and deactivated it in the first place. But in principle there is no problem using it along with lowmemkiller tuning.

Maybe you to have set higher values to get the same effect when compcache enabled (as you have more available memory). But as said, I am not sure about this. You will have to try.
 

androcheck

Senior Member
Dec 7, 2009
236
413
john.zweng.at
...
I also found the following line :
Code:
    write /sys/module/lowmemorykiller/parameters/minfree 1536,2048,4096,5120,5632,6144
So it seems that changing the numbers associated with every constant is not enough ...

Hi!

Nothing weird about this. :)
After adwinp's post I tried to find out more about this constants and digged deeper into the platform's source code.

One thing seems to be clear:
As the lowmemorykiller driver is compiled into the kernel (and not loaded as Linux kernel module) the only way to configure it is the /sys/ interface (i.e. writing into the 'files' in /sys/module/lowmemorykiller/parameters/).
That's the reason why this also has to be done in the init.rc file.

If the line
Code:
write /sys/module/lowmemorykiller/parameters/minfree 1536,2048,4096,5120,5632,6144
wouldn't be there, the settings would have no effect on the kernel (which is the part who does the actual taskkilling work).

Whereas the "setprop .." lines are used to write these values to Android's property server. This is kind of a 'regsitry' service of the Android system where system parameters can be stored and retreived later by system services.

What I still do not fully understand is why does init.rc script write the values to the property service. I did a grep over the complete Android source tree for these keys (FOREGROUND_APP_MEM, VISIBLE_APP_MEM, ...) but the only occurances I found were in "com.android.server.am.ActivityManagerService.java" where they are read into final int fields which seem to be never referenced anywhere..


So to sum up (again ;)):
Echo'ing into the /sys/.../minfree file directly instructs the kernel and immediately takes effect.

Whereas the "setprop" commands write (the same) values to some "registry" where we do not know at the moment what they are used for.


So.. hopefully I did not confuse much more people now. But maybe someone knows more about this topic and can help us with some hints. I'd be delighted to learn more about this. :rolleyes:


[edit]
Once again, all of this is "as far as I understand". If I'm wrong somewhere, please correct me.. Thx.
[/edit]
 
Last edited:

booon

Senior Member
Mar 8, 2008
98
0
London
blagh.co.uk
after using 1536,3072,4096,21000,23000,25000 on my eclair rom, I am having a good experience. Everything is running smoother, and apps feel like they open quicker. My hero is definitely more responsive.

Trying to edit the 99complete file cause my hero to not be able to boot. But i may have done it wrong. Not to willing to try it again yet, I have only just re-installed, maybe after I do a nandroid backup I will try again.
 

MacCarron

Senior Member
Dec 9, 2008
521
145
Treviso
Thank you very much for this hint androcheck! :D:D. I "hijacked" one of the scripts in /system/init.d/ and added your tweak, now the changes survive reboots and the device is extremely snappier :D
 

HomerSp

Retired Recognized Developer
Jan 5, 2010
621
591
Stockholm
This really is a great fix, before I rarely had more than 40MB free RAM, now I rarely have less than 80MB :D
 

androcheck

Senior Member
Dec 7, 2009
236
413
john.zweng.at
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! :)
 
Last edited:

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