Notes swap mémoire

Voir :

https://wiki.debian.org/Swap

https://chrisdown.name/2018/01/02/in-defence-of-swap.html

https://kubernetes.io/blog/2025/03/25/swap-linux-improvements/

Voir aussi :

Swap sur ramdisk

systemctl --type swap cat /proc/swaps cat /proc/vmstat
echo 0 > /proc/sys/vm/swappiness # cat /proc/sys/vm/vfs_cache_pressure # sysctl -w vm.vfs_cache_pressure=50
# 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

smem -u -s swap

Consommation de la SWAP par process

smem -s swap

ou

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

dphys-swapfile

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 ?

System suddenly using all available swap, but plenty of free memory

High swap usage in spite of low RAM usage

Voir :

https://en.wikipedia.org/wiki/Memory_paging

https://www.reddit.com/r/linux4noobs/comments/1aep2bc/why_is_swap_used_when_there_is_ram_available/

Voir aussi :

Shared Memory

zramctl

cat /proc/meminfo cat /proc/sys/vm/swappiness cat /proc/sys/vm/dirty_ratio cat /proc/sys/vm/dirty_background_ratio cat /proc/sys/vm/overcommit_ratio
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

stress --vm 1 --vm-bytes 2G --timeout 30

Vérif

journalctl -k | grep -i "out of memory"

Autres test

#include <stdlib.h> #include <strings.h> #include <unistd.h> 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