Linux clear system cache

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

In this article, we will see How to Clear RAM Memory Cache, Buffer, and Swap Space on Linux. In every system we do come across caches that have unwanted files and can harm our system, the same thing happens in Linux cache and if you want to clear the cache and free some memory then Linux has many commands to do that.

To Clear Cache in Linux:

In all the Linux systems we have three options to clear cache without interrupting any services or processes.

Example 1: To Clear PageCache only

Syntax :

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

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:

Example 3: To Clear PageCache, dentries and inodes

Syntax:

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:

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.

Example 4: To Clear Swap Space in Linux

You can clear the swap space by running the below command

Syntax :

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 this two different command 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

Источник

How to Clear Memory Cache in Linux

Sometimes the system goes out of memory due to huge RAM is used by cached objects. In that cases, either you need to increase physical memory in the system or add more swap space. You can also instruct kernel to clear RAM memory cache on system by adding a number in /proc/sys/vm/drop_caches file.

It is safe but not recommended to clear the memory cache on a Linux system. Clearing the Memory cache in Linux systems slows 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.

Читайте также:  Что такое windows 10 rs2 rs3

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:
  • Clear dentries and inodes only in cache memory
  • Clear page cache only in cache memory

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 with 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:

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

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

Источник

How To Clear .Cache Folders and Free Up Space On Your Linux PC

Clean up your cache in a few easy steps!

GNU/Linux has implemented efficient storage management for its users. But have you noticed your Linux system running out of space, filled with unused packages you installed months or years ago? How do find them and remove them? Here are a few valuable tips!

Cache File Location

Cache files are stored in /home/username/.cache which mostly consists of your browser’s data, IDE’s (if you use any ) and other software. Each user has its own data and this can build up exponentially . In order to clear it all it’s recommended to have Disk usage analyzer installed.

Disk Usage Analyzer

Disk Usage Analyzer, formerly known as Baobab, is a graphical disk usage analyzer for the GNOME desktop environment. It was part of gnome-utils, but has been a standalone application since GNOME 3.4.

The software gives the user a graphical representation of a disk drive’s contents. The interface allows for selection of specific parts of filesystem so a single folder, the entire filesystem, and even remote folders and filesystems can be scanned and listed at the folder level.

The graphical representation can be switched between a ‘Rings’ chart and a ‘Treemap’ chart to better suit the content being viewed.

Installation

If you don’t already have Disk Usage Analyzer installed, you first need to install the baobab package:

Clear Cache files and folders

Click on home for your user data .

Click on your second disk i.e Vostro-14-3468 as shown in the image below .

Читайте также:  Dart windows 10 iso

Note: Names will be different on your own computer.

Here, the other disks shown as New Volume / 262 GB volume are formatted disks for a dual boot system, in my case Ubuntu 20.10 and Windows 10.

As you click on it, the Disk Usage Analyzer will start to scan your directory for files and folders. Be patient and allow it to finish. After Disk Usage Analyzer is done scanning your Linux PC for files it shows the live illustration of Linux disk usage as the form of a pie chart.

To see updated changes in your filesystem, you’ll need to refresh the scan as it won’t track these storage changes “live.”

Look in the folder tree structure for .cache, and click on it .

Once you click on the .cache folder, the disk usage analyzer will show you the space it’s consuming graphically on the right side .

  • Find the sub folders inside it which you wish to delete .
  • Right-click each one
  • Select Move to trash / bin
  • Now empty your trash / bin

And you are done!

Do not worry about deleting these folders. It is totally safe and the software will recreate them if and when it needs to.

WARNING!

Do not delete anything from your usr and var directories. usr is used for “user programs”. Usually your package manager installs all the binaries, shared files etc. from all programs here (except config files, which go to /etc).

You can check /usr/bin for binaries, /usr/share for shared files (media, etc), /usr/share/doc for documentation.

There is also an /opt folder, where there are “other” binary programs or programs installed from sources other than the default package manager. Some programs like that (usually compiled) also go to usr/local

/var is usually used for log files, ‘temporary’ files (like mail spool, printer spool, etc), databases, and all other data not tied to a specific user.

Logs are usually in /var/log databases in var/lib (mysql — var/lib/mysql ), etc.

If you liked this article, please follow the author on medium !

Источник

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.

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.

2. Clear dentries and inodes.

3. Clear pagecache, dentries, and inodes.

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.

Читайте также:  Браузер usb для windows

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.

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.

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.

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.

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?

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

We are thankful for your never ending support.

Источник

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