swap

How to increase Swap size on CentOS + cPanel

Managed Dedicated Servers

Swap memory is a disk based memory that is used when the RAM memory is full. Once you reach the total amount of RAM, Linux will automatically switch and start using SWAP memory, then the inactive pages in memory will be moved to SWAP. One important thing to consider: swap is not the same as RAM, swap is based on disk space (SATA, SAS or SSD), which is slower than RAM, so keep in mind that once SWAP is used, you may see a slow down in your system performance.

Swap memory is

How can I increase Swap size on cPanel and CentOS Linux?

We can increase 4GB of swap size now.

Create Swap file

touch /newswap

Use the following command to create 4GB of swap file.

dd if=/dev/zero of=/newswap bs=1M count=4096

This command will create a 4GB Linux partition. In order to convert this partition into a Swap partition, you’ll need to use the mkswap command, as you see below:

mkswap /newswap

Alright, your new swap partition is ready to be active swap, we will do that using the command ‘swapon’, check it out:

swapon /newswap

Finally, in order to have your swap memory automatically loaded after reboot, you will need to add an entry to your /etc/fstab file, like this:

nano -w /etc/fstab

Then add this content:

/newswap swap swap defaults 0 0

Save and close the file.

How can I verify if my Swap is detected and used by the Linux system?

Simple, use the ‘free’ command:

free -m

You should get something like:

[root@server.cpaneltips.com:~]free -m
total used free shared buffers cached
Mem: 24001 19118 4883 6 638 1697
-/+ buffers/cache: 16781 7219
Swap: 7999 185 7814 

The last line shows all the values of your SWAP Memory (total swap memory, swap used and free swap left)

How can I remove my SWAP memory?

First, deactivate your SWAP using ‘swapoff’ command, like this:

swapoff /newswap

Remove your Swap entry from /etc/fstab file

nano -w /etc/fstab

Remove /newswap file using ‘rm’ commnad, as you see below:

rm /newswap -fv

Conclusion

As you see, it’s pretty easy to increase Swap size on CentOS and cPanel servers, it should take about 1 minute and then you are ready to go. Remember that using SWAP is a sympthom of lack of RAM, so, it’s always recommended to have plenty of RAM because Swap is a slow type of memory and never recommended for high demanding applications.

originally from https://cpaneltips.com/increase-swap-size-cpanel/

How to Clear RAM Memory Cache, Buffer and Swap Space on Linux

Every Linux System has three options to clear cache without interrupting any processes or services.

1. Clear PageCache only.

# sync; echo 1 > /proc/sys/vm/drop_caches

2. Clear dentries and inodes.

# sync; echo 2 > /proc/sys/vm/drop_caches

3. Clear PageCache, dentries and inodes.

# sync; echo 3 > /proc/sys/vm/drop_caches 

Explanation of above command.

sync will flush the file system buffer. Command Separated by “;” run sequentially. The shell wait for each command to terminate before executing the next command in the sequence. As mentioned in kernel documentation, writing to drop_cache will clean cache without killing any application/service, command echo is doing the job of writing to file.

If you have to clear the disk cache, the first command is safest in enterprise and production as “...echo 1 > ….” will clear the PageCache only. It is not recommended to use third option above “...echo 3 >” in production until you know what you are doing, as it will clear PageCache, dentries and inodes.

Is it a good idea to free Buffer and Cache in Linux that might be used by Linux Kernel?

When you are applying various settings and want to check, if it is actually implemented specially on I/O-extensive benchmark, then you may need to clear buffer cache. You can drop cache as explained above without rebooting the System i.e., no downtime required.

Linux is designed in such a way that it looks into disk cache before looking onto the disk. If it finds the resource in the cache, then the request doesn’t reach the disk. If we clean the cache, the disk cache will be less useful as the OS will look for the resource on the disk.

Moreover it will also slow the system for a few seconds while the cache is cleaned and every resource required by OS is loaded again in the disk-cache.

Now we will be creating a shell script to auto clear RAM cache daily at 2PM via a cron scheduler task. Create a shell script clearcache.sh and add the following lines.

#!/bin/bash
# Note, we are using "echo 3", but it is not recommended in production instead use "echo 1"
echo "echo 3 > /proc/sys/vm/drop_caches"

Set execute permission on the clearcache.sh file.

# chmod 755 clearcache.sh

Now you may call the script whenever you required to clear ram cache.

Now set a cron to clear RAM cache everyday at 2PM. Open crontab for editing.

# crontab -e

Append the below line, save and exit to run it at 2PM daily.

0  3  *  *  *  /path/to/clearcache.sh

For more details on how to cron a job you may like to check our article on 11 Cron Scheduling Jobs.

Is it good idea to auto clear RAM cache on production server?

No! it is not. Think of a situation when you have scheduled the script to clear ram cache everyday at 2PM. Everyday at 2PM the script is executed and it flushes your RAM cache. One day for whatsoever reason, may be more than expected users are online on your website and seeking resource from your server.

At the same time scheduled script run and clears everything in cache. Now all the user are fetching data from disk. It will result in server crash and corrupt the database. So clear ram-cache only when required,and known your foot steps, else you are a Cargo Cult System Administrator.

How to Clear Swap Space in Linux?

If you want to clear Swap space, you may like to run the below command.

# swapoff -a && swapon -a

Also you may add above command to a cron script above, after understanding all the associated risk.

Now we will be combining both above commands into one single command to make a proper script to clear RAM Cache and Swap Space.

# echo 3 > /proc/sys/vm/drop_caches && swapoff -a && swapon -a && printf '\n%s\n' 'Ram-cache and Swap Cleared'

OR

$ su -c "echo 3 >'/proc/sys/vm/drop_caches' && swapoff -a && swapon -a && printf '\n%s\n' 'Ram-cache and Swap Cleared'" root

After testing both above command, we will run command “free -h” before and after running the script and will check cache.

article source: http://www.tecmint.com/clear-ram-memory-cache-buffer-and-swap-space-on-linux/