• Categories
    Category
  • Categories
    Category
  • News
  • Tutorials
  • Forums
  • Tags
  • Users
Tutorial Comments FAQ Related Articles

How to clear RAM Memory Cache, Buffer & Swap Space on Linux

202


Clearing RAM Memory Cache, Buffer and Swap Space on Linux

Memory management like Clear RAM Memory Cache, Buffer and Swap Space in Linux will be discussed in this article.

To Clear Cache in Linux

Generally all the Linux System will have three options to clear cache without interrupting any services or processes.

To Clear PageCache only

[root@linuxhelp ~]# free -h
                     total       used       free     shared    buffers     cached
Mem:                  987M       923M        64M       5.1M         0B       240M
-/+ buffers/cache:    682M       304M
Swap:                 2.0G        15M       1.9G

[root@linuxhelp ~]# sync  echo 1 >  /proc/sys/vm/drop_caches 
[root@linuxhelp ~]# free -h
                      total       used       free     shared    buffers     cached
Mem:                   987M       583M       403M       5.1M         0B        38M
-/+ buffers/cache:     544M       442M
Swap:                  2.0G        15M       1.9G

To Clear dentries and inodes

[root@linuxhelp ~]# free -h
                      total       used       free     shared    buffers     cached
Mem:                   987M       585M       401M       5.1M         0B        40M
-/+ buffers/cache:     545M       442M
Swap:                  2.0G        15M       1.9G

[root@linuxhelp ~]# sync  echo 2 >  /proc/sys/vm/drop_caches 
[root@linuxhelp ~]# free -h
                       total       used       free     shared    buffers     cached
Mem:                    987M       576M       410M       5.1M         0B        42M
-/+ buffers/cache:      533M       453M
Swap:                   2.0G        15M       1.9G

To Clear PageCache, dentries and inodes

[root@linuxhelp ~]# free -h
                     total       used       free     shared    buffers     cached
Mem:                  987M       584M       402M       5.1M         0B        50M
-/+ buffers/cache:    533M       453M
Swap:                 2.0G        15M       1.9G

[root@linuxhelp ~]# sync  echo 3 >  /proc/sys/vm/drop_caches 
[root@linuxhelp ~]# free -h
                     total       used       free     shared    buffers     cached
Mem:                  987M       572M       414M       5.1M         0B        38M
-/+ buffers/cache:    534M       453M
Swap:                 2.0G        15M       1.9G

sync command will flush the file system buffer. To drop_cache will clean cache without killing any application, echo command is doing the job of writing to file.

" ...echo 1 > &hellip ." will clear the PageCache only. It is not recommended to use third option above

" ...echo 3 > " can clear PageCache, dentries and inodes.

Using Linux Kernel, to free Buffer and Cache in Linux. Create a shell script to auto clear RAM cache daily, through a cron scheduler task. To Create a shell script script.sh by adding the below lines.

[root@linuxhelp ~]# vim script.sh
echo " echo 3 >  /proc/sys/vm/drop_caches" 

To set run permission on the script.sh file

[root@linuxhelp ~]# chmod 755 script.sh

To clear ram cache, you may call the script whenever required.

Setting a cron to clear RAM cache everyday 2 hours.

[root@linuxhelp ~]# crontab -e
Append the following line, save and exit to execute it at 2 hours daily.

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

To clear automatically the RAM cache on production server

On the scheduled time, the script executes and clear everything in cache. Now all users are fetching data from disk. It will result in server crash and corrupt the database.

To Clear Swap Space in Linux

If you need to clear Swap space, run the following command.

[root@linuxhelp ~]# free -h
                     total       used       free     shared    buffers     cached
Mem:                  987M       602M       384M       5.1M         0B        71M
-/+ buffers/cache:    531M       455M
Swap:                 2.0G        15M       1.9G

[root@linuxhelp ~]# swapoff -a
[root@linuxhelp ~]# swapon -a
[root@linuxhelp ~]# free -h
                    total       used       free     shared    buffers     cached
Mem:                 987M       615M       371M       8.1M        12K        72M
-/+ buffers/cache:   543M       443M
Swap:                2.0G         0B       2.0G

Add the above command to a cron script, after understanding all the risk. Here we will combine both the commands into one single command, to form a proper script to clear Swap Space and RAM Cache.

[root@linuxhelp ~]# echo 3 >  /proc/sys/vm/drop_caches & &  swapoff -a & &  swapon -a & &  printf ' 
%s
'  ' Ram-cache and the swap get cleared' 

Now the Ram-cache and the swap gets cleared.

$ su -c " echo 3 > ' /proc/sys/vm/drop_caches'  & &  swapoff -a & &  swapon -a & &  printf ' 
%s
'  ' Ram-cache and the swap get cleared’  root

After testing above commands, we will execute " free -h" command before and after executing the script and then we will check the cache.

[root@linuxhelp ~]# free -h
                       total       used       free     shared    buffers     cached
Mem:                    987M       588M       399M       8.1M       1.4M        43M
-/+ buffers/cache:      543M       444M
Swap:                   2.0G         0B       2.0G

Tags:
mason
Author: 

Comments ( 0 )

No comments available

Add a comment

Frequently asked questions ( 5 )

Q

How to check swap space without using free or swapon command?

A

You can read swaps file, cat /proc/swaps to check swap size

Q

what are the known command to check ram usage?

A

you can use free, /proc/meminfo, vmstat, top and htop commands to check ram usage

Q

How can I know other informations about ram?

A

you can know other informations about ram type by using the following command,

dmidecode --type 17

Q

How to To Clear Page Cache only?

A

To Clear Page Cache use the following command

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

Q

How to To Clear Swap Space in Linux?

A

If you need to clear Swap space, run the following command.

free -h

Related Tutorials in How to clear RAM Memory Cache, Buffer & Swap Space on Linux

Related Tutorials in How to clear RAM Memory Cache, Buffer & Swap Space on Linux

How to clear RAM Memory Cache, Buffer & Swap Space on Linux
How to clear RAM Memory Cache, Buffer & Swap Space on Linux
Jun 25, 2016
How to configure Caching DNS Server in Ubuntu
How to configure Caching DNS Server in Ubuntu
May 4, 2016
How to Cache-Control in Nginx
How to Cache-Control in Nginx
Apr 5, 2018
How to allow users to access the caches at specific times using Squid proxy server
How to allow users to access the caches at specific times using Squid proxy server
Oct 20, 2017

Related Forums in How to clear RAM Memory Cache, Buffer & Swap Space on Linux

Related Forums in How to clear RAM Memory Cache, Buffer & Swap Space on Linux

Squid
oliver class=
how to clear squid cache
Feb 7, 2017
Cache
madridgenim class=
how to clear powerdns cache
Mar 14, 2018
npm
robert class=
Npm ERR! Please run npm cache clean
Oct 1, 2021
Cache
nicholas class=
How to clear the cache in linux
Feb 8, 2017
Cache
james class=
How to clear cache in particular directory
Feb 7, 2017
Back To Top!
Rank
User
Points

Top Contributers

userNamenaveelansari
135850

Top Contributers

userNameayanbhatti
92510

Top Contributers

userNamehamzaahmed
32150

Top Contributers

1
userNamelinuxhelp
31040

Top Contributers

userNamemuhammadali
24500
Can you help legeek ?
Installation of the call center module

hello

I wish to install a call center in virtual with issabel, I downloaded the latest version of it , but I don' t arrive to install the call center module in issabel. please help me

thanks!

Networking
  • Routing
  • trunk
  • Netmask
  • Packet Capture
  • domain
  • HTTP Proxy
Server Setup
  • NFS
  • KVM
  • Memory
  • Sendmail
  • WebDAV
  • LXC
Shell Commands
  • Cloud commander
  • Command line archive tools
  • last command
  • Shell
  • terminal
  • Throttle
Desktop Application
  • Linux app
  • Pithos
  • Retrospect
  • Scribe
  • TortoiseHg
  • 4Images
Monitoring Tool
  • Monit
  • Apache Server Monitoring
  • EtherApe 
  • Arpwatch Tool
  • Auditd
  • Barman
Web Application
  • Nutch
  • Amazon VPC
  • FarmWarDeployer
  • Rukovoditel
  • Mirror site
  • Chef
Contact Us | Terms of Use| Privacy Policy| Disclaimer
© 2025 LinuxHelp.com All rights reserved. Linux™ is the registered trademark of Linus Torvalds. This site is not affiliated with linus torvalds in any way.