Advance

Tutorial Clear Memory Cache on Ubuntu and Debian

Clear Memory Cache on Ubuntu and Debian
5
(1)

The memory cache plays a crucial role in improving system performance by storing frequently accessed data. However, there are situations where clearing the memory cache becomes necessary. You may want to free up memory for a specific task or troubleshoot memory-related problems. This article will teach you How to Clear Memory Cache on Ubuntu and Debian. Eldernode offers an economical Linux VPS Server, which you can check out the offered package to purchase it.

How to Clear Memory Cache on Ubuntu and Debian

Cache memory is a smaller and faster type of memory that is located closer to the processor. It is used to store frequently accessed data and instructions so that they can be quickly retrieved by the processor. The purpose of cache memory is to reduce the average time it takes to access data from the computer’s main memory (RAM), thereby improving the system’s performance. You may encounter a situation where clearing the memory cache becomes necessary.

Recommended Article: How To Install CMake On Ubuntu And Debian

Clear Memory Cache on Ubuntu and Debian

You can clear the cache of every Linux system without interrupting any processes or services using the following three options:

1- To clear PageCache only, run the following command:

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

2- To clear dentries and inodes, enter the command below:

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

3- To clear pagecache, dentries, and inodes, execute the following command:

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

Please be aware that synchronizing will cause the file system buffer to be flushed. The shell waits for each command in the sequence to finish before executing the next command. Writing to drop_cache cleans the cache without killing the application/service and the echo command does the writing to the file.

The first command we introduced is the safest in enterprise and production and is the best to use if you have to clear your disk cache. This command only clears the PageCache. It is better not to use the third option command until you know what you are doing, because this command clear pagecache, dentries, and inodes.

Schedule the Clear Memory Cache Using Crontab

In Linux, the kernel utilizes a disk cache to improve performance by storing frequently accessed data in memory. This cache is known as the page cache, and it holds copies of data from files that have been read from or written to disk.

When a program requests a resource from a disk, the kernel first checks the page cache to see if it already has a copy of the data. If the data is present in the cache, the kernel can serve the request directly from memory, resulting in faster access times compared to accessing the physical disk.

However, if the cache is cleared or flushed, the kernel will no longer have the data readily available in memory. As a result, subsequent requests for the same data will need to be fulfilled by reading from the disk again, potentially leading to slower access times.

You can create a shell script that automatically clears the RAM cache daily at a certain hour via a scheduled cron job. So, for this, you need to create the clearcache.sh shell script and add the following lines:

!/bin/bash

Please refrain from using “echo 3” in production, as it is not recommended. It is advised to use “echo 1” instead.

echo "echo 3 > /proc/sys/vm/drop_caches"

Now set execute permission on the clearcache.sh file as shown below:

chmod 755 clearcache.sh

This allows you to call the script whenever you need to clear the RAM cache.

So, run the command below to open crontab for editing:

crontab -e

Then add the following line to run it at 2 am daily:

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

Lastly, remember to save and exit.

Clear Swap Space on Ubuntu and Debian

To clear Swap space, just run the below command:

swapoff -a
swapon -a

Next, you should make a proper script to clear RAM Cache and Swap Space by combining both above commands into one single command:

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

Lastly, check the cache after running the script using the command below:

free -h

That’s it!

Recommended Article: Tutorial Install PostgreSQL in Ubuntu 21.04

Conclusion

In this article, we taught you how to clear the memory cache on Ubuntu and Debian. This can be a helpful step in optimizing system performance or resolving memory-related issues. By following the steps outlined in this tutorial, you can easily clear the memory cache, freeing up memory for other tasks.

How useful was this post?

Click on a star to rate it!

Average rating 5 / 5. Vote count: 1

No votes so far! Be the first to rate this post.

View More Posts
Tom Veitch
Eldernode Writer
We Are Waiting for your valuable comments and you can be sure that it will be answered in the shortest possible time.

    Leave Your Comment

    Your email address will not be published.

    We are by your side every step of the way

    Think about developing your online business; We will protect it compassionately

    We are by your side every step of the way

    +8595670151

    7 days a week, 24 hours a day