Linux交换分区 swap分区

一、基础知识

      SWAP就是LINUX下的虚拟内存分区,它的作用是在物理内存使用完之后,将磁盘空间(也就是SWAP分区)虚拟成内存来使用.它和Windows系统的交换文件作用类似,但是它是一段连续的磁盘空间,并且对用户不可见。

     需要注意的是,虽然这个SWAP分区能够作为"虚拟"的内存,但它的速度比物理内存可是慢多了,因此如果需要更快的速度的话,并不能寄厚望于SWAP,最好的办法仍然是加大物理内存.SWAP分区只是临时的解决办法.

linux下得分区推荐一个交换分区,一个根分区,其中交换分区如上所说作为redhat或者其它虚机所划分的内存不够时候来使用!

     交换分区(swap)的合理值一般在内存的2 倍左右,可以适当加大。实际上具体还是以实际应用为准,swap为内存的2倍也不过是一种以讹传讹的说法。如果交换分区的使用都超过4GB以上了,可想而知服务器的性能应该差很多了。

     Linux下可以创建两种类型的交换空间,一种是swap分区,一种是swap文件。前者适合有空闲的分区可以使用,后者适合于没有空的硬盘分区,硬盘的空间都已经分配完毕。

二、创建交换分区

   1.环境准备

    准备一台虚拟机,并添加一块虚拟磁盘.vdb是我添加的虚拟磁盘

[root@server0 ~]# lsblk
NAME   MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
vda    253:0    0  10G  0 disk 
└─vda1 253:1    0  10G  0 part /
vdb    253:16   0  10G  0 disk 
 

  2.划分分区

    在vdb下划分一块分区,大小位512M,作为交换分区

[root@server0 ~]# fdisk /dev/vdb

[root@server0 ~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
vda    253:0    0   10G  0 disk 
└─vda1 253:1    0   10G  0 part /
vdb    253:16   0   10G  0 disk 
└─vdb1 253:17   0  512M  0 part 
 

   3.格式化交换分区

  [root@server0 ~]# mkswap /dev/vdb1   #格式化交换分区

  [root@server0 ~]# blkid /dev/vdb1
/dev/vdb1: UUID="8ba9c6d1-afe1-47a7-bcca-557087c0c881" TYPE="swap"     #确认格式化结果

 

4.配置交换分区的开机启用

 [root@server0 ~]# vim /etc/fstab 

 /dev/vdb1 swap swap defautls 0 0   #在/etc/fstab 配置挂载信息 挂载点比较特殊 为swap

 5.确认挂载配置可用

  [root@server0 ~]# swapon -a    #启用新交换分区

  [root@server0 ~]# swapon -s    #查看挂载信息

  文件名                类型        大小    已用    权限
/dev/vdb1           partition    524284    0    -1

 [root@server0 ~]# free                     #查看内存信息
             total       used       free     shared    buffers     cached
Mem:        836720     746600      90120      17108        356     255424
-/+ buffers/cache:     490820     345900
Swap:       524284          0     524284

  6.基本命令

swapon [分区名或文件名]    #将指定分区或文件作为swap使用,也可以-a开启全部swap

swapoff [分区名或文件名]    #同上,有开就有关。

swapon -s     #查看目前使用的swap

 7.根据实际内存调整分区大小

4G以内的物理内存,SWAP 设置为内存的2倍。
4-8G的物理内存,SWAP 等于内存大小。
8-64G 的物理内存,SWAP 设置为8G。

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

 8.修改交换分区的方法

  思路:将之前的交换分区关闭,重新调整分区大小,或者新建一个主分区作为交换分区

  [root@server0 ~]# fdisk /dev/vdb

  [root@server0 ~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
vda    253:0    0   10G  0 disk 
└─vda1 253:1    0   10G  0 part /
vdb    253:16   0   10G  0 disk 
├─vdb1 253:17   0  512M  0 part 
└─vdb2 253:18   0    1G  0 part  

 [root@server0 ~]# vim /etc/fstab 

 /dev/vdb2 swap swap defautls 0 0    #开机自启

[root@server0 ~]# mkswap /dev/vdb2   #格式化

[root@server0 ~]#swapon -a   #开启所有

[root@server0 ~]# swapon /dev/vdb2
[root@server0 ~]# swapon -s
文件名                类型        大小    已用    权限
/dev/vdb2       partition    1048572    0    -

 

 

How to add Swap File

Follow these steps to add 1GB of swap to your server. If you want to add 2GB instead of 1 GB, replace 1G with 2G.

  1. 创建交换分区,存放到根下,使用的是挂载根的那部分磁盘

    sudo fallocate -l 1G /swapfile

    如果以上命令执行失败,使用下面这一条,dd命令可以指明使用哪块磁盘分区来做交换分区,bs=1024代表1024个块,count=1048576bytes=1m,也就是1024m=1G,如果要分大点做乘数即可

  2. sudo dd if=/dev/zero of=/swapfile bs=1024 count=1048576
  3. 仅允许root用户做写操作

    sudo chmod 600 /swapfile
  4. 使用mkswap命令声明swapfile为一个swap文件

    sudo mkswap /swapfile
  5. 允许使用该交换分区

    sudo swapon /swapfile

    可以设置开机自动挂载:

    /etc/fstab

    /swapfile swap swap defaults 0 0
    Copy
  6. To verify that the swap is active, use either the swapon or the free command as shown below:

    sudo swapon --show
    NAME      TYPE  SIZE   USED PRIO
    /swapfile file 1024M 507.4M   -1
    sudo free -h
                  total        used        free      shared  buff/cache   available
    Mem:           488M        158M         83M        2.3M        246M        217M
    Swap:          1.0G        506M        517M

How to adjust the swappiness value

Swappiness is a Linux kernel property that defines how often the system will use the swap space. Swappiness can have a value between 0 and 100. A low value will make the kernel to try to avoid swapping whenever possible, while a higher value will make the kernel to use the swap space more aggressively.

The default swappiness value is 60. You can check the current swappiness value by typing the following command:

 

cat /proc/sys/vm/swappiness
60

While the swappiness value of 60 is OK for most Linux systems, for production servers, you may need to set a lower value.

For example, to set the swappiness value to 10, you would run:

sudo sysctl vm.swappiness=10

To make this parameter persistent across reboots append the following line to the /etc/sysctl.conf file:

/etc/sysctl.conf

vm.swappiness=10

The optimal swappiness value depends on your system workload and how the memory is being used. You should adjust this parameter in small increments to find an optimal value.

如何删除交换分区

If for any reason you want to deactivate and remove the swap file, follow these steps:

  1. 首先停止交换分区

    sudo swapoff -v /swapfile
  2. 删除/etc/fstab里的开机自启项

  3. 最后使用rm删除文件

    sudo rm /swapfile

Conclusion

You have learned how to create a swap file and activate and configure swap space on your Linux system.


版权声明:本文为ck784101777原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。