Linux free memory cached

How to free RAM and cache in Linux with one command

There are a few ways by which you can clear RAM in Linux like from system monitor and htop. In this tutorial you will know how to clear the ram and cache using one command.

How to check how RAM info in terminal

To check the current state of RAM in terminal just type ‘free’ use -m switch to display output in megabytes.

total used free shared buff/cache available Mem: 3760 2363 144 489 1252 669 Swap: 2047 1079 968

Command to clear the cache

To clear the cache type the command in terminal and it requires root permission.

free -m && sync && echo 3 > /proc/sys/vm/drop_caches && free -m 
total used free shared buff/cache available Mem: 3760 2363 144 489 1252 669 Swap: 2047 1079 968 bash: /proc/sys/vm/drop_caches: Permission denied 

To run this command in root type

total used free shared buff/cache available Mem: 3760 2408 173 503 1178 610 Swap: 2047 1105 942 total used free shared buff/cache available Mem: 3760 2414 458 494 887 616 Swap: 2047 1105 

It will show before and after effect of command.

How to make the command short

This command may look lengthy and hard to remember, to make it short follow the steps below.

Create a file with name free_ram we are using vim as a text editor you can use any of your choice.

And write the command in this file.

free -m && sync && echo 3 > /proc/sys/vm/drop_caches && free -m 

Now give this file executable permissions

Now whenever you need to clear your cache or buffer or free some RAM just open this file as

Читайте также:  Acpi драйвер для linux

Open this file inside root. Use sudo su to enter root.

Источник

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.

# 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.

Читайте также:  Docker alpine linux install

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.

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.

Читайте также:  Linux audio not working

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 see buffered and cached data

@strugee Not a duplicate imo. The linked question just explains what the terms mean. It does not address any of the two questions.

2 Answers 2

The memory represented by «buffers/cache» in free is your disk and filesystem cache, respectively, which Linux caches to speed up reading data from your disk, as hitting the disk is generally a fairly slow way to access data repeatedly. As such, they are cached in memory, and transparently served from there if available.

You can see which blocks are currently in your cache by using fincore . Here is an example from the project page:

# fincore --pages=false --summarize --only-cached * stats for CLUSTER_LOG_2010_05_21.MYI: file size=93840384 , total pages=22910 , cached pages=1 , cached size=4096, cached perc=0.004365 stats for CLUSTER_LOG_2010_05_22.MYI: file size=417792 , total pages=102 , cached pages=1 , cached size=4096, cached perc=0.980392 stats for CLUSTER_LOG_2010_05_23.MYI: file size=826368 , total pages=201 , cached pages=1 , cached size=4096, cached perc=0.497512 stats for CLUSTER_LOG_2010_05_24.MYI: file size=192512 , total pages=47 , cached pages=1 , cached size=4096, cached perc=2.127660 stats for CLUSTER_LOG_2010_06_03.MYI: file size=345088 , total pages=84 , cached pages=43 , cached size=176128, cached perc=51.190476 

As for how to clear them, from man 5 proc :

  • To free pagecache, use: echo 1 > /proc/sys/vm/drop_caches
  • To free dentries and inodes, use: echo 2 > /proc/sys/vm/drop_caches
  • To free pagecache, dentries and inodes, use: echo 3 > /proc/sys/vm/drop_caches

You generally don’t want to flush the cache, as its entire purpose is to improve performance, but for debugging purposes you can do so by using drop_caches like so (note: you must be root to use drop_caches, but sync can be done as any user):

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

Источник

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