How to clear RAM memory cache, buffers and swap space on Linux

s2 0

Like any other operating system, GNU / Linux implemented memory management efficiently and even better. But if a process is eating your memory and you want to clear it, Linux offers a way to flush or clear the RAM cache.

  • Find the top 15 processes by memory usage in Linux
  • Find the best running processes by the highest memory and CPU usage in Linux
  • How to limit the time and memory usage of processes in Linux

How do I clear the cache on Linux?

Every Linux system has three options for clearing the cache without interrupting processes or services.

1. Clear PageCache only.

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

2. Clean dentries and inodes.

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

3. Clear page cache, dentries and inodes.

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

Explanation of the above command.

synchronize empties the file system buffer. Command separated by “;” run one after the other. The shell waits for each command to complete before executing the next command in the sequence. As mentioned in the kernel documentation, write to drop_cache clears the cache without terminating an application / service, the echo command does the job of writing to the file.

When you need to clear the hard drive cache, the safest first command in business and production is there “...echo 1 > ….” will delete that Page cache only. We don’t recommend using the third option above “...echo 3 >” in production until you know what you’re doing as it turns out Page cache, Gums, and inodes.

Is it a good idea to free up buffers and cache in Linux that could be used by the Linux kernel?

Free buffer and cache on Linux

If you want to make various settings and check whether they are actually implemented specifically on the benchmark that is rich in I / O, you may have to empty the buffer cache. You can clear the cache as described above without restarting the system, which means that no downtime is required.

Linux is designed to look into the hard drive cache before it looks at the hard drive. If the resource is found in the cache, the request does not reach the hard drive. When we clean up the cache, the hard drive cache is less useful as the operating system searches for the resource on the hard drive.

In addition, it slows the system down for a few seconds while the cache is cleaned and all resources required by the operating system are reloaded into the hard drive cache.

Now let’s create a shell script to automatically clear the RAM cache every day 2 a.m 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 execution authorization for the clearcache.sh File.

# chmod 755 clearcache.sh

Now you can call the script anytime you need to clear the RAM cache.

Now set a cron so that it clears the RAM cache every day at 2 a.m. Open minded crontab to edit.

# crontab -e

Append, save and exit the bottom line to run 2 a.m Daily.

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

For more information on how to crown a job, see our article on 11 Cron Scheduling Jobs.

Is it a good idea to automatically clear the RAM cache on the production server?

Clear RAM cache on Linux production server?

No! it is not. Imagine a situation where you have the script scheduled to turn around every day. clears the RAM cache 2 a.m. Every day at 2 a.m. the script runs and it clears your RAM cache. One day, for some reason, more users than expected may be online on your website looking for resources from your server.

At the same time, the scheduled script is executed and everything in the cache is deleted. Now all users are fetching data from the hard drive. This leads to a server crash and corruption of the database. So only clear the ram cache when necessary and know your footsteps, otherwise you will be a Cargo Cult system administrator.

How do I clear the swap space on Linux?

If you want to clear the swap space, you can run the following command.

# swapoff -a && swapon -a

You can also add the above command to a cron script above after understanding all of the risks involved.

Now we are going to combine the above two commands into a single command to create a suitable script to clear the RAM cache and swap space.

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

OR

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

After we have tested both of the above commands, we run the command “free -h”Before and after running the script and checking the cache.

That’s all for now, if you enjoyed the article, don’t forget to leave us your valuable feedback in the comments to let us know what you think is a good idea to get the RAM cache and buffer in To delete production and enterprise?

Tags: