SWAP memory on the server you should know

SWAP memory on the server you should know

As is well known, the basic hardware architecture of any computer (and of course “computer” refers to a PC / tablet / server, etc.) is based on the three main core components:

CPU-processor :

We summarize the concept in the general definition as “the brain that performs the calculations in the system”

RAM – Volatile memory / random access memory :

This is a virtual-logical memory component that performs operations and interactions with the processor and physical memory-DATA against the requirements of the operating system and other software components that require resources.

Hard disk drive :

This is the component responsible for the actual storage of HDD (hard-drive) data

SWAP memory

Full utilization of RAM can crash the system and even cause it to crash. In the various distribution systems of LINUX there is a special solution to this situation and it is based on the utilization of part of the physical disk itself to continue the RAM activity. This solution is called SWAP memory . It is important to remember that RAM is a fast and efficient memory component tens of times more than the physical memory in a computer (and the use of SWAP should be very temporary with the aim of 0)In terms of the server system, the SWAP solution can be defined as a dedicated file, as a separate partition (disk partition – the most recommended solution) or as a combination of the two.It is important to emphasize that this solution uses and “takes up” space on the actual hard disk (although this is a memory that should be volatile – RAM, again – we are talking about an extreme state that should not be reached). If the SWAP memory goes into abnormal increased activity, it is possible to reach a state of problem in the face of over-utilization in the physical disk space.

The following is the general recommendation for calculating the SWAP volume accordingly and comparing it to the RAM data:

RAM in the System Recommended SWAP 4GB or less Minimum 2GB of SWAP 4GB to 16GB Minimum 4GB of SWAP 16GB to 64GB Minimum 8GB of SWAP 64GB to 256GB Minimum 16GB of SWAP 256GB to 512GB Minimum 32GB of SWAP Actual commandsWe will check the RAM data using the free command (this is a CentOS-7 server):[root@test-server ~]# free -h PS: The flag of (h-) indicates a useful format of human readable dataWe will get an answer in the style:[root@test-server ~]# free -h total used free shared buff/cache available Mem: 23G 6.3G 1.5G 1.2G 15G 13G Swap: 15G 0B 15G Output Findings It can be seen that the server is functioning with 23GB of RAM in actual utilization of 6.3GBIn the SWAP bar you can see that the server has a potential SWAP memory up to 15GB and in practice the SWAP utilization is 0.To locate the SWAP location we will use one of the commands:[ root @ test-server ] # swapon Alternatively[root@test-server ~]# cat /proc/swaps The output will show us the path to SWAP on the server:[ root @ test-server ] # swapon NAME TYPE SIZE USED PRIO /dev/xvde1 partition 15G 0B -1 The SWAP on this server is set to the / dev / xvde1 path

Create a SWAP File

To create a SWAP file, use the command:[root@test-server ~]# dd if=/dev/zero of=/swapfile bs=1024 count=30000 When the count parameter refers to the desired block size (here 30000 = 30GB)We will arrange the permission of the file:[root@test-server ~]# chmod 600 /swapfile We will clean the file in the system:[root@test-server ~]# mkswap /swapfile We will activate the file:[root@test-server ~]# swapon /swapfile And in order to put the SWAP into activity with the following boot-boot to the server, we will edit the file / etc / fstab: and enter:/swapfile swap swap defaults 0 0

Changes in SWAP data

Once we have created the SAWP solution on the server it will remain unchanged, even in the case of increasing / decreasing the RAM volume.To change the size of the SWAP you can create a new SWAP component (as we explained above) and remove the previous SWAP:We will use the swapoff command to remove the previous SWAP:[root@test-server ~]# swapoff /dev/Old_SWAP_Path