{{tag>Brouillon Mémoire SWAP Kernel Linux}} # Notes swap mémoire Voir : * https://wiki.debian.org/Swap * https://chrisdown.name/2018/01/02/in-defence-of-swap.html * https://www.linuxtricks.fr/wiki/swap-informations-bonnes-pratiques-configuration * https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/8/html/managing_storage_devices/getting-started-with-swap_managing-storage-devices * https://fr.wikipedia.org/wiki/Gestionnaire_de_m%C3%A9moire_virtuelle * https://fr.wikipedia.org/wiki/Gestionnaire_de_m%C3%A9moire_virtuelle#/media/Fichier:Steps_In_a_Translation_Lookaside_Buffer.png * https://docs.kernel.org/admin-guide/mm/index.html ZRAM ZSWAP: * https://chrisdown.name/2026/03/24/zswap-vs-zram-when-to-use-what.html K8S : * https://kubernetes.io/blog/2025/03/25/swap-linux-improvements/ * https://github.com/kubernetes/enhancements/blob/master/keps/sig-node/2400-node-swap/README.md * https://kubernetes.io/docs/concepts/cluster-administration/swap-memory-management/ * https://medium.com/@robertbotez/demystifying-swap-in-kubernetes-a-handbook-for-devops-engineers-e5ef934593e3 * https://scaleops.com/blog/kubernetes-swap-gpu-workload-sizing/ * https://faun.pub/why-swap-on-kubernetes-isnt-the-same-as-swap-on-linux-and-what-you-should-do-instead-5c812b8392ae * https://devopscube.com/kubernetes-swap/ Mesure / Perf : * https://blog.romaindasilva.fr/optimiser-le-swap-sous-linux-pour-reduire-les-ralentissements-et-prolonger-la-vie-des-disques/ Voir aussi : * [[Swap sur ramdisk]] ~~~bash systemctl --type swap cat /proc/swaps cat /proc/vmstat ~~~ Voir : * [RedHat - What does swappiness do and how does it affect swap_tendency?](https://access.redhat.com/solutions/103833) Swap is a memory management technique where idle memory pages are moved to temporary storage Swappiness \ 0 -> 100 \ Prefer RAM -> Swap aggressively Cache Pressure \ 0 -> 100 \ Keep caches -> Drop caches aggressively ~~~bash echo 0 > /proc/sys/vm/swappiness # cat /proc/sys/vm/vfs_cache_pressure # sysctl -w vm.vfs_cache_pressure=50 ~~~ ~~~bash # Allocate storage and restrict access fallocate --length 4GiB /swapfile chmod 600 /swapfile # Format the swap space mkswap /swapfile # Activate the swap space for paging swapon /swapfile ~~~ ## Analyse consommation Consommation de la SWAP par utilisateur ~~~bash smem -u -s swap ~~~ Consommation de la SWAP par process ~~~bash smem -s swap ~~~ ou ~~~bash echo -e "SWAP_KB\tPPID\tPID\tEXE" for S_FILE in /proc/[0-9]*/ ; do S_PPID="$(awk '/^PPid:/ { print $2}' ${S_FILE}/status)" ; S_PID="$(echo ${S_FILE}/status | cut -d'/' -f3)" ; S_EXE="$(readlink $S_FILE/exe)" ; S_SWAPKB="$(awk '/VmSwap/ { print $2}' ${S_FILE}/status)" ; [ ! -z $S_SWAPKB ] && echo -e "$S_SWAPKB\t${S_PPID}\t${S_PID}\t${S_EXE}" ; done | sort -n ~~~ ou ~~~bash for file in /proc/*/status ; do awk '/Tgid|VmSwap|Name/{printf $2 " " $3}END{ print ""}' $file; done | grep kB | sort -k 3 -n ~~~ ## dphys-swapfile ~~~bash dphys-swapfile swapoff ~~~ ## Recommandations SWAP ### Prereq Oracle | **RAM** | **Swap Space** | | ----------------------- | ----------------------------- | | Between 1 GB and 2 GB | 1.5 times the size of the RAM | | Between 2 GB and 16 GB | Equal to the size of the RAM | | More than 16 GB | 16GB | | **RAM** | **Swap Space** | | ----------------------- | ----------------------------- | | Between 8 GB and 16 GB | Equal to the size of the RAM | | More than 16 GB | 16GB | ### Recommandations RedHat Source : https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/8/html/managing_storage_devices/getting-started-with-swap_managing-storage-devices | **Amount of RAM in the system** | **Recommended swap space** |**Recommended swap space if allowing for hibernation** | | ------------------------------- | ------------------------------ | ----------------------------------------------------- | | ⩽ 2 GB | 2 times the amount of RAM | 3 times the amount of RAM | | > 2 GB – 8 GB | Equal to the amount of RAM | 2 times the amount of RAM | | > 8 GB – 64 GB | At least 4 GB | 1.5 times the amount of RAM | | > 64 GB | At least 4 GB | Hibernation not recommended | ## Utilisation de SWAP alors que la RAM n'est que partiellement utilisé En anglais sur les forums : * Why is Swap used when there is RAM available? * Swap being used when RAM is almost half free * Why linux has enough memory but swap is used * Why is Linux not using RAM but only Swap ? * https://discussion.fedoraproject.org/t/system-suddenly-using-all-available-swap-but-plenty-of-free-memory/79411 * https://askubuntu.com/questions/157793/why-is-swap-being-used-even-though-i-have-plenty-of-free-ram * High swap usage in spite of low RAM usage Voir : * [Change in swap behavior between RHEL 7 and RHEL 8 kernels ](https://access.redhat.com/solutions/7042476) * https://en.wikipedia.org/wiki/Memory_paging * https://www.reddit.com/r/linux4noobs/comments/1aep2bc/why_is_swap_used_when_there_is_ram_available/ * https://www.youtube.com/watch?v=fC6GuRDOL08 Le swap et la RAM sous Linux -- Ep. #6 Voir aussi : * Shared Memory * zramctl ~~~bash cat /proc/meminfo cat /proc/sys/vm/swappiness # default value of vfs_cache_pressure=100 # beyond 100 may have negative performance impact. cat /proc/sys/vm/vfs_cache_pressure cat /proc/sys/vm/dirty_ratio cat /proc/sys/vm/dirty_background_ratio cat /proc/sys/vm/overcommit_ratio ~~~ Voir VmSwap ou VmSize ~~~bash ps -eo pmem,pcpu,rss,vsize,args --sort rss grep VmSwap /proc/*/status | sort -n -r --key=2.1 | head -5 grep Vm /proc/3593/status ~~~ ## Tests SWAP Utiliser 2 giga de mémoire ~~~bash stress --vm 1 --vm-bytes 2G --timeout 30 ~~~ Vérif ~~~bash journalctl -k | grep -i "out of memory" ~~~ Autres test ~~~c #include #include #include int main() { char *p; size_t s = 100 * 1024 * 1024; for (;;) { p = malloc(s); bzero(p, s); sleep(1); } /* unreached */ } ~~~ Source : https://bbs.archlinux.org/viewtopic.php?id=212750