Like any other operating system, GNU/Linux already implements memory management effectively and more. However, if any process is eating into your memory and you want to clear it, Linux provides a way to refresh or clear the RAM cache.

How do I clear the cache in Linux? Every Linux system has three options to clear the cache without interrupting any processes or services.

1. Only cached pages are cleared

sync; Echo 1 > /proc/sys/vm. drop_caches 2 to clear directory entries and inodes

sync; Echo 2 > /proc/sys/vm. drop_caches 3, clear, cache pages, directory entries and inodes

sync; Echo 3 > /proc/sys/vm/drop_caches

Sync will refresh the file system cache. Run the; command. Delimited, sequentially executed, the shell waits to terminate before each command in the sequence is executed. As mentioned in the kernel documentation, writing to drop_cache clears the cache without killing any applications/services, and the echo command does the writing.

If you must clear the disk cache, the first command is the safest in enterprise and production environments, “… Echo 1 >…” Only the page cache is cleared.

The third option above is not recommended in a production environment “… Echo 3 > “unless you know exactly what you’re doing, because it clears cache pages, directory entries, and inodes.

Is it a good idea to use the kernel to release buffers and caches on Linux?

When you request a lot of Settings you want to check, you may need to clear the cache if it is actually specifically implementing extensive benchmarking on I/O. You can remove the cache as shown above without restarting the system or shutting down.

Linux is designed to look for disks in the disk cache before it looks for them. If it finds the resource in the cache, the request does not reach disk. If we clean the cache, the disk cache will be useless and the system will look for resources on disk.

In addition, it slows down the system when the cache is cleared, and the system reloads each requested resource back into the disk cache.

Now, we’ll create a shell script through a Cron task scheduler to automatically clear the RAM cache at 2 p.m. every day.

Create a shell script clearCache.sh and add the following lines to it:

#! /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"Copy the code

Set the execute permission for the clearCache. sh file

Sh Now, when you need to clear the RAM cache you just call the script.

Now set up a scheduled task to clear RAM cache every day at 2pm, turn on crontab for editing.

# crontab -e Add the following line, save and exit.

0 3 * * * /path/to/ clearCache. sh For more details on how to create a scheduled task, see our article 11 Cron Scheduling Jobs.

Is it a good idea to automatically flush RAM on your servers in production?

No! It is not. Consider a situation where you have scheduled a script to clear the RAM cache every day at 2pm. Every day at 2pm this script will execute and flush your RAM cache. At any time of day, users of your site may be online more than expected and request resources from your server. At the same time, the scheduler runs the script and clears everything in the cache. When all users are reading data from disk, this can cause the server to crash and corrupt the database.

So, clearing the cache is only necessary and expected, otherwise you’re a Cargo Cult System Administrator.

How do I clear swap space on Linux? If you want to clear swap space, you can run the following command:

In addition, you may want to add the command above to CRon once you understand the risks.

Now, let’s combine the above two commands into a single command that writes the correct script to clear both the 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'
Copy the code

or

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

Before testing the above command, we run “free-m” and execute the script to check the cache.