内存体系结构(一)
Memory Organization
Linux Swap Memory
Swap memory, also known as swap space, is a section of a computer’s hard disk or SSD that the operating system (OS) uses to store inactive data from Random Access Memory (RAM). This allows the OS to run even when RAM is full, preventing system slowdowns or crashes.
以下引用来自于 All About Linux Swap Space:
Linux 将物理内存分为内存段,叫做页面。交换是指内存页面被复制到预先设定好的硬盘空间(叫做交换空间)的过程,目的是释放这份内存页面。物理内存和交换空间的总大小是可用的虚拟内存的总量。
swap 支持由 Linux 内核和来自 util-linux 包 软件包的用户空间实用程序提供。
What is Swap Memory?
When the OS exhausts its available RAM, it swaps data between RAM and the swap space. This mechanism, known as swapping, enhances memory management efficiency.
Swap memory is important for systems with restricted RAM or those executing memory-intensive tasks. Without swap memory, these systems are susceptible to crashing when RAM capacity is exceeded.
1 | ┌-------┐ ┌-╤═══════╤-┐ |
Swap Space
此处使用 Swap Space 描述更加直观。交换空间可以是磁盘的一个分区,也可以是一个文件。连续的交换文件和交换分区之间没有性能之别,两者的处理方式是一样的。用户可以在安装时或安装后的任何时候创建交换空间。
交换空间有两种用途:
- 将虚拟内存扩大到超过已安装的物理内存(RAM)的容量
- 用于 suspend-to-disk 支持
如果物理内存不足以支撑机器的正常运行,通过配置 Swap,可以避免 out of memory conditions,Linux OOM Killer 将尝试通过杀进程的方式来自动释放内存。
Swap Partition
交换分区可以用大多数 GNU/Linux 分区工具 ( e.g., fdisk
, cfdisk
) 创建。通过 systemd
激活,通过 swapoff
关闭。
Swap File
相比于使用一个磁盘分区作为交换空间,Swap File 可以更方便地随时调整大小或者移除。当磁盘空间有限 ( 例如常规大小的 SSD ) 时,使用交换文件更加理想。
Swap Partition vs. Swap File
Differences | Swap FIles | Swap Partitions |
---|---|---|
Permanence | Temporary or permanent. | Permanent. |
Location | Anywhere on the hard drive. | In a dedicated section of a hard drive. |
Disk space | Fragmented. However, contiguous when needed. | Contiguous space. |
Size | Resizeable as needed. | Fixed size. |
Manageability | Created and deleted using file management tools. More difficult to manage when located on a large or fragmented hard drive. | Easier to manage than a swap file. Can be created and deleted using the operating system’s disk management tools. |
Flexibility | More flexible. Ability to be added or removed without altering the disk structure. | Less flexible due to the fixed partition size. |
Security | Stored in the file system, which is more accessible to unauthorized users and less secure. | More secure, as the data is less likely to be accessed by unauthorized users. |
Use cases | Desktops, laptops, and cloud environments. | In servers and high-performance systems. |
How does Swap Memory Work?
页面交换,熟悉的配方。
- Swapping. The operating system uses algorithms based on usage patterns to decide which data to move to/from swap space.
- Paging. The OS transfers active data from saturated RAM to the swap space.
- Reclaiming. The OS transfers data that hasn’t been referenced recently from saturated RAM to the swap space.
Check Swap Memory
要检查交换空间的状态,使用
1 | $ swapon --show |
显示物理内存以及交换使用情况
1 | $ free -h |
( 当前没有交换内存配置 )
References
Linux THP
reprinted from @LRL52
Linux 的 Transparent Huge Pages ( THP ) 是一种内存管理特性,旨在提高大内存页使用的性能,通过减少页表的数量来降低 TLB ( Translation Lookaside Buffer,页表缓存 ) 缺失的可能性,从而提高系统的性能。这一特性是从 Linux 内核 2.6.38 版本开始引入的。
THP 是巨页支持的一个进一步的发展,它旨在使得使用巨页变得更加“透明”,无需应用程序的特定支持。在 THP 启用的系统中,内核会自动尝试为合适的内存区域分配巨页,而不需要应用程序进行特殊的内存分配调用或修改。
1 | # enable THP |