本章我们介绍一下Linux交换分区。
1.交换分区的概念
哪些是Linuxswapspace呢?我们先来瞧瞧下边两段关于Linuxswapspace的中文介绍资料:
Linux divides its physical RAM (random access memory) into chucks of memory called pages.
Swapping is the process whereby a page of memory is copied to the preconfigured space on
the hard disk, called swap space, to free up that page of memory. The combined sizes of
the physical memory and the swap space is the amount of virtual memory available.
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.
Linux内核为了提升读写效率和速率,会将文件在显存中进行缓存linux系统安装,这部份显存就是CacheMemory(缓存内容)。就算你的程序运行结束后,CacheMemory也不会手动释放。这都会造成你在Linux系统中程序频繁读写文件后,你会发觉可用化学显存变少。当系统的数学显存不够用的时侯,就须要将数学显存中的一部份空间释放下来,以供当前运行的程序使用。这些被释放的空间可能来自一些很长时间没哪些操作的程序,这种被释放的空间被临时保存到Swap空间中,等到这些程序要运行时,再从Swap分区中恢复保存的数据到显存中。这样,系统总是在数学显存不够时,才进行swap交换。
关于swap分区,虽然我们有好多疑惑,倘若能弄清楚这种疑惑,这么你对swap的了解把握就差不多了。怎么查看Swap分区大小?swap分区大小应当怎样设置?系统在哪些时侯会使用swap分区?是否可以调整?怎样调整swap分区的大小?swap分区有哪些好坏和要注意的地方?swap分区是否必要?这么我们一个一个来瞧瞧这种疑惑吧!
2.查看swap分区大小
查看swap分区的大小以及使用情况linux swap used 0,通常使用free命令即可。如下所示,swap的大小为2044M,目前没有使用swap分区:
# free -m
total used free shared buff/cache available
Mem: 2012 418 509 9 1084 1339
Swap: 2044 0 2044

另外,我们还可以使用swapon命令查看当前swap相关信息:比如swap空间是swappartition,swapsize使用情况等详尽信息:
# swapon -s Filename Type Size Used Priority /dev/sda5 partition 2094076 0 -1 # cat /proc/swaps Filename Type Size Used Priority /dev/sda5 partition 2094076 0 -1
3.分区大小设置
系统的swap分区大小设置多大才是最优呢?关于这个这个问题,应当说只有一个统一的参考标准,具体还应当按照系统实际情况和显存的负荷综合考虑,像ORACLE的官方文档就推荐如下设置,这个是按照化学显存来做参考的:

另外,在其他博客中也听到下边一个推荐设置,其实我不清楚如何得到这个标准的。是否合理也无从考证。可以作为一个参考:
4G以内的物理内存,SWAP 设置为内存的2倍。 4-8G的物理内存,SWAP 等于内存大小。 8-64G 的物理内存,SWAP 设置为8G。 64-256G物理内存,SWAP 设置为16G
上下两个标准确实也很让人无所适从。我就有一次在一台ORACLE数据库服务器(64GB的RAM),根据官方推荐设置了一个很大的swap分区,然而我发觉似乎这个swap几乎极少用到linux swap used 0,虽然是浪费了c盘空间。所以假如依照系统实际情况和显存的负荷综合考虑,虽然应当根据第二个参考标准设置为8G即可。其实这个只是个人的一些认知。
4.释放swap分区空间

我们使用如下命令查看当前交换空间使用情况:
# free -m
total used free shared buff/cache available
Mem: 2012 418 509 9 1084 1339
Swap: 2044 44 2000
# swapon -s
Filename Type Size Used Priority
/dev/sda5 partition 2094076 44 -1
之后使用swapoff命令关掉交换分区:
# swapoff /dev/sda5
再接着使用swapon命令启用交换分区linux防火墙设置,此时查看交换分区的使用情况,你会发觉used为0了:
# swapon /dev/sda5 # swapon -s Filename Type Size Used Priority /dev/sda5 partition 2094076 0 -1
[参看]:
LinuxSwap交换分区介绍总结

