網頁知識:Real memory與Swap space

作者:webmaster 於 2015年08月03日 12:10:00
9427
次閱讀

真實記憶體與交換區

當Linux的交換空間使用時的物理內存(RAM)的量是滿的。如果系統需要更多的內存資源和內存已滿,內存中不活躍的頁面會被移動到交換空間。雖然交換空間可以幫助機器用少量的RAM,它不應該被認為用來替代更多的RAM。交換空間位於硬碟上,它比物理內存讀取時間要慢。



What is Swap Space?

Swap space in Linux is used when the amount of physical memory (RAM) is full. If the system needs more memory resources and the RAM is full, inactive pages in memory are moved to the swap space. While swap space can help machines with a small amount of RAM, it should not be considered a replacement for more RAM. Swap space is located on hard drives, which have a slower access time than physical memory.

Swap space can be a dedicated swap partition (recommended), a swap file, or a combination of swap partitions and swap files.

Swap should equal 2x physical RAM for up to 2 GB of physical RAM, and then an additional 1x physical RAM for any amount above 2 GB, but never less than 32 MB.

So, if:
M = Amount of RAM in GB, and S = Amount of swap in GB, then If M < 2
S = M *2
Else
S = M + 2

Using this formula, a system with 2 GB of physical RAM would have 4 GB of swap, while one with 3 GB of physical RAM would have 5 GB of swap. Creating a large swap space partition can be especially helpful if you plan to upgrade your RAM at a later time.

For systems with really large amounts of RAM (more than 32 GB) you can likely get away with a smaller swap partition (around 1x, or less, of physical RAM).

Important

File systems and LVM2 volumes assigned as swap space cannot be in use when being modified. For example, no system processes can be assigned the swap space, as well as no amount of swap should be allocated and used by the kernel. Use the free and cat /proc/swaps commands to verify how much and where swap is in use.
The best way to achieve swap space modifications is to boot your system in rescue mode, and then follow the instructions (for each scenario) in the remainder of this chapter. Refer to the Red Hat Enterprise Linux Installation Guide for instructions on booting into rescue mode. When prompted to mount the file system, select Skip.

查看swap空間

free -m

查看swap配置

swapon -s

關閉swap空間

swapoff -a

手動清理swap空間

swapoff -a && swapon -a

指令說明:如果已經使用了Swap,清空當下且buffers / cache還有空間,在執行swapoff -a操作時,會觸發Swap中的內容交換到真實記憶體中,數據不會遺失。

如何使用交換文件分配硬碟充當 Amazon EC2 實例中的交換空間?

創建交換文件

1.提升角色為root
sudo su
2. 使用 dd 命令在根文件系統上創建一個交換文件,其中“bs”是塊大小,“count”是塊的數量。在該示例中,交換文件為 4 GB:
dd if=/dev/zero of=/swapfile bs=1M count=4096
3.更新交換文件的讀取權限和寫入權限:
chmod 600 /swapfile
4.設置 Linux 交換區域:
mkswap /swapfile
5.通過將交換文件添加到交換空間,使交換文件立即可供使用:
swapon /swapfile
6.確認過程已成功完成:
swapon -s
7.透過編輯 /etc/fstab 文件,在開機時自動掛載交換文件:
vi /etc/fstab
8.按a進入編輯,再文件中加入下面指令(通常加在最後一行)
/swapfile swap swap defaults 0 0
9.重開機或是直接執行掛載
mount -a
10.確認是否有正常掛載
top

SWAP的使用策略有可能造成系統慢或是Linux系統卡的原因

Linux kernel內建的參數 swappiness 是調整系統swap的程度。
設定值是0~100,0是最大限度使用實體記憶體,100則是最大限度使用swap空間。

查看設定值的指令

cat /proc/sys/vm/swappiness

CENTOS的值是30,UBUNTU的預設值是60

調整設定值為20的指令(重開機將還原)

sysctl vm.swappiness=20

如果需要永久設定此參數,需要修改/etc/sysctl.conf這隻檔案,加入vm.swappiness=20