Stopping UT from killing apps

doniks

Senior Member
Jul 15, 2014
70
11
28
I finally found a way to not have my apps killed all the time!

I am using a device with 2 GB of ram (Nexus 7 2013) and I experienced frequently that apps are killed. Quite often it gets so bad that it's impossible to have both dekko and the browser open at the same time. It always felt like it was an out of memory situation. Today I stumbled over this remark of ShadowEO on his github page to configure the lowmemorykiller. In my experience, I need two other tweaks in addition. So in total I did the following three things:

  1. Increase the swappiness
    Checking with
    Code:
    dstat --top-oom --top-mem --vmstat -s 30
    and
    Code:
    tail -f /var/log/syslog  | grep sigkill
    I see which app the out-of-memory killer (oom) considers for killing and I see when that app is being killed. Also, I see that even though there is swap space available, it isn't being used. In order to make the system actually use the available swap space I increased the swappiness from 0 to 70:
    Code:
    cat /proc/sys/vm/swappiness
    0
    echo 70 | sudo tee /proc/sys/vm/swappiness
  2. Increase swapspace
    Now the swap is being used, but it doesn't seem to make a whole lot of a difference with the killing, so I increase the size of the swapspace.
    Out of the box, if I remember it correctly, I only had 32 MB of swap:
    Code:
    swapon -s
    The swap file being /userdata/SWAP.img. I increased it to 2 GB following these tips:
    Code:
    sudo swapoff /userdata/SWAP.img 
    sudo rm /userdata/SWAP.img
    sudo fallocate -l 2g /userdata/SWAP.img 
    sudo mkswap /userdata/SWAP.img
    sudo chmod 0600 /userdata/SWAP.img
    sudo swapon /userdata/SWAP.img
  3. Configure the lowmemorykiller
    Now a little more of the swap is being used, but it still doesn't stop the killing. Seems on Android there is something called lowmemorykiller, which on my Nexus 7 had this original configuration:
    Code:
    cat /sys/module/lowmemorykiller/parameters/minfree
    1536,2048,4096,16384
    Where 16384 pages (4k each) corresponds to 16384*4/1024 = 64 MB. So, seems that once the free memory drops below 64MB, the killing starts. Inspired by ShadowEO's tip, I set it:
    Code:
    echo "1536,2048,4096,5120,10240,15360" | sudo tee /sys/module/lowmemorykiller/parameters/minfree
    which corresponds to 20 instead of 64 MB. (I also set the last two levels to 40 and 60 MB - don't know whether that is needed.)

With all three things in place, I can now have the browser, dekko, podbird, filemanager, unav, uradio playing music, terminal and gedit and libreoffice writer running from libertine all open at the same time. I'm almost running out of apps that I care to run in addition :)

At times there was some stuttering, but of course nobody forces you to have so many apps open at the same time. However, now you can choose what you want to close to relieve the memory pressure :) Swappiness and lowmemorykiller I need to repeat after every reboot. Need to find a solution to persist this.

I'd be curious to hear whether/how this works for other people!
 
Last edited:
  • Like
Reactions: ShadowEO

doniks

Senior Member
Jul 15, 2014
70
11
28
I'm running this now for a couple of weeks without noticing any obvious problems caused by these settings (actually I'm running swappiness=100)

Just now I figured out another aspect: The browser does a very aggressive unloading of inactive tabs. The bugreport here suggests that this unloading is related to a percentage of free memory.

By doing
Code:
echo $[ 300 * 1024 ] | sudo tee /proc/sys/vm/min_free_kbytes
My expectation is that it should keep 300 megabytes of free memory (which of course is a waste). It seems to actually keep some 600+ mb free and after testing this for all of 5 minutes :) I can successfully listen to a youtube video playing in an inactive tab, while writing this post in another tab!
 
Last edited:

doniks

Senior Member
Jul 15, 2014
70
11
28
Need to find a solution to persist this.
Ok that was easy. Put the two lines
Code:
echo 100 > /proc/sys/vm/swappiness
echo "1536,2048,4096,5120,10240,15360" > /sys/module/lowmemorykiller/parameters/minfree
into /etc/rc.local. I don't dare to put the min_free_kbytes setting in there just yet.
 

sem-geologist

New member
Sep 15, 2016
1
0
0
Ok that was easy. Put the two lines
Code:
echo 100 > /proc/sys/vm/swappiness
echo "1536,2048,4096,5120,10240,15360" > /sys/module/lowmemorykiller/parameters/minfree
into /etc/rc.local. I don't dare to put the min_free_kbytes setting in there just yet.
There is this OpenStore (I can't add link, google it 'OpenStore ubuntu') which have very nice app (UT Tweek tool) for tweeking the UT. It allows to prevent killing apps and it can be set per app, so no more annoying termination of i.e. terminal scripts when you switch the apps or youtube music kill. By this method you need no reentering of the code after every reboot and you have quite granular control over killing or not killing automatically the apps.
 
Last edited:

doniks

Senior Member
Jul 15, 2014
70
11
28
There is this OpenStore (I can't add link, google it 'OpenStore ubuntu') which have very nice app (UT Tweek tool) for tweeking the UT. It allows to prevent killing apps and it can be set per app,
Yeah, I know it, and I use it, but to the best of my understanding that applies to a different kind of killing. I think the suspension itself doesn't actually terminate any process. It just freezes it. So with the UT tweak tool the process continues executing even when you switch to another app, which is indeed a good first step (yt playing in the background).

But there is another form of killing going on where UT seems to very aggressively try to keep some memory free and actually terminates a process. What is left is an a bit blurry picture of the last state of the app, that is shown in the task switcher and when you switch back. Then the app is restarted and its the job of the app to restore the state it was in previously.

My experience was that when I write an email and I'm switching to the browser to check something and back, the email app had been killed. It takes a few seconds to load and it doesnt perfectly restore the state (half written email, cursor position, selected text). Same the other way around. I'm typing a forum post but want to check that link I had in that email. Browser was killed, restarts, which takes a couple of seconds, and the halftyped posting is gone.

so no more annoying termination of i.e. terminal scripts when you switch the apps
true, with the terminal I didnt have the problem of actually be killed so often. The script you had running has just been suspended. You can revive it by typing fg, or use uttt as you suggested.

or youtube music kill. By this method you need no reentering of the code after every reboot and you have quite granular control over killing or not killing automatically the apps.
So, are you saying, with the uttt settings, you have no trouble using email and browser at the same time? Switching back and forth? Switching btw tabs in the browser? How much RAM does your device have?
 

TronFourtyTwo

New member
Oct 2, 2016
3
0
0
Hi!
Sadly in the aquaris E5 ubuntu edition(That I own) the SWAP is in a partition, so isn't so easy to expand it. I create a second swap in a file but isn't persistent after a reboot.
 

doniks

Senior Member
Jul 15, 2014
70
11
28
Interesting. Where did you create the swapfile? What exactly isnt persisted? Is it just not used anymore or does the file go away? How did you attempt to persist it? In rc.local?