Clear RAM Memory Cache, Buffer, and Swap Space on Linux with 3 steps intechost

Introduction

GNU/Linux operating system has been executing the memory management quite efficiently. One can state that it has implemented memory administration even more than some of the other leading operating systems. The best part of the Linux operating system is that it renders the way to flush or free the ram cache if any process eats away your system’s memory unnecessarily. In this article, we will provide insights into the ways employing which you can free or clear RAM cache Linux, Clear Cache in Linux, buffer, and swap space. Begin diving into the subsequent sections to get the details.

How to Clear RAM Cache in Linux?

With every kind of Linux system, you get three different choices in order to clear cache Linux. With each option, you do not have to intervene in any processes or services. They are explained below along with the command you need to use to implement them –

1.

First choice you have is to Clear PageCache only –

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

1

2. The second option you have is to Clear dentries and inodes –

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

Clear RAM Memory Cache, Buffer, and Swap Space on Linux with 3 steps intechost

3. The third and the last choice you get is to Clear pagecache, dentries, and inodes –

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

PsS ZygYG8ZgyWFKho8z8TeZGVvF6HvX8AJT9 mGd aGwplwik8C9G8bnWbeIXVxxqr977JGSZ AekfPzHckViUAbNMiTghcqy93Tk emQdX4ypw RvBf6VUY EpJms1xuP fF

In the above commands, sync functions to clean the file system buffer. Further, the command that is separated by “;” operates sequentially. Until each command is terminated, the shell waits before it implements the subsequent command in the sequence. Moreover, as the kernel documentation states, the drop_cache helps in cleaning the cache without destroying any application or service. And, the command echo does the task of writing to file.

You must note here that for clearing the disk cache, you should make use of the first command as it serves as the safest option in enterprise and production. The “…echo 1 > ….” plays a role in clearing PageCache only. On the other hand, you should not employ the third option “…echo 3 >” in production when you are not completely aware of what you are doing. This is because it flushes out the page cachedentries, as well as inodes.

Free Buffer and Cache in Linux

There are times when you need to confirm whether the various settings that you apply in your system actually get implemented or not. This becomes more significant when it comes to the I/O-extensive benchmark. For confirming or checking the changes, you are required to clear the buffer cache. You have the option to drop cache in a way that we have discussed in the previous section without rebooting the System. This needs no downtime.

However, Linux operating system is created in such as way that it considers looking into the disk cache before disk. Thus, in case, Linux discovers any resource in the cache, your request can not reach the disk. So, cleaning the buffer and cache will enable your operating system to look for the resource on the disk as the disk cache becomes less beneficial. Further, with this, your system also happens to slow down for a few seconds when the cleaning of the cache is done. And, each resource that your operating system needs get loaded again in the disk cache.

Here we will teach you to create a shell script that can auto free the RAM cache on a daily basis at 2 am through a cron scheduler task.

First of all, you need to create a shell script clearcache.sh and append the following lines.

#!/bin/bash

bOBxIM0dXnL5Xu13rCquVuQXOyk10 OmW8YWm 7vIrHPzjbiXl3cmvdtCm1IzeVyck1WHvs8otdoj4Hc6QX1j1mvMNsOX24dlocUPvUVCqY 45xtD
1adff3WFwfdkZ0j56j6GcStjES23mk6bx0n6ymFvH2dUt2dPzgLGahR 6ncJnx5dAmKi22zLINM698 WS 8sCjBmvWdbIOziEUYv5plph3N170tN Z7NtbVKYQE7bPPY6hYSdRhJ

# 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”

Next, you need to set execute permission on the clearcache.sh file utilizing the following command –

# chmod 755 clearcache.sh

JsayN8A161YURORbomh6lWmM gpRDz8Kw U jlk6htrsqgerpKZZ5QNXkC JwJ4QaabFHpzC0 fcV4 v TnybPRSJMkmJH59WA IcwhkqyDl HMmMV7jjqnisPPAPddrrtNOqZFS

After entering this command, you can call the script as and when you wish to you free the ram cache. Further, you can set a cron to clear the RAM cache on a daily basis at 2 am. For doing so, you have to open crontab for editing and enter the command as –

# crontab -e

Next, you have to append the below line. And then, save and exit to let it run at 2 am every day.

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

KwLKkF3eiHlJsq5N2223uzQX35eZrU4S6twiZBDkgN2KuXXR6IDX1peCVzwab5XWa56HKFU9cX1G5 2h8 nn7js5JPTIC oJQcyOm7sltq2AzTJqVetW2lrXz1KSuhDyYvGzrsLc

Clear RAM Cache on Linux Production Server?

No, you should not consider clearing the RAM cache on the Linux production server. For instance, if you get to schedule the script in order to clear ram cache on a daily basis at 1 am. Then, your script will execute every day at 1 am and flush your RAM cache. There can be times when someday for any reason more than expected users come online on your website to request resources from your server. And simultaneously, your scheduled script will run and free everything in the cache. So, at this time, when users attempt to fetch data from your disk, your server will crash and can corrupt the entire database. Thus, to avoid being a Cargo Cult System Administrator, you must consider clearing ram-cache only when needed.

How to Clear Swap Space in Linux?

In this section, you will know how to clear swap memory in Linux. In order to clear or flush Swap space, you can simply enter the following command –

# swapoff -a && swapon -a

guei4zFUUQQG1WzFPNH98MMzglVE

Moreover, you can also append this command to a cron script once you get aware of all the related risks.

For combining both the commands into a single one for making an apt script that can clear RAM Cache as well as Swap Space, you need to enter as –

# 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

AhxGKtx7D rJA7tIE2GTto5 1pD87r8k7k637JDTiUiMIhn9NujjT52aeY56gmqUrt8adcvCwjuapjYoHaBP2T8 ghPrAtg y4McWP yvQNZGguo5VmUmBTDXm7QBf2zl3zzju5a
PNs fxHVAxrUgRQmNKr2dFphD6VpUeXrpyq

Once you get to test both the above commands, you can execute the command “free -h” before and after running the script and check the cache.

Conclusion

While following this guide thoroughly, you must be able to comprehend how to clear cache in Linux, free buffer and cache, clear RAM cache, and flush swap space. If you encounter any issue or difficulty while performing any of these tasks on your system, you can reach out to us. Our Hostbillo technical professionals are available 24/7 to render the immediate response and expert solutions via different communication mediums.

Leave a Reply

Your email address will not be published. Required fields are marked *