Centos 添加SWAP

创建SWAP文件

我们准备在/(根目录)下创建一个SWAP分区

 

创建SWAP最简单也是最快的方法是用 fallocate。这个命令创建马上就能创建一个文件,根据你的指令决定它的大小

我们来创建一个4G大小的SWAP文件

dd if=/dev/zero of=/swapfile count=4096 bs=1MiB

我们来验证一下文件的正确性是不是4G

ls -lh /swapfile
-rw-r--r-- 1 root root 4.0G Oct 30 11:00 /swapfile

正如你想见文件已经被成功并且正确的创建了。

启用SWAP文件

现在SWAP文件已经被创建了,但是我们的系统还不知道这个文件应该被用来当作SWAP。我们需要告诉系统去格式化这个文件变成SWAP文件然后启用它。

 

在我们这这些之前我们应该先调整SWAP文件的权限让他不能被除了ROOT用户的其他人随便的读取。让其他用户可以读取或写入这个文件将会有巨大的风险。

我们用chmod命令

sudo chmod 600 /swapfile

这样就可以该文件的限制读取和写入权限,只有ROOT账户才有权限操作。

然后再执行一次 ls -lh

ls -lh /swapfile
-rw------- 1 root root 4.0G Oct 30 11:00 /swapfile

Now that our swap file is more secure, we can tell our system to set up the swap space for use by typing:

现在SWAP文件安全多了,我们可以告诉系统设置SWAP分区了

sudo mkswap /swapfile
Setting up swapspace version 1, size = 4194300 KiB
no label, UUID=b99230bb-21af-47bc-8c37-de41129c39bf

我们的SWAP文件现在已经准备作为SWAP空间了

输入以下指令启动SWAP分区

sudo swapon /swapfile

验证一下是否设置成功

swapon -s
Filename                Type        Size    Used    Priority
/swapfile               file        4194300 0     -1

This output confirms that we have a new swap file. We can use the free utility again to corroborate our findings:

free -m
             total       used       free     shared    buffers     cached
Mem:          3953        315       3637          8         11        107
-/+ buffers/cache:        196       3756
Swap:         4095          0       4095

Our swap has been set up successfully, and our operating system will begin to use it as needed.

Make the Swap File Permane设置SWAP分区永久生效

系统引导时自动启动

我们的SWAP文件已经启用了,但是当我们重启的时候系统并不会自动的加载它。我们需要更改fstab文件,这个一个管理文件系统的和分区的表。

VI nano vim 随便你们了

sudo nano /etc/fstab

 

在文件的顶部,你需要添加一行来告诉操作系统自动加载你创建的SWAP分区

/swapfile   swap    swap    sw  0   0

When you are finished adding the line, you can save and close the file. The server will check this file on each bootup, so the swap file will be ready for use from now on.

当你添加完了这行,你可以保存并且关闭文件,服务器每次启动的时候都会检查这个文件。

所以SWAP文件以后每次启动都会被准备好。

调整你的Swap设置(可选)妈个比懒得翻译了。下次再说系统默认的差不多了

 

There are a few options that you can configure that will have an impact on your system's performance when dealing with swap. These configurations are optional in most cases, and the changes that you make will depend on your application needs and your personal preference.

Swappiness

The swappiness parameter determines how often your system swaps data out of memory to the swap space. This is a value between 0 and 100 that represents the percentage of memory usage that will trigger the use of swap.

With values close to zero, the system will not swap data to the drive unless absolutely necessary. Remember, interactions with the swap file are "expensive" in that they are a lot slower than interactions with memory, and this difference in read and write speed can cause a significant reduction in an application's performance. Telling the system not to rely on the swap as much will generally make your system faster.

Values that are closer to 100 will try to put more data into swap in an effort to keep more memory free. Depending on your applications' memory profile, or what you are using your server for, this might be the better choice in some cases.

We can see the current swappiness value by reading the swappiness configuration file:

cat /proc/sys/vm/swappiness
30

CentOS 7 defaults to a swappiness setting of 30, which is a fair middle ground for most desktops and local servers. For a VPS system, we'd probably want to move it closer to 0.

We can set the swappiness to a different value by using the sysctl command. For instance, to set the swappiness to 10, we could type:

sudo sysctl vm.swappiness=10
vm.swappiness = 10

This setting will persist until the next reboot. To make the setting persist between reboots, we can add the outputted line to our sysctl configuration file:

sudo nano /etc/sysctl.conf

Add your swappiness setting to the bottom of the file:

vm.swappiness = 10

When you are finished adding the line, you can save and close the file. The server will now automatically set the swappiness to the value you declared on each bootup.

Cache Pressure

Another related value that you might want to modify is the vfs_cache_pressure. This setting affects the storage of special filesystem metadata entries. Constantly reading and refreshing this information is generally very costly, so storing it on the cache for longer is excellent for your system's performance.

You can see the current value of this cache pressure by querying the proc filesystem again:

cat /proc/sys/vm/vfs_cache_pressure
100

As it is currently configured, our system removes inode information from the cache far too quickly. We can set this to a more conservative setting, like 50, by using sysctl:

sudo sysctl vm.vfs_cache_pressure=50
vm.vfs_cache_pressure = 50

Again, this is only valid for our current session. We can change that by adding it to our configuration file, like we did with our swappiness setting:

sudo nano /etc/sysctl.conf

At the bottom, add the line that specifies your new value:

vm.vfs_cache_pressure = 50

When you are finished adding the line, you can save and close the file. The server will now automatically set the cache pressure to the value you declared on each bootup.

Conclusion

By following the steps in this guide, you will have given your server some breathing room in terms of memory usage. Swap space is incredibly useful in avoiding some common problems.

If you are running into OOM (out of memory) errors, or if you find that your system is unable to use the applications that you need, the best solution is to optimize your application configurations or upgrade your server. However, configuring swap space can give you more flexibility and can help buy you time on a less powerful server.

结语

以上只是一个过程,如果本身VPS没有SWAP叫唤分区的话,加上一个肯定是有好处的,比如阿里云;但是,本身若有SWAP,增加过多的SWAP感觉意义 就不大了,个人觉得SWAP不大于物理内存的2倍为宜。我也在很多VPS上遇到过,内存都用到SWAP去了以后,VPS就卡的很了。当然,上面这些还是我 个人肤浅的看法,也许是我没优化好的缘故吧。

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据