Open In App

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

Improve
Improve
Like Article
Like
Save
Share
Report

RAM memory is a very important component of the computer system. it helps store data and code currently used by the operating system. This article will show how to Clear RAM Memory Cache, Buffer, and Swap Space on Linux. In every system, we come across caches that have unwanted files and can harm our system, the same thing happens in the Linux cache; if you want to clear the cache and free some memory, then Linux has many commands to do that.

To Clear RAM memory Cache in Linux:

RAM memory Cache is a type of memory that stores accessed data to speed up the performance of the system and provide a better and faster experience to the user. In all the Linux systems we have three options to clear the cache without interrupting any services or processes. 

Example 1. To Clear PageCache only

Syntax:

sudo sh -c 'echo 1 >  /proc/sys/vm/drop_caches'

The command # free -h will give us the status of the memory. 

clear RAM memory cache in linux

drop_caches is used a clean cache without killing any application, you can run the # free -h command to see the difference between used and free memory before and after clearing the cache.

Example 2. To Clear dentries and inodes

Syntax:

sudo sh -c 'echo 2 >  /proc/sys/vm/drop_caches' 

Example 3. To Clear PageCache, dentries and inodes

Syntax:

sudo sh -c 'echo 3 >  /proc/sys/vm/drop_caches'  

Clearing Buffer/Cache in Linux:

Buffer memory is used for storing data temporarily which is being transferred between the CPU and the hard drives. similarly as RAM memory Cache, buffer can aslo accumulate over time and make our system slow. to clear buffer memory we can use the command below:

sudo sync && echo 1 > /proc/sys/vm/drop_caches
clear buff/cache in linux

 

Here you can see that the buff/cache is being cleared.

Now using Linux Kernel, to free Buffer and Cache in Linux we will Create a shell script to auto clear RAM cache daily, through a cron scheduler task., the command vim script.sh is used to create a shell script “script.sh”

Now in script, you have to add the below syntax:

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

Now to set run permission, to clear ram cache, you have to call the script whenever required, setting a cron to clear RAM caches every day for 3 hours.

# chmod 755 script.sh

To schedule the script so that it run daily, open the crontab editor by running the following command.

# crontab -e

Then add this line at the end of the file.

0 0 * * * /path/to/script.sh

This line will run the script at midnight every day. User has to replace “/path/to/script.sh” with the actual path of the script file. 

Then save and exit the editor. Now our script will finally run automatically executed daily at midnight to free up the RAM cache.

To Clear Swap Space in Linux:

Swap space is used a part of hard drive that is used as a supplement of RAM memory. You can clear the swap space by running the below command

Syntax:

sudo swapoff -a

sudo swapon -a

clear swap space in linux

You can run the # free -h command to see the difference between used and free memory before and after clearing the swap space

Add the above command to a cron script, here we are going to combine these two different commands into one single command, to form a proper script which will help us to clear Swap Space and RAM Cache.

echo 3 >  /proc/sys/vm/drop_caches & &  swapoff -a & &  swapon -a & &  printf ‘\n%s\n’  ‘  ‘ Ram-cache and the swap get cleared’

Now Ram cache and swap will be cleared you can run # free -h command to see

After running the command, you will get output like this 

clear swap space in linux

Frequently Asked Questions

How to clear buffer cache swap in Linux?

If we want to clear the buffer cache and swap in Linux, we can use the following command:

sudo sync && echo 3 | sudo tee /proc/sys/vm/drop_caches

This command syncs the file system and then clears the page cache, dentries, and inodes from the buffer cache. It also releases swap space. But one must note that to execute this command we need to have root privileges.

How to clear the RAM cache in Linux?

To clear ram cache in Linux, we can use the following command:

sudo sync && echo 3 | sudo tee /proc/sys/vm/drop_caches

This command will free up the memory used by the page cache, dentries, and inodes, effectively clearing the RAM cache. But one must note that to execute this command we need to have root privileges.

If we want to verify this, we can use `free -h` command before and after the command.

sudo sync && echo 3 | sudo tee /proc/sys/vm/drop_caches

How to clear swap memory in Linux without reboot?

To clear swap memory in Linux without rebooting, we can follow these steps:

Step 1: Disable swap

sudo swapoff -a

This command disables all swap devices.

Step 2: Clear swap space

sudo dd if=/dev/zero of=/swapfile bs=1M count=<size-in-megabytes>

Replace <size-in-megabytes> with the desired size of our swap space.

Step 3: Recreate swap

sudo mkswap /swapfile
sudo swapon /swapfile

These commands create a new swap file and enable it as the swap space.

By performing these steps, we can effectively clear the swap memory without the need for a system reboot.

How to check RAM cache in Linux?

If we want to check the RAM cache in Linux, we can simply use the following command.

free -h
free -h

free -h

This command will display the memory usage, availability and RAM cache.

What is swap space in Linux?

Swap space in Linux is a designated area on a hard drive or SSD which is used as a virtual memory when the system’s RAM is fully utilized. It temporarily stores inactive data while freeing up RAM for active processes going on. Generally, it prevents crashes due to memory exhaustion. It size varies but it is somehow larger than the RAM size.



Last Updated : 09 Jun, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads