{{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://kubernetes.io/blog/2025/03/25/swap-linux-improvements/ Voir aussi : * [[Swap sur ramdisk]] ~~~bash systemctl --type swap cat /proc/swaps cat /proc/vmstat ~~~ ~~~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 ? * 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 ~~~bash 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 ~~~ 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