Swappiness defaults

By default, it is 10, which means Swap will come to play once you use 90 % of RAM. You should set it to around 30- 50 % if you get "low RAM warning" often.

2 Likes

Interestingly, this is apparently a widely-held misconception and the swappiness value doesn't map directly to a percentage. It's actually more complicated than that:

So, a default of 10 is actually far too low, especially with ZRAM being available. I've been running with swappiness=133 specifically to make the kernel actively use the available ZRAM (or ZSWAP in my case).

There's some more explanation here:

7 Likes

I have the following text in the file for
vm.swappiness.
"Swappiness can have a value between 0 and 100"

This confuses me :slight_smile:

2 Likes

The only thing in the kernel documentation is:

This said, it looks like the discussion I linked above is out-of-date now, so who knows what is actually going on. :man_shrugging:

3 Likes

Oh, alright, Archwiki
https://wiki.archlinux.org/title/Swap#Swappiness

5 Likes

I wonder whether this is still true with the changes in kernel 5.8+.

It seems to make sense to me to swap out unused pages to compressed memory (i.e. ZRAM) and leave more available RAM for active pages and cache... But, needs testing.

Also, just thinking, maybe having swappiness set at a higher value so this happens earlier would mean people didn't see a massive CPU/lag spike when swapping to ZRAM first kicks in?

5 Likes

I personally use zram-generator, swappiness set to 100, page cluster 0 and a single zram0 set to half of the actual RAM. I have 8 GB RAM and on my system I found lot of improvements with this config. It might be useful to play with cache pressure value as well. Anyone else is trying a similar config?

EDIT: obviously I do not recommend this set to anyone, I'm only testing this by myself after reading lot of documentation. If anyone else is interested, that's a good start:

https://www.kernel.org/doc/html/latest/admin-guide/sysctl/vm.html#swappiness

3 Likes

I'm using a single ZRAM pool with 100% RAM size with swappiness=133 on a 32-bit Atom system with 1GB RAM and it seems to work fine - Firefox runs reasonably well enough given the CPU limitations.

Is there any reason not to set ZRAM to the same size as RAM?

Edit: Ah ha! I knew I'd read "133" somewhere:

3 Likes

I don't think so, especially on systems with RAM < 8GB but probably not only

1 Like

In any case, I think that we should consider changing default Swappiness. 10 seems like effectively we are disabling swap by default, because we also have nohang.

If user uses much RAM, nohang gets triggered and swap is mostly unused. Especially when systems have ,say, less than 8 GB RAM, this nohang triggers very often, though swap space is almost unused.

6 Likes

I'm testing this config:

 ╰─λ cat /usr/lib/systemd/zram-generator.conf
File: /usr/lib/systemd/zram-generator.conf
# This file is part of the zram-generator project
# https://github.com/systemd/zram-generator

[zram0]
# This section describes the settings for /dev/zram0.
#
# The maximum amount of memory (in MiB). If the machine has more RAM
# than this, zram device will not be created.
#
# "host-memory-limit = none" may be used to disable this limit. This
# is also the default.
host-memory-limit = none

# The fraction of memory to use as ZRAM. For example, if the machine
# has 1 GiB, and zram-fraction=0.25, then the zram device will have
# 256 MiB. Values in the range 0.10–0.50 are recommended.
#
# The default is 0.5.
zram-fraction = 1

# The maximum size of the zram device (in MiB).
#
# If host-memory times zram-fraction is greater than this,
# the size will be capped to this amount;
# for example, on a machine with 2 GiB of RAM and with zram-fraction=0.5,
# the device would still be 512 MiB in size due to the limit below.
#
# The default is 4096.
max-zram-size = 8192

# The compression algorithm to use for the zram device,
# or leave unspecified to keep the kernel default.
compression-algorithm = lzo-rle

Swappiness -> 133
Kernel -> linux-cacule (5.12.x)

The system is really responsive when I stress my RAM.

My inxi:

Summary
 ╰─λ inxi -Fxxxza
System:    Kernel: 5.12.5-1-cacule x86_64 bits: 64 compiler: gcc v: 11.1.0
           parameters: BOOT_IMAGE=/@/boot/vmlinuz-linux-cacule root=UUID=03dacf15-9056-4631-9c0f-13397609fd59 rw
           [email protected] quiet splash rd.udev.log_priority=3 vt.global_cursor_default=0
           systemd.unified_cgroup_hierarchy=1 loglevel=3 mitigations=off
           Desktop: KDE Plasma 5.22.80 tk: Qt 5.15.2 info: latte-dock wm: kwin_wayland vt: 1 dm: SDDM Distro: Garuda Linux
           base: Arch Linux
Machine:   Type: Desktop Mobo: ASUSTeK model: CROSSHAIR V FORMULA-Z v: Rev 1.xx serial: <filter> UEFI: American Megatrends
           v: 1801 date: 11/25/2013
CPU:       Info: 8-Core model: AMD FX-8320 bits: 64 type: MCP arch: Bulldozer family: 15 (21) model-id: 2 stepping: 0
           microcode: 6000852 cache: L2: 2 MiB
           flags: avx lm nx pae sse sse2 sse3 sse4_1 sse4_2 sse4a ssse3 svm bogomips: 56184
           Speed: 3512 MHz min/max: N/A Core speeds (MHz): 1: 3512 2: 3512 3: 3512 4: 3510 5: 3512 6: 3512 7: 3512 8: 3512
           Vulnerabilities: Type: itlb_multihit status: Not affected
           Type: l1tf status: Not affected
           Type: mds status: Not affected
           Type: meltdown status: Not affected
           Type: spec_store_bypass status: Vulnerable
           Type: spectre_v1 status: Vulnerable: __user pointer sanitization and usercopy barriers only; no swapgs barriers
           Type: spectre_v2 status: Vulnerable, IBPB: disabled, STIBP: disabled
           Type: srbds status: Not affected
           Type: tsx_async_abort status: Not affected
Graphics:  Device-1: AMD Curacao XT / Trinidad XT [Radeon R7 370 / R9 270X/370X] vendor: ASUSTeK driver: amdgpu v: kernel
           alternate: radeon bus-ID: 01:00.0 chip-ID: 1002:6810 class-ID: 0300
           Display: wayland server: X.Org 1.21.1.1 compositor: kwin_wayland driver: loaded: amdgpu,ati
           unloaded: modesetting,radeon alternate: fbdev,vesa display-ID: :1 screens: 1
           Screen-1: 0 s-res: 1920x1080 s-dpi: 96 s-size: 508x285mm (20.0x11.2") s-diag: 582mm (22.9")
           Monitor-1: XWAYLAND0 res: 1920x1080 hz: 72 dpi: 94 size: 520x290mm (20.5x11.4") diag: 595mm (23.4")
           OpenGL: renderer: AMD Radeon HD 8800 Series (PITCAIRN DRM 3.40.0 5.12.5-1-cacule LLVM 11.1.0)
           v: 4.6 Mesa 21.2.0-devel (git-9350900fcd) direct render: Yes
Audio:     Device-1: Advanced Micro Devices [AMD/ATI] SBx00 Azalia vendor: ASUSTeK driver: snd_hda_intel v: kernel
           bus-ID: 00:14.2 chip-ID: 1002:4383 class-ID: 0403
           Device-2: AMD Oland/Hainan/Cape Verde/Pitcairn HDMI Audio [Radeon HD 7000 Series] vendor: ASUSTeK
           driver: snd_hda_intel v: kernel bus-ID: 01:00.1 chip-ID: 1002:aab0 class-ID: 0403
           Sound Server-1: ALSA v: k5.12.5-1-cacule running: yes
           Sound Server-2: JACK v: 0.125.0 running: no
           Sound Server-3: PulseAudio v: 14.2 running: no
           Sound Server-4: PipeWire v: 0.3.28 running: yes
Network:   Device-1: Intel 82583V Gigabit Network vendor: ASUSTeK driver: e1000e v: kernel port: c000 bus-ID: 03:00.0
           chip-ID: 8086:150c class-ID: 0200
           IF: enp3s0 state: down mac: <filter>
           Device-2: Ralink RT5572 Wireless Adapter type: USB driver: rt2800usb bus-ID: 1-3:2 chip-ID: 148f:5572
           class-ID: 0000 serial: <filter>
           IF: wlp0s18f2u3 state: up mac: <filter>
Drives:    Local Storage: total: 931.51 GiB used: 372.78 GiB (40.0%)
           SMART Message: Unable to run smartctl. Root privileges required.
           ID-1: /dev/sda maj-min: 8:0 vendor: Western Digital model: WD10EZRX-00L4HB0 size: 931.51 GiB block-size:
           physical: 4096 B logical: 512 B speed: 6.0 Gb/s rotation: 5400 rpm serial: <filter> rev: 1A01 scheme: GPT
Partition: ID-1: / raw-size: 922.46 GiB size: 922.46 GiB (100.00%) used: 372.78 GiB (40.4%) fs: btrfs dev: /dev/sda2
           maj-min: 8:2
           ID-2: /boot/efi raw-size: 256 MiB size: 252 MiB (98.46%) used: 546 KiB (0.2%) fs: vfat dev: /dev/sda1 maj-min: 8:1
           ID-3: /home raw-size: 922.46 GiB size: 922.46 GiB (100.00%) used: 372.78 GiB (40.4%) fs: btrfs dev: /dev/sda2
           maj-min: 8:2
           ID-4: /var/log raw-size: 922.46 GiB size: 922.46 GiB (100.00%) used: 372.78 GiB (40.4%) fs: btrfs dev: /dev/sda2
           maj-min: 8:2
           ID-5: /var/tmp raw-size: 922.46 GiB size: 922.46 GiB (100.00%) used: 372.78 GiB (40.4%) fs: btrfs dev: /dev/sda2
           maj-min: 8:2
Swap:      Kernel: swappiness: 133 (default 60) cache-pressure: 75 (default 100)
           ID-1: swap-1 type: zram size: 7.69 GiB used: 0 KiB (0.0%) priority: 100 dev: /dev/zram0
Sensors:   System Temperatures: cpu: 23.2 C mobo: N/A gpu: amdgpu temp: 33.0 C
           Fan Speeds (RPM): N/A
Info:      Processes: 251 Uptime: 8m wakeups: 0 Memory: 7.69 GiB used: 2.02 GiB (26.3%) Init: systemd v: 248 tool: systemctl
           Compilers: gcc: 11.1.0 clang: 11.1.0 Packages: pacman: 1975 lib: 561 Shell: fish v: 3.2.2 running-in: alacritty
           inxi: 3.3.04
5 Likes

Have you tried zstd here too?

Just for reference, systemd/swap.conf from my Atom system:

zram_enabled=1
zram_size=${RAM_SIZE}
zram_count=1
zram_streams=${NCPU}
zram_alg=zstd
zram_prio=32767
3 Likes

Not yet, I'm going to try it out

2 Likes

Ok zstd it's fine, it looks a bit more responsive than lzo-rle. Anyway I'm going to test better all the algorithms and let you know the results

4 Likes

In General no Hang shouldn’t be used on higher performing systems.

Also ananicy should be not used since the default version is disabling the autogroup feature which is by many features required.

I talked with the ananicy-cpp maintainer and he is using another method to give tasks a different niceness level.

Also if using swap, then should be zstd as compression used.

4 Likes