Tuning the oom killer / low memory killer

So the SFOS commits for changing the minfree levels hint that Jolla is frightened of SFOS processes being killed (understandably). Thus, they set the minfree levels rather high, probably to get lmkd to kill Android applications before the system oom killer does it.
From this perspective, this seems to work quite well, every time I watch a video in my Fennec browser, the system is killing Android Apps / the whole Android Subsystem afterwards.
However, I adjusted the minfree levels on my Xperia 10 II (cut them by half) and restarted the Android subsystem. This definitely improved the stability of android apps (no more killing while watching a video in the browser), but probably at the cost of system stability to some degree (higher chance of oom killing a jolla process).
If the minfree values currently used by Jolla are empirically tested or if they came from a ‘just make sure its enough’ approach is hard to tell. They may have been tested to an extreme I won’t reach in daily usage, so it may be ok for me to change them but not for users longing for extreme reliability of the system. This is something, only Jolla can tell us.

3 Likes

True, the comments in the source code must be there for a reason.

However, my fingerprint sensor still works, mpris-proxy is fine, phone calls work and no applications have been killed after three days of uptime. I set the values to roughly 1/3 of the original values and they work for me. Just halving them worked wonders, too! But I know my use case is not the general case, but so far the ones who have tried this have had similar results.

1 Like

Funny, the reason they set it so high is exactly the opposite! The person writing the commit message wrote lkmd instead of lkm:

Set minfree to 6 times higher values, only then lmkd starts killing native
Sailfish OS apps (otherwise it only kills within the App Support container,
and when there are many native apps, Android apps eventually get killed upon
launch).

I tested it myself. If I set the values 12x lower (Sony defaults) then so much ram would get used by native apps, that when you tried to launch Android app it would get killed in 2 seconds after launching, because lkmd would see there is not enough RAM and would kill it.
So setting minfree values higher is actually to make lowmemorykiller kill native apps faster, to leave some space for Android apps, not the other way around! So if you don’t use Android apps, you can set it super low.

Also, I used super low Sony values for a few days of uptime, and I also got not a single problem with native services. Because low minfree doesn’t badly affect native apps, just can cause weird interference with lkmd. And I only managed to replicate that problem once, when my browser was using 30% of memory, with a few tabs and a 2 hours long YT video playing.

TL:DR Only reason for high minfree is too frequent killing of Android apps, and I only could replicate it once. I think current defaults could be way lower.

3 Likes

My experience is closer to the @JacekJagosz, than @jenix, one. On my Xperia 10 II, I feel that browser, mail, notes, clock - all native applications - are reaped earlier than Whatsapp and Firefox, being Android apps.

1 Like

That is why I actually prefer low values and are voting for them, experience with Sony values is sooo nice, it really feels like the RAM is so much bigger, because native apps don’t get closed, and Android apps deal with closing much more gracefully, remembering where you were. The only problem is that bug that can sometimes occur, where the ram is so full you can’t open Android apps.

Oh, then I got it all wrong (future goal: reading stuff more thoroughly).
Guess I never met the “lots of native application” criteria then. I’d say my daily usage is about 50% SFOS and 50% Android apps (or more precisely: 3 SFOS and 3 Android apps being active or running in the background) and I always experience Android apps (or the whole Android runtime) to get killed.
Reducing minfree (I cut the values in half) did improve that situation, I never experienced another app kill since then. So it seems to be working better for me as well.
If there is even less risk for the system itself, I also opt for lower values, as long as Android apps keep running. But then again, I don’t know which tests Jolla did to come up with the current settings and I can only speak as ‘light’ user. Maybe things are getting completely different of you use a lot of applications at the same time.

2 Likes

Ok, I will try with halved values for minfree on my Xperia 10II.
I usually only have about 4 native apps open, but sometimes when I open another browser in android all my native apps (except phone app) will be closed after some time.
I prefer to have the native apps stay alive, and I suppose that if I have problems with android apps closing on me that I can close native apps manually to remedy that problem.

Will see if that makes a noticeable improvement for me, have been avoiding to start applications for some time now due to the issues with apps being forcibly closed on me.

I’m still experiencing Email and Browser crashes, and they are not OOM related. Just something to keep in mind…

3 Likes

Good point, I have not verified the cause I’m just assuming it has to do with some automatic termination of processes since it happens much more often when I start more apps.
Time will tell if my situation improves or not, I am now trying to leave more apps open as I used to be able to in the past.

After more than a week of use I can really recommend Sony’s default values, which are 12x lower than SFOS’.
"15360,19200,23040,26880,34415,43737" taken from Add LMK tuning that was missing compared to previous devices ¡ sonyxperiadev/device-sony-lena@c409f0e ¡ GitHub
I could only replicate the bug that made Jolla orogonally increase the values over stock just once. The bug was that all Android apps got closed after a second of launching them, but that was only once.
Otherwise the multitasking experience was great. So if someone is curious it might be worth a try, maybe on some other people’s device the bug can actually be replicated and those values are too low.
Or, those values are superior.

3 Likes

After changed the minfree to sony default value, i can open many apps without closing! That nice!

2 Likes

How does one persistently configure these settings properly on SFOS?

/etc/sysctl? /etc/udev/rules.d? systemd oneshot unit? Something else?

2 Likes

I can tell you of an incorrect way, that I used.
nano /vendor/etc/init/init.lena.rc and changed the settings to Sony defaults listed above, you can input anything.
I am sure it is risky and a mistake here could make your phone unbootable, but it works.

3 Likes

Just a follow-up to say that I have only seen positive results from the changes I made (as mentioned earlier in the thread).

1 Like

How can I adjust the values for the XA2?
There are no values to adjust in init.nile.rc.

  GNU nano 2.8.5                                           Datei: init.nile.rc                                                       

# Copyright 2014 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

on post-fs
    rm /persist/bluetooth/.bt_nv.bin
    rmdir /persist/bluetooth

on boot
    # WLAN MAC
    chown wifi wifi /sys/module/wlan/parameters/fwpath
    chown wifi wifi /sys/kernel/boot_wlan/boot_wlan

    # update foreground cpuset now that processors are up
    write /dev/cpuset/foreground/cpus 0-7
    write /dev/cpuset/foreground/boost/cpus 0-7
    write /dev/cpuset/background/cpus 0
    write /dev/cpuset/system-background/cpus 0-1

    # Set the default IRQ affinity to the primary cluster. When a
    # CPU is isolated/hotplugged, the IRQ affinity is adjusted
    # to one of the CPU from the default IRQ affinity mask.
    write /proc/irq/default_smp_affinity f

Original values are:

[root@XperiaXA2 init]# cat /sys/module/lowmemorykiller/parameters/minfree
1536,2048,4096,16384

Can you also do a PR for the XA2?

The values in the XA2 series phones are already minimal, there’s no benefit taking them any lower than that.

4 Likes

On the contrary, I didn’t see very positive impact by changing to Sony’s default values with 10 II. Weird freezes with apps and OS crashing etc. Switched back and I see the reason for 6x.

4 Likes

Good to know, interesting!

Just a quick update!

I have been using the minfree values of 65536,73728,90112,131072,196608 and so far I have been very happy with these! While I may not get the maximum amount of opened applications at the same, I think 12 is plenty good! I’ve also had zero OOM kills since.

I stand my opinion that the X10III ships with too conservative memory limits and they can be safely lowered to some extent without risking OOM at application startup, and providing a much better user experience.

Edit: I simply haven’t had more than 12 apps opened at the same time.

2 Likes

Didsomeone find something that helped with the 10II ? I tried half of jollas defaults and got weird crashes

1 Like