Skip to content

Commit 4163d09

Browse files
committed
feat: optimizing swap on zram
1 parent 13031e3 commit 4163d09

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

hosts/12kingdoms-shoukei/apple-silicon.nix

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
lib,
23
pkgs,
34
nixos-apple-silicon,
45
my-asahi-firmware,
@@ -10,6 +11,8 @@
1011
nixos-apple-silicon.nixosModules.default
1112
];
1213

14+
zramSwap.memoryPercent = lib.mkForce 75;
15+
1316
# Workaround for Mesa 25.3.0 regression
1417
# https://github.com/nix-community/nixos-apple-silicon/issues/380
1518
# https://github.com/NixOS/nixpkgs/pull/461866

modules/nixos/base/zram.nix

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,33 @@
1111
# Priority of the zram swap devices.
1212
# It should be a number higher than the priority of your disk-based swap devices
1313
# (so that the system will fill the zram swap devices before falling back to disk swap).
14-
priority = 5;
14+
priority = 100;
1515
# Maximum total amount of memory that can be stored in the zram swap devices (as a percentage of your total memory).
1616
# Defaults to 1/2 of your total RAM. Run zramctl to check how good memory is compressed.
1717
# This doesn’t define how much memory will be used by the zram swap devices.
1818
memoryPercent = 50;
1919
};
20+
21+
# Optimizing swap on zram
22+
boot.kernel.sysctl = {
23+
# vm.swappiness - Controls kernel preference for swapping (range: 0-200, default: 60)
24+
# For in-memory swap devices like zram/zswap, values above 100 are recommended.
25+
"vm.swappiness" = 180;
26+
27+
# vm.watermark_boost_factor - Controls aggressiveness of memory reclaim (default: 15000)
28+
# Setting to 0 disables watermark boost, preventing premature memory reclamation.
29+
# This allows fuller memory utilization before the kernel starts reclaiming pages.
30+
"vm.watermark_boost_factor" = 0;
31+
32+
# vm.watermark_scale_factor - Controls kswapd wakeup frequency (range: 1-1000, default: 10)
33+
# A higher value triggers background memory reclamation earlier (at 12.5% memory pressure).
34+
# Value 125 means kswapd becomes active when free memory drops below 1/125 of total memory,
35+
# balancing memory more proactively to prevent sudden swap storms at high swappiness values.
36+
"vm.watermark_scale_factor" = 125;
37+
38+
# vm.page-cluster - Controls swap readahead (range: 0-6, default: 3)
39+
# 0 means read only 1 page (2^0) at a time, disabling readahead.
40+
# For low-latency devices like zram, readahead hurts performance by fetching unnecessary data.
41+
"vm.page-cluster" = 0;
42+
};
2043
}

0 commit comments

Comments
 (0)