Linux clear cache and buffer

How to Clear the Buffer, Cache, and Swap Memory in Linux

Clearing the buffer, cache, and swap memory in Linux can be done easily with a few simple commands.

If you’ve ever used the free -m command, you’re probably familiar with the output, which provides information about the system’s total memory, used memory, free memory, shared memory, and buffer/cache memory.

Apart from buffer/cache memory, all are self-explanatory, and we all know how to clear the memory that has been used, but I found there was some confusion around how to clear buffer/cache memory, so I decided to write an article about it.

Here we will discuss how to clear buffer/cache and swap memory in Linux, why it is important to do so, and the best methods to clear this type of memory.

How to Clear or Empty Buffer, Cache, and Swap Memory in Linux

As I previously stated, we all know how to clear used RAM space, but we rarely scratch the surface of what buffer and cache memory are and how to clear them in Linux, so before I show you the command, let me define buffer and cache memory, which will help you understand why we need to clear them.

What is Buffer and Cache Memory?

The terms buffer and cache memory are often used synonymously because they both serve the same function of storing frequently accessed data temporarily for quick access.

If we talk specifically about buffer memory, it is the part of the RAM that is used to temporarily store metadata information about files that are being read from or written to a disk, allowing the data to be accessed quickly without having to go through the slower disk I/O operations.

While cache memory is a form of fast access memory that stores recently used instructions and data (with the actual contents of files or block devices) so that it can be quickly accessed when the same data is requested.

I cannot explain the above abstract concept in greater detail here, so I recommend you read this article for a more complete picture of buffer/cache memory.

When you should manually clear buffers and cache memory

As you know, memory buffers and caches are used to store data and instructions so they can be accessed quickly. However, their size is limited, so Linux is designed in such a way that it tries to keep as many of the most recently used instructions and data in the memory buffers and caches as possible by removing old data and instructions that haven’t been used in a while.

But sometimes you need to manually clear the buffers and caches in order to free up space for other memory-intensive processes.

To do so, you can use a set of commands that will force the system to clear out the memory buffers and caches and reset them to their default states without affecting any of the other processes currently running.

Читайте также:  But does it run linux

You will first see how to clear the disc cache, then how to clear the dentries and inodes cache, and finally how to clear the page cache, dentries, and inodes on Linux.

Clear Page Cache or Disk Cache on Linux

I would suggest you first check the current status of memory by executing the below command, which will help you get an idea of how much free memory is available and also how much buffer and cache memory are currently being used by the system.

The output of the above command is “Show me 3 GB of space that is occupied by buffer/cache memory, so now let free some space by running the next command.”

Buffer memory stats

To clear page cache or disk cache on Linux, you can use the below command, which will clear all of the system’s page cache (used by the Linux kernel during disk reads and writes) without affecting the buffer cache.

$ sudo sync; echo 1 | sudo tee /proc/sys/vm/drop_caches
  • sudo sync : command is used to synchronise the data in memory with that on disk, ensuring any changes to disk are committed before flushing them out.
  • echo 1 : parameter followed by the sync command can be used to clear the page cache.
  • sudo tee : command will write the data provided by the previous command to /proc/sys/vm/drop_caches.

To get the result of the above command, execute free -m , which will print the memory usage of the system in MB.

Result shows the output of the above command

Command to Clear Dentries and Inodes Cache

You already learned how to clear page cache with echo 1, so now you’ll use echo 2 to clear dentries (which are data entries in a computer’s filesystem that refer to both directories and files) and inodes.

To do this, run the below command:

$ sudo sync; echo 2 | sudo tee /proc/sys/vm/drop_caches

Command to Clear out Page Cache, Dentries, and Inodes

The next command will remove everything like page cache, dentries, and inodes, which are not recommended to run on production servers unless you are aware of the impact, but you can run it to free up memory on your Linux server.

So to clear page cache, dentries, and inodes on a Linux server, run the following command:

$ sudo sync; echo 3 | sudo tee /proc/sys/vm/drop_caches

Command to Clear out Swap Memory

There might be a situation where you need to clear up the swap memory to free up more memory on the system. In that case, you can use the below command, which will deallocate the swap memory and allow you to use the system resources effectively.

For this, you need to switch your account to root and execute the command.

# swapoff -a && swapon -a

This command is a powerful way to clear up the swap memory and free up RAM on the system.

Wrap up

That’s all for this article, where you learn how to clear the buffer cache and swap memory. You can also configure the above command as an alias or short script to run automatically when needed using the curl command.

If you have any questions or comments, please feel free to leave them in the comments section below.

Till then, put your feet up and relax. As always, the next article will be up shortly.

A man with a tech effusive who has explored some of the amazing technology stuff and is exploring more. While moving towards, I had a chance to work on Android development, Linux, AWS, and DevOps with several open-source tools.

Источник

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

Like any other operating system, GNU/Linux has implemented memory management efficiently and even more than that. But if any process is eating away your memory and you want to clear it, Linux provides a way to flush or clear ram cache.

Читайте также:  Посмотреть чем занят файл linux

How to Clear Cache in Linux?

Every Linux System has three options to clear cache without interrupting any processes or services.

1. Clear PageCache only.

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

2. Clear dentries and inodes.

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

3. Clear pagecache, dentries, and inodes.

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

Explanation of the above command.

sync will flush the file system buffer. Command Separated by “;” run sequentially. The shell waits for each command to terminate before executing the next command in the sequence. As mentioned in the kernel documentation, writing to drop_cache will clean cache without killing any application/service, command echo is doing the job of writing to file.

If you have to clear the disk cache, the first command is safest in enterprise and production as “. echo 1 > ….” will clear the PageCache only. It is not recommended to use the third option above “. echo 3 >” in production until you know what you are doing, as it will clear pagecache, dentries, and inodes.

Is it a good idea to free Buffer and Cache in Linux that might be used by Linux Kernel?

Free Buffer and Cache in Linux

When you are applying various settings and want to check, if it is actually implemented specially on the I/O-extensive benchmark, then you may need to clear the buffer cache. You can drop cache as explained above without rebooting the System i.e., no downtime required.

Linux is designed in such a way that it looks into the disk cache before looking onto the disk. If it finds the resource in the cache, then the request doesn’t reach the disk. If we clean the cache, the disk cache will be less useful as the OS will look for the resource on the disk.

Moreover, it will also slow the system for a few seconds while the cache is cleaned and every resource required by OS is loaded again in the disk cache.

Now we will be creating a shell script to auto clear RAM cache daily at 2 am 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 execute permission on the clearcache.sh file.

Now you may call the script whenever you are required to clear the ram cache.

Now set a cron to clear RAM cache every day at 2 am. Open crontab for editing.

Append the below line, save and exit to run it at 2 am daily.

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

For more details on how to cron a job, you may like to check our article on 11 Cron Scheduling Jobs.

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

Clear RAM Cache on Linux Production Server?

No! it is not. Think of a situation when you have scheduled the script to clear ram cache every day at 2 am. Every day at 2 am the script is executed and it flushes your RAM cache. One day for whatsoever reason may be more than expected users are online on your website and seeking resources from your server.

At the same time, the scheduled script runs and clears everything in the cache. Now all the users are fetching data from the disk. It will result in a server crash and corrupt the database. So clear ram-cache only when required, and known your footsteps, else you are a Cargo Cult System Administrator.

Читайте также:  Device driver ubuntu linux

How to Clear Swap Space in Linux?

If you want to clear Swap space, you may like to run the below command.

Also, you may add the above command to a cron script above, after understanding all the associated risks.

Now we will be combining both above commands into one single command to make a proper script to clear 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' OR $ su -c "echo 3 >'/proc/sys/vm/drop_caches' && swapoff -a && swapon -a && printf '\n%s\n' 'Ram-cache and Swap Cleared'" root

After testing both the above commands, we will run the command “free -h” before and after running the script and will check the cache.

That’s all for now, if you liked the article, don’t forget to provide us with your valuable feedback in the comments to let us know, what you think is a good idea to clear ram cache and buffer in production and Enterprise?

Источник

How to Clear Memory Cache in Linux

Memory cache is a critical component of any Linux system, helping to improve performance by storing frequently accessed data in a fast and easily accessible location. However, the cache can also become bloated over time, leading to degraded performance. Fortunately, it is relatively easy to clear the memory cache in Linux.

Clearing the memory cache is safe but not recommended every time. It can slow down the system performance as reading files from memory is much faster than persistent disk. Since it discards cached objects from memory, it may cost a significant amount of I/O and CPU to recreate the dropped objects.

This tutorial will help you to clear the memory cache on Linux/Unix system via the command line.

How to Clear Memory Cache on Linux

There are three options available to clear the memory cache in Linux. Choose one of the below options to flush the Linux system cache memory as per your requirements.

    Clear PageCache, dentries and inodes in cache memory. In short it will clear all the memory cache:

sync; echo 3 | sudo tee /proc/sys/vm/drop_caches 
sync; echo 2 | sudo tee /proc/sys/vm/drop_caches 
sync; echo 1 | sudo tee /proc/sys/vm/drop_caches 

Here the first command sync is used to synchronize all the in-memory cache files to the persistent storage. The next command is separated with a “;”. Once the first command is completed, the next command will be triggered to clear cache memory.

Scheduleng the Clear Memory Cache usiing Crontab

You can also schedule a corn job to clear the cache on a regular basis. Schedule the following in system crontab to automatically flush cache memory at a regular interval.

Open a terminal and execute ‘crontab -e’ command to edit crontab:

Append below entry to the file:

0 10 * * * sync; echo 3 | sudo tee /proc/sys/vm/drop_caches

The above cron will execute on every hour and flushes the memory cache on your system.

On the production servers, it is not recommended to schedule a clear cache command. It can lead to data corruption or data loss. So beware before running the above command in a production environment.

How to find Cached Memory in Linux

Use free command to find out cache memory uses by Linux system. The output of the free command is like below

Output
total used free shared buffers cached Mem: 16050 15908 142 0 120 12953 -/+ buffers/cache: 834 15216 Swap: 0 0 0

Here the last column is showing cached memory (12953 MB) on Linux system. The -m option is used to show output MB’s.

Источник

Оцените статью
Adblock
detector