Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to add swap to CentOS7 system

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to add swap in the CentOS7 system, the content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

Is large memory computer swap necessary?

For computers with less memory, swap is necessary. The existence of swap makes it possible to execute memory-eating programs, and it is better to execute slowly than not to run or run. However, modern computers often have more than 8 gigabytes of memory, such as not running large or professional software, the memory is enough for daily use. In this case, is it necessary for swap to exist?

The answer depends on the usage scenario. If you run desktop Linux on your PC and turn on hibernation, you should divide swap partitions that are not less than memory; if the memory is large enough and the server does not shut down all the year round, there will be no problem without swap.

Do you want to set up swap for ssd hard drives?

Previous ssd hard drives have read and write life problems, so it is not recommended to put swap files or partitions on ssd hard drives.

The current ssd hard drive technology has been upgraded to be smarter, and if there is a need for swap, it will be no problem to put it on the ssd hard drive.

How big is the swap setting?

If hibernation is enabled, the swap partition should be divided, and the partition size should not be less than memory. The recommended value is "memory size + 2G".

Server without hibernation function, swap can be a file, a practical method to determine the size of swap is: 1G memory and less, swap size is twice the memory; 2-4G memory, the same size as memory; 5-16G memory, swap can be fixed for 4G swap;33 G and above 17-32G memory, fixed as 16G swap.

The above methods are not absolute, there are special needs can be appropriate to increase or decrease the size of swap.

How to add swap?

When installing the operating system, you can specify swap in the form of partitions (separate mount points), or you can add and remove dynamically after the system installation is complete.

View swap

The free command can view swap information about system activity, such as:

The output of $free-m # is as follows # total used free shared buff/cache available# Mem: 7976 4979 328 124 2669 270 Swap: 0 4979

The second line is the swap information, and you can see that there is no active swap.

You can also use the swapon command to view the active swap, for example:

$swapon-s # has no swap information, so no output # output with swap is # Filename Type Size Used Priority# / swap file 2097148 281052-2

Add swap

Suppose we are going to run a very memory-intensive program, and 8G of physical memory is not enough, so we can increase the virtual memory available to the system with the help of swap. The method of operation is as follows:

1. Select or create a file as swap (you can also use partitions). The commands commonly used to create files of specified size are fallocate and dd. For example, create an 8G file:

# fallocatesudo fallocate-l 8G / swap# ddsudo dd bs=1GB count=8 if=/dev/zero of=/swap

Because the process of replication in dd is slow, it is recommended to use fallocate in practice.

2. Swap files store data in memory. For security reasons, access to other users should be restricted: sudo chmod 0600 / swap

3. Format the file as swap file: sudo mkswap / swap

4. Enable the swap file: sudo swapon / swap; if you do not want to use swap, you can use the swapoff command to uninstall: sudo swapoff / swap

5. If you want the swap file to be loaded automatically after the system starts, add a line to the / etc/fstab file: / swap swap swap sw 0 0.

After adding swap, you can use the swapon-s or free-m command to view the swap information.

Swap related Settings

One of the setting parameters that is most directly related to swap and also affects system performance is vm.swappiness. The value is an integer from 0 to 100, indicating how many percent of the memory is used to start using swap. 100 means to use swap,0 whenever possible, which means that the swap spare is used only if there is not enough physical memory. For servers, between 10 and 30 is recommended.

The command to set and change swappiness is: sudo sysctl vm.swappiness=10. This command is only valid for the current system and returns to the default value after the system is rebooted. To make the setting permanent, add or change the corresponding setting line in / etc/sysctl.conf: vm.swappiness = 10.

On how to add swap in the CentOS7 system to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report