{{tag>Mem SNMP}} # Mémoire Linux Voir : * [[Supervision - sonde Nagios - Memoire Linux]] * psacct [[Suivre la conso CPU par utilisateurs]] * https://linuxblog.io/free-vs-available-memory-in-linux/ * https://en.pingcap.com/blog/linux-kernel-vs-memory-fragmentation-2/ * https://blog.stephane-robert.info/docs/admin-serveurs/linux/exploiter/memoire/ * /proc/sys/vm/compact_memory * procmap * pmap * lsmem / chmem * https://linuxfr.org/news/sortie-du-noyau-linux-6-17 damon_stat * slabtop * memstrack * smem - memory reporting tool * smemcap - data collection tool for memory reporting * smemstat - memory usage monitoring tool * SLAB / SLUB / SLOB Linux Memory Management : * https://fr.wikipedia.org/wiki/Gestionnaire_de_m%C3%A9moire_virtuelle * https://lambdafunc.medium.com/linux-memory-management-2c13b5e386f1 * https://zedas.fr/posts/linux-explained-7-memory-management/ * https://www.abhik.ai/concepts/systems/memory-management * [Vidéo - The Linux Foundation - Introduction à la gestion de la mémoire sous Linux ](https://www.youtube.com/watch?v=7aONIVSXiJ8) * http://shtroumbiniouf.free.fr/CoursInfo/Systeme2/Cours/GestionMemoire/PageFault/PageFault.html * https://www.youtube.com/watch?v=Gpm2-ktMLWs * https://www.youtube.com/watch?v=A9WLYbE0p-I * https://docs.kernel.org/mm/page_tables.html Exemples : * http://shtroumbiniouf.free.fr/CoursInfo/Systeme2/Cours/GestionMemoire/PageFault/PageFault.html#DebutPage ## Fuite mémoire Voir : * Valgrind * https://www.hebergementwebs.com/tutoriels/linux-recherche-de-fuites-de-memoire-dans-les-programmes * https://qastack.fr/unix/36450/how-can-i-find-a-memory-leak-of-a-running-process * https://www.linuxtricks.fr/wiki/performance-memoire-avec-free-et-vmstat * http://www.ordinateur.cc/syst%C3%A8mes/Linux/205699.html * [Atop - Case study with atop: memory leakage](https://www.atoptool.nl/download/case_leakage.pdf) * https://github.com/iovisor/bcc/blob/master/README.md tools/memleak ## Config ### Kernel Linux param #### NUMA (Non-Uniform Memory Access) Voir : * [What is NUMA ?](https://www.kernel.org/doc/html/v4.19/vm/numa.html) * [La mémoire : numactl, lstopo, perf, etc.](https://www.it-connect.fr/chapitres/la-memoire-numactl-lstopo-perf-etc/) * ''numatop'' Disable NUMA (Pour Oracle DB) /usr/share/doc/kernel-doc-4.18.0/Documentation/x86/x86_64/boot-options.txt ~~~ini numa=off Only set up a single NUMA node spanning all memory. ~~~ Vérif ''numactl'' command can be used to check the status of NUMA ~~~ # numactl -H | grep available available: 8 nodes (0-7) ~~~ ~~~ # numactl -H | grep available available: 2 nodes (0-1) ~~~ If the number of available nodes is ''2 nodes (0-1)'' or ''8 nodes (0-7)'' then NUMA is enabled If the number of available nodes is ''1 nodes (0)'' then NUMA is not enabled. Voir aussi : * numastat * numademo #### NUMA (Non-Uniform Memory Access) - Useful Commands To identify NUMA nodes, socket memory and CPU core allocation ~~~bash numactl -H ~~~ To identify the NUMA node local to an adapter ~~~bash cat /sys/class/net//device/numa_node ~~~ To identify memory allocation and use on a particular NUMA node: ~~~bash cat /sys/devices/system/node/node/numastat ~~~ To identify NUMA node mapping to cores, use one of the following ~~~bash numactl --hardware cat /sys/devices/system/node/node/cpulist ~~~ Driver Loading - NUMA Node ~~~bash numactl --cpunodebind=1 onload_tool reload ~~~ ### Memory usage per user ~~~bash smem -u ~~~ ### Transparent HugePages (THP) Voir * https://blog.zwindler.fr/2020/02/24/transparent-hugepages-mesurer-limpact-sur-les-performances/ * https://www.postgresql.org/docs/9.4/kernel-resources.html #### Désactiver **Transparent HugePages (THP)** ~~~bash cat /sys/kernel/mm/transparent_hugepage/enabled ~~~ The following is a sample output that shows Transparent HugePages are being used as the [always] flag is enabled. ~~~ [always] never ~~~ Paramètre Kernel Linux ~~~ini transparent_hugepage=never ~~~ ## Notes Free / Aviable ~~~ $ ./centreon_plugins.pl --plugin os::linux::snmp::plugin --mode memory --hostname localhost --help ... --patch-redhat If using Red Hat distribution with net-snmp >= 5.7.2-43 and net-snmp < 5.7.2-47. But you should update net-snmp!!!! This version: used = memTotalReal - memAvailReal // free = memAvailReal Others versions: used = memTotalReal - memAvailReal - memBuffer - memCached // free = total - used ~~~ ## Exemple Voir psacct [[Suivre la conso CPU par utilisateurs]] ~~~bash free -mt ps aux --sort=-%mem | head -n 11 vmstat -SM 45 10 vmstat -SM -a 45 10 ps -C apache2 -o rss --no-headers | tr "\n" "+" | sed -e 's/+$/\n/' | bc ps -e -o pid,user,%mem,rss,vsize,cmd --sort rss ps -e -o pid,user,%mem,rss,vsize,cmd --sort vsize ps -u $USER -o comm,vsz,pcpu | sort -nrk2 ps -eo comm,pcpu,pmem,rss --sort rss | tail -n 30 | tac ~~~ ~~~bash read CommitLimit Committed_AS <<< $(egrep "CommitLimit|Committed_AS" /proc/meminfo | awk '{print $2}') echo $(( Committed_AS * 100 / CommitLimit)) ~~~ ~~~bash memused=$(free | grep "buffers/cache" | awk '{print $3}') memtotal=$(free | grep ^Mem | awk '{print $2}') echo $(( memused * 100 / memtotal)) ~~~ ~~~bash memFree=$(free | grep ^- | awk '{print $3}') memTot=$(free | grep ^Mem | awk '{print $2}') memUsed=$(echo "scale=2 ; 100 * $memFree / $memTot" | bc) echo "$memUsed % Mem used" | grep ^[3-9][0-9] ~~~ ~~~bash ps -C apache2 -o rss --no-headers | tr "\n" "+" | sed -e 's/+$/\n/' | bc read CommitLimit Committed_AS <<< $(egrep "CommitLimit|Committed_AS" /proc/meminfo | awk '{print $2}') echo $(( Committed_AS * 100 / CommitLimit)) memused=$(free | grep "buffers/cache" | awk '{print $3}') memtotal=$(free | grep ^Mem | awk '{print $2}') echo $(( memused * 100 / memtotal)) ps -e -o pid,user,rss,vsize,cmd --sort rss ~~~ ~~~bash smem -u smem -u -t -k top -b -n 1 -u apache ~~~ Taille totale de la mémoire physique de la RAM (sans la swap) ~~~ # head -1 /proc/meminfo MemTotal: 65391036 kB # awk '/MemTotal/ { print $2 }' /proc/meminfo 65391036 ~~~ ## Fonctionnement Voir : OOM Killer et Overcommit Memory http://www.blog.florian-bogey.fr/empecher-les-depassements-de-memoire-sur-un-serveur-linux.html ## Autres Clearing KernelCare Cache Source : https://www.linuxjournal.com/content/how-install-and-uninstall-kernelcare ~~~bash rm -rf /var/cache/kcare/patches echo 1 > /proc/sys/vm/compact_memory echo 3 > /proc/sys/vm/drop_caches kcarectl –update ~~~ ----------------- * https://www.postgresql.org/docs/current/kernel-resources.html Overcommit Memory Overcommit Accounting Voir : * https://clickhouse.com/blog/strict-memory-overcommit-for-postgres vm.overcommit_memory=0 Default Heuristic overcommit handling vm.overcommit_memory=1 Always overcommit. Appropriate for some scientific applications. Classic example is code using sparse arrays and just relying on the virtual memory consisting almost entirely of zero pages. vm.overcommit_memory=2 Strict setting vm.overcommit_memory=2 DOES allow overcommit. If you set overcommit_ratio to (say) 200, then memory can be committed to the extent of swap +(RAM * 200/100). The current overcommit limit and amount committed are viewable in /proc/meminfo as CommitLimit and Committed_AS A la place de overcommit_ratio Il est possible d'utiliser vm.overcommit_kbytes OOM ~~~bash echo -1000 > /proc/self/oom_score_adj cat /proc//oom_score echo -1000 > /proc//oom_score_adj # protect from OOM kill ~~~ Or in a specific cgroup, set oom_kill_disable to 1. ~~~bash echo 1000 > /proc//oom_score_adj # mark as first target ~~~ For systemd services, use `OOMScoreAdjust=-1000` in the unit file to protect critical processes. `cgroup_enable=memory swapaccount=1` ~~~ $ echo $(($(cat /sys/fs/cgroup/memory/memory.max_usage_in_bytes) / 2**20)) MB 11389 MB $ cat /sys/fs/cgroup/memory/memory.stat ~~~ ---- OOM is not invoked when memory.high is reached. However, if cgroup memory.max is reached then OOM is invoked. TLB hit TLB miss ~~~bash sudo perf stat -e dTLB-loads,dTLB-load-misses,iTLB-loads,iTLB-load-misses -a sleep 60 perf list | grep -i tlb ~~~ Slub ~~~bash slabtop --sort=1 ~~~ ~~~bash slabinfo -v ~~~ access.redhat.com/ page_owner=on ## Premature swapping while there is still plenty of pagecache to be reclaimed Cgroup v1 * https://access.redhat.com/solutions/6785021 * https://fatdba.com/2025/12/12/when-linux-swaps-away-my-sleep-mysql-rhel8-and-the-curious-case-of-high-swap-usage/ * https://docs.couchbase.com/server/current/install/install-swap-space.html * https://support.axway.com/kb/192440/language/en * https://communities.sas.com/t5/Administration-and-Deployment/Red-Hat-RHEL-8-Release-Swappiness-Algorithm-Setting-Requires/td-p/921008 * https://docs.acceldata.io/odp/documentation/troubleshooting-premature-swapping * [Change in swap behavior between RHEL 7 and RHEL 8 kernels](https://access.redhat.com/solutions/7042476)