前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Linux Swap交换分区介绍总结

Linux Swap交换分区介绍总结

作者头像
zhangdd
发布2021-03-02 11:45:06
4.5K0
发布2021-03-02 11:45:06
举报
文章被收录于专栏:zhangdd.comzhangdd.com

什么是Linux swap space呢?我们先来看看下面两段关于Linux swap space的英文介绍资料:

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内核为了提高读写效率与速度,会将文件在内存中进行缓存,这部分内存就是Cache Memory(缓存内存)。即使你的程序运行结束后,Cache Memory也不会自动释放。这就会导致你在Linux系统中程序频繁读写文件后,你会发现可用物理内存变少。当系统的物理内存不够用的时候,就需要将物理内存中的一部分空间释放出来,以供当前运行的程序使用。那些被释放的空间可能来自一些很长时间没有什么操作的程序,这些被释放的空间被临时保存到Swap空间中,等到那些程序要运行时,再从Swap分区中恢复保存的数据到内存中。这样,系统总是在物理内存不够时,才进行Swap交换。

关于Swap分区,其实我们有很多疑问,如果能弄清楚这些疑问,那么你对Swap的了解掌握就差不多了。如何查看Swap分区大小? Swap分区大小应该如何设置?系统在什么时候会使用Swap分区? 是否可以调整? 如何调整Swap分区的大小?Swap分区有什么优劣和要注意的地方? Swap分区是否必要?那么我一个一个来看看这些疑问吧!

查看Swap分区大小

free -m

total used free shared buff/cache available Mem: 64304 14396 274 1683 49632 47541 Swap: 7999 1772 6227

swapon -s

Filename Type Size Used Priority /dev/sda3 partition 8191996 1815988 -1

我这里划分了8G

Swap分区大小设置

系统的Swap分区大小设置多大才是最优呢? 关于这个问题,应该说只能有一个统一的参考标准,具体还应该根据系统实际情况和内存的负荷综合考虑,像ORACLE的官方文档就推荐如下设置,这个是根据物理内存来做参考的。

RAM

Swap Space

Up to 512 MB

2 times the size of RAM

Between 1024 MB and 2048 MB

1.5 times the size of RAM

Between 2049 MB and 8192 MB

Equal to the size of RAM

More than 8192 MB

0.75 times the size of RAM

另外在其它博客中看到下面一个推荐设置,当然我不清楚其怎么得到这个标准的。是否合理也无从考证。可以作为一个参考。

4G以内的物理内存,SWAP 设置为内存的2倍。

4-8G的物理内存,SWAP 等于内存大小。

8-64G 的物理内存,SWAP 设置为8G。

64-256G物理内存,SWAP 设置为16G。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2021-02-23,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档