Why does my Red Hat Enterprise Linux (RHEL) system swap despite of having free RAM?

简介: Why does my Red Hat Enterprise Linux (RHEL) system swap despite of having free RAM?Swapping in a linux system happens under two conditions.1) Anonymous mapped memory.2) Oversized workload abusing memory overcommit.In our problem, only point 1 is valid. So, we will discuss that here.

Why does my Red Hat Enterprise Linux (RHEL) system swap despite of having free RAM?
Swapping in a linux system happens under two
conditions.
1) Anonymous mapped memory.
2) Oversized workload abusing memory overcommit.
In our problem, only point 1 is valid. So, we will discuss that here.

1) Anonymous mapped memory
Only pages which aren't filesystem backed i.e. doesn't have in disk place, gets swapped. The pages which aren't filesystem backed are called anonymous pages. This can be checked in /proc/meminfo file with the AnonPages field. Unmapped pages are those which are not present in address space of any process. they are not present in page tables. But anon mapped memory is present in page table and exists in process address space. Since they are anon, they are the candidates to be swapped.

The page frame reclaim algorithm (PFRA) performs periodic reclaiming in a linux system. It does this by two methods. One is by kswapd which invoke shrink_zone( ) and shrink_slab( ) to reclaim pages from the LRU (Least Recently Used) lists and another is cache_reap( ) function which reclaims unused slabs from the slab allocator.

Now, the entire RAM is divided into zones, as can be seen in /proc/zoneinfo . Each zone in memory has a field called pages(low). Now, the function __alloc_pages( ) might discover that there are zones where the pages(free) is less than pages(low) and then it will invoke kswapd kernel thread. This indicates that the kernel is reclaiming some pages to avoid the aggressive 'low on memory' or 'out of memory' conditions.

But why do it? We have try_to_free_pages() which does the direct path reclaiming. So why we can't just sit around and wait for low on memory condition and then work. Because that will be inefficient. Because, apart from user space processes, kernel can also request pages in interrupt context. That means it can't wait until free pages are discovered. And if there is some critical code that has already acquired an exclusive lock and wants memory, we have to satisfy it. So, to be on the safe side, we have kswapd doing the reclaim. Now, this reclaim is also done from the LRU list. Meaning, only the inactive pages in the anonymous memory region are reclaimed.

Summing up, every time we hit the page allocator, i.e. whenever some memory allocation is being done, the low, high and free watermarks in a memory zone are checked and if it finds that the zone is not balanced, kswapd wakes up and balances the zone. The logic is, why would kernel want some modified anon pages to lie around for a longer time when it can sort out the allocation in a better way.

Other notes
There are two other properties of virtual memory which you should consider in these circumstances:

   •   When the kernel needs contiguous memory for some reason, and memory is fragmented, this is handled as if the system were low on memory, and swapping can occur.
   •   The kernel strongly prefers to make NUMA-local allocations, often to the point of swapping rather than making an allocation on another node.


This is the default expected behaviour under a linux system. This is how the PFRA works.

Check /proc/meminfo file and note the counters of SwapTotal and SwapFree .

---https://access.redhat.com/solutions/406253

相关文章
|
安全 Linux 数据安全/隐私保护
Red Hat Enterprise Linux 9.6 (x86_64, aarch64) - 红帽企业 Linux (RHEL)
Red Hat Enterprise Linux 9.6 (x86_64, aarch64) - 红帽企业 Linux (RHEL)
1402 36
Red Hat Enterprise Linux 9.6 (x86_64, aarch64) - 红帽企业 Linux (RHEL)
|
安全 Linux 虚拟化
Rocky Linux 10 - RHEL 100% 1:1 兼容免费发行版
Rocky Linux 10 - RHEL 100% 1:1 兼容免费发行版
1039 34
Rocky Linux 10 - RHEL 100% 1:1 兼容免费发行版
|
7月前
|
安全 Linux iOS开发
Nessus Professional 10.10 Auto Installer for RHEL 10, AlmaLinux 10, Rocky Linux 10 - Nessus 自动化安装程序
Nessus Professional 10.10 Auto Installer for RHEL 10, AlmaLinux 10, Rocky Linux 10 - Nessus 自动化安装程序
550 6
Nessus Professional 10.10 Auto Installer for RHEL 10, AlmaLinux 10, Rocky Linux 10 - Nessus 自动化安装程序
|
Oracle Cloud Native 关系型数据库
Oracle Linux 10 - Oracle 提供支持 RHEL 兼容发行版
Oracle Linux 10 - Oracle 提供支持 RHEL 兼容发行版
491 11
Oracle Linux 10 - Oracle 提供支持 RHEL 兼容发行版
|
11月前
|
Oracle 关系型数据库 Linux
Rocky Linux 9.6 正式版发布 - RHEL 100% 1:1 兼容免费发行版
Rocky Linux 9.6 正式版发布 - RHEL 100% 1:1 兼容免费发行版
1482 0
Rocky Linux 9.6 正式版发布 - RHEL 100% 1:1 兼容免费发行版
|
安全 Linux iOS开发
Nessus Professional 10.8 | 10.6 Auto Installer for RHEL 9, AlmaLinux 9, Rocky Linux 9 (updated May 2025)
Nessus Professional 10.8 | 10.6 Auto Installer for RHEL 9, AlmaLinux 9, Rocky Linux 9 (updated May 2025)
256 6
|
Oracle Cloud Native 关系型数据库
Oracle Linux 9.6 正式版发布 - Oracle 提供支持 RHEL 兼容发行版
Oracle Linux 9.6 正式版发布 - Oracle 提供支持 RHEL 兼容发行版
544 0
Oracle Linux 9.6 正式版发布 - Oracle 提供支持 RHEL 兼容发行版
|
人工智能 安全 Linux
Red Hat Enterprise Linux 10 正式版发布 - 红帽企业 Linux (RHEL)
Red Hat Enterprise Linux 10 正式版发布 - 红帽企业 Linux (RHEL)
1561 0
Red Hat Enterprise Linux 10 正式版发布 - 红帽企业 Linux (RHEL)
|
Linux 数据库
Red Hat Enterprise Linux(RHEL)
Red Hat(红帽)是一家美国的软件公司,专注于开发开源软件产品。公司成立于1993年,总部位于美国北卡罗来纳州的罗利市。目前,Red Hat已成为全球最大的开源解决方案提供商和商业Linux主要供应商。 Red Hat由Bob Young和Marc Ewing共同创立。Marc Ewing最初创建了自己的Linux发行版,并将其命名为红帽Linux。1995年,Bob Young收购了Marc Ewing的企业,两者合并成为红帽软件公司。此后,红帽Linux逐渐发展成为一款备受欢迎的Linux发行版。 特点
562 10
Red Hat Enterprise Linux(RHEL)
|
11月前
|
Oracle 关系型数据库 Linux
MyEMS开源系统安装之CentOS/RHEL/Rocky/AlmaLinux/Oracle Linux
本指南介绍如何在CentOS/RHEL/Rocky/AlmaLinux/Oracle Linux服务器上部署MyEMS开源能源管理系统。内容涵盖系统准备、数据库配置、多个MyEMS服务(如myems-api、myems-admin、myems-modbus-tcp等)的安装与配置,以及Nginx服务器设置和防火墙规则调整。通过完成所有步骤,您将能够访问MyEMS Admin UI和Web UI,默认端口分别为8001和80,初始登录凭据已提供。
802 0