In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to add Swap swap space on CentOS7". The content in the article is simple and clear, easy to learn and understand. Please follow the editor's train of thought to study and learn "how to add Swap swap space on CentOS7".
Preface
How to make the server respond faster? How to avoid the error of out of memory in the application? The easiest way is to increase the swap space. Swap is a reserved area on the storage disk, where the operating system can temporarily store something that cannot be stored in memory.
To some extent, this is equivalent to increasing the available memory of the server. Although reading and writing from swap is slower than memory, it is better than none, which is a safety net when there is not enough memory.
If there is no swap, once the server runs out of memory, it will start to terminate the application to free memory, or even crash, which can cause you to lose some data that you haven't saved yet, or cause a crash. Some applications explicitly require the system to configure swap to ensure the reliability of data access.
This article describes how to create and enable swap files on a CentOS 7 server.
Note: swap usually performs better on traditional mechanical hard drives, and using swap on SSD can cause problems, especially after hardware aging. Therefore, we do not recommend enabling swap for DigitalOcean and other users who use SSD-based CVM services. This may even affect other users who share the host with your virtual machine.
For DigitalOcean users, the best way to improve performance is to update Droplet. Generally speaking, the performance of the upgraded host will be improved and will be less vulnerable to hardware problems.
Preparatory work
First, you need a CentOS 7 server that has been configured with sudo privileges for non-root users (see steps 1 through 4 of this tutorial).
When you are ready, SSH to your CentOS server with that user name, ready to install the swap file.
Check the Swap information of the system
First, we need to check the storage of the system to see if swap has been configured. A system can set up multiple swap files or partitions, but generally one is enough.
Use the swapon command to check whether the system has been configured with swap, which is a general swap tool. Use the-s tag to list swap usage on the current storage device:
Swapon-s
If the command does not return a result, it means that the system has not been configured with swap.
Alternatively, we can use the free tool to view the overall memory usage of the system, where you can see the memory and swap usage status (displayed in MB):
Free-m total used free shared buffers cachedMem: 3953 315 3637 8 11 107 Muhammad + buffers/cache: 196 3756Swap: 0 4095
Here you can see that the total swap space of our system is 0, that is, no swap has been configured. This is consistent with the results we see in swapon.
Check the available storage space
Typically, we create a separate partition as a swap. However, sometimes due to hardware or software limitations, it is impossible to create a new partition, in which case you can create a swap file to achieve the same function.
Before you begin, check the free space on the disk. Enter the following instructions:
Df-h Filesystem Size Used Avail Use% Mounted on / dev/vda1 59G 1.5G 55G 3% / devtmpfs 2.0G 02.0G 0% / devtmpfs 2.0G 02.0G 0% / dev/shm tmpfs 2.0G 8.3M 2.0G 1% / run tmpfs 2.0G 02.0G 0% / sys/fs/cgroup
The-h flag here is to tell dh to output information in a human-friendly format, such as output space usage and free space in MB or GB units, rather than directly output the number of memory blocks.
From the first line, we can see that there is still room for 59GB on our storage partition, which is enough for us to operate. (this is a medium-sized new CVM, and everyone's situation may be very different. )
How big is the appropriate swap space? There are many options for this question, depending on your application needs and your personal preferences. Generally speaking, twice the amount of memory is a good starting point.
I have 4GB in my system, and if I set swap for 8GB, it will take up too much space, so I decided to just set 4GB.
Create a Swap file
Next we will create the swap file on the file system. We want to create a file called swapfile in the root directory (/). Of course, you can also choose the file name you like. The space allocated by this file will be equal to the swap space we need.
The quickest way to create is the fallocate command, which creates a file that pre-allocates a specified size of space. Enter the following instructions to create a 4GB file:
Sudo fallocate-l 4G / swapfile
As soon as you enter the password, the swap file will be created. We can check the file size with the ls command:
Ls-lh / swapfile-rw-r--r-- 1 root root 4.0G Oct 30 11:00 / swapfile
At this point, our swap file is created.
Enable Swap files
Now that we have the swap file, the system doesn't know that it should be used as a swap, so we need to tell the system to format the file as swap and enable it.
First of all, we need to change the permissions of the swap file to ensure that only root can be read, otherwise there will be great security risks. Use the chmod command for permission operations:
Sudo chmod 600 / swapfile
In this way, only root can read and write the file. Use the ls-lh command to check:
Ls-lh / swapfile-rw- 1 root root 4.0G Oct 30 11:00 / swapfile
Then, use the following command to tell the system to use the file for swap:
Sudo mkswap / swapfile Setting up swapspace version 1, size = 4194300 KiB no label, UUID=b99230bb-21af-47bc-8c37-de41129c39bf
Now the swap file can be used as a swap space. Enter the following command to start using the swap:
Sudo swapon / swapfile
We can enter the following command to confirm that the settings are in effect:
Swapon-s Filename Type Size Used Priority / swapfile file 4194300 0-1
You can see that the swap we just set up is already in the returned result. Use the free tool to confirm again:
Free-m total used free shared buffers cached Mem: 3953 315 3637 8 11 10 7-/ + buffers/cache: 196 3756 Swap: 4095 0 4095
At this point, our swap is set up and the operating system will use it when needed.
Make the Swap file permanent
At this point, we have enabled the swap file on the system, but once the system is rebooted, the server cannot automatically enable the file. To enable the system to automatically take effect on swap after a restart, we can do this by modifying the fstab file (which is a table that manages file systems and partitions).
Open the file with sudo permissions to edit:
Sudo nano / etc/fstab
Add the following line to the end of the file to tell the operating system to automatically use the swap file you just created:
/ swapfile swap swap sw 0 0
After adding, save and exit. In the future, every time the server restarts, the file is checked and swap is automatically enabled.
Change Swap configuration (optional)
There are several options involving swap that may affect the performance of the system. In most cases, these options are optional, depending on your application needs and personal preferences.
Swappiness
The swappiness parameter determines how often the system exchanges data from memory to swap space, and the value is set between 0 and 100, which represents the strength of the system to swap data from memory to swap space.
The closer the value is to 0, the more likely the system is not to swap, but only to swap when necessary. Because swap is much slower than memory, reducing reliance on swap means higher system performance.
The closer the number is to 100, the more swap the system tends to do. The memory usage habits of some applications are more suitable for this situation, which is also related to the purpose of the server.
Enter the following command to view the current swappiness value:
Cat / proc/sys/vm/swappiness 30
CentOS 7 sets the swappiness of 30 by default, which is a moderate value for most desktop systems and local servers. For VPS systems, a value close to 0 may be more appropriate.
Use the sysctl command to modify the swappiness. For example, set swappiness to 10:
Sudo sysctl vm.swappiness=10 vm.swappiness=10
This change will take effect until the next restart. If you want to permanently modify the value, you need to edit the sysctl configuration file:
Sudo nano / etc/sysctl.conf
Paste the following to the end of the file:
Vm.swappiness = 10
After editing, save and exit, and then set swappiness to this value every time the server is restarted.
Cache pressure (Cache Pressure)
Another configuration item that can be considered for change is vfs_cache_pressure, which involves the storage of special file system metafile entries. Frequent reading of this kind of information consumes performance, so extending its storage time in the cache can improve the performance of the system.
View the current setting of cache pressure through the proc file system:
Cat / proc/sys/vm/vfs_cache_pressure 100
This value is relatively high, which means that the system removes inode information from the cache faster. A more conservative value is 50, which is set using the sysctl command:
Sudo sysctl vm.vfs_cache_pressure=50 vm.vfs_cache_pressure=50
This command is valid only until it is restarted. To make this setting permanent, you need to edit the sysctl configuration file:
Sudo nano / etc/sysctl.conf
Add the following at the end of the file:
Vm.vfs_cache_pressure = 50
Save exit, and the server will automatically set the cache pressure to 50 after each restart.
Summary
At this point, our system memory has some breathing space. With swap space, you can effectively avoid some common problems.
If you still encounter OOM,out of memory error messages, or your system can't run the applications you need, the best way is to optimize your application configuration or upgrade your server, but configuring swap space is also a flexible way to save.
Thank you for your reading, the above is the content of "how to add Swap swap space on CentOS7". After the study of this article, I believe you have a deeper understanding of how to add Swap swap space on CentOS7, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.