How to Add Memory Swap in Centos 7

0 174

How to Add Memory Swap in Centos 7 | In this article, I want to share how to add memory swap on Centos 7. The background of this writing is as a reminder dikala later one day I forget how cata settingnya. Why Centos 7? Because now all VPS that I run I install CentOS 7.

What is a Swap?

The system uses RAM to handle running applications. If multiple applications are running, or pretty much application session, then it will require more RAM memory. If the entire space in RAM has been used, then the application will crash. or at least hang, waiting for available space in RAM to be filled with other data.

Swap will help the performance of RAM when the RAM space running out. The memory needed to run an application will be handled by this swap. Then comes the question

Means that if you want more applications that can be run, simply adding swap?

It was a thought that is LOST. Swap can indeed help the performance of RAM, but not completely optimal. This is because the swap has limitations on read speed – written, because swap use storage space on your device, can be in the form of HDD, SSD, or NVMe.

What is the Optimal Size File Swap?

A few years back, there is a grip that the swap size should be at least 2x the size of RAM. For example, the size of 4GB RAMnya, the size of at least 8GB SWAP. Along with the development of technology, it does not become something that must again.

Any linux distro has its own grip on the size of the swap. Red Hat recommends a minimum swap size 20% of RAM for modern systems. CentOS has a different recommendation from Red Hat to the size of the swap partition. CentOS suggest the size of the swap:

  • Twice the size of RAM if RAM is less than 2 GB.
  • RAM size + 2 GB if RAM size is more than 2 GB. Example: RAM 4 GB means swap 6 GB (4 GB + 2 GB).

Ubuntu has a completely different rule for swap size is also a factor for consideration hibernation. If you require hibernation, the size of the swap becomes very important in Ubuntu.

But if we do not often use the following hibernation recommended swap size:

  1. If the RAM is less than 1 GB, swap size minimum size of RAM and at most twice the size of RAM.
  2. If the RAM is more than 1 GB, swap size of at least the square root of the size of RAM and at most twice the size of RAM.
  3. If hibernation is used, swap size should be equal to the size of RAM plus the square root of the size of RAM.

How to Add Memory Swap in Centos 7

Before you begin to add swap, try to check whether the system is already configured with the command swapnya swapon Add flag -s to display ringksan swaps used in our device

If the system returns a blank, without any information, means the swap has been no.

In addition to using swapon -s, we can actively check whether the swap with the command free -m

Nanti will be no return of a system like this :

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

1. Checking Availability Storage

df -h

Filesystem      Size  Used Avail Use% Mounted on
/dev/vda1        59G  1.5G   55G   3% /
devtmpfs        2.0G     0  2.0G   0% /dev
tmpfs           2.0G     0  2.0G   0% /dev/shm
tmpfs           2.0G  8.3M  2.0G   1% /run
tmpfs           2.0G     0  2.0G   0% /sys/fs/cgroup

2. Creating Swap Files

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

dapata count parameter value is replaced with another measure. This value is the size of the swap file to be created. 4096 means 4GB.

check if it works with the command :

ls -lh /swapfile

permissionnya still

-rw-r--r--

3. Enable Swap File

sudo chmod 600 /swapfile

Now check the permissions with

ls -lh /swapfile

permissions to

-rw-------

Now we will create the swap space with a command:

sudo mkswap /swapfile

the return of the system :

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

Time to register the swap file to swapspace

sudo swapon / swapfile

To ensure the measures that we are doing is correct, do check the swap with the command

swapon -s

If the return of a system like this, means that it is correct

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

In order to further ensure longer, check with the command

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

4. Adjust to Become Permanent Swap File

Set through fstab file

sudo nano /etc/fstab

In the bottom row, add

/swapfile   swap    swap    sw  0   0

If it is already, save dan exit

Tweak Swap Setting (Optional but highly recommended)

Swappiness

How to Add Memory Swap the next one is setting swappiness. Swappiness will set the sensitivity when the swap memory will be used. the value of 0 to 100. If filled 0, then swap file will start to be used when the RAM memory runs out, but if set in 100, then the RAM memory will never be used, in other words, fully only use swap memory

We can see the current arrangement with the command swappiness:

cat /proc/sys/vm/swappiness
30

With the above results, means the swap memory will be used if the memory ram stay 30%.

to change the value swappiness be 10 (10%), execute the following command :

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

In order for these settings permanent, add in sysctl

sudo nano /etc/sysctl.conf

on the bottom line add :

vm.swappiness = 10

Do not forget to save ya.

Cache Pressure

Check the pressure setting current cache with the command :

cat /proc/sys/vm/vfs_cache_pressure
100

Pressure to Change Cache 50

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

In order for these settings permanently, enter the sysctl

sudo nano /etc/sysctl.conf

At the end of the bottom line add

vm.vfs_cache_pressure = 50

A few tips and How to Add Memory Swap in Centos 7, by enabling memory swap, server be more helpful when approaching the limit of RAM memory, and avoid the system encountered a stuck

Get real time updates directly on you device, subscribe now.

You might also like

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More