• Categories
    Category
    {{ postCtrl.tags }}
    • {{ category.tag_type }}

      • {{tag.tag_name}}
      • View more
  • Categories
    Category
    {{ postCtrl.tags }}
    • {{ category.tag_type }}

      • {{tag.tag_name}}
      • View more
  • News
  • Tutorials
  • Forums
  • Tags
  • Users
Tutorial News Comments FAQ Related Articles

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

{{postValue.id}}


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

[[email protected] ~]# 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

[[email protected] ~]# sync  echo 1 >  /proc/sys/vm/drop_caches 
[[email protected] ~]# 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

[[email protected] ~]# 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

[[email protected] ~]# sync  echo 2 >  /proc/sys/vm/drop_caches 
[[email protected] ~]# 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

[[email protected] ~]# 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

[[email protected] ~]# sync  echo 3 >  /proc/sys/vm/drop_caches 
[[email protected] ~]# 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.

[[email protected] ~]# vim script.sh
echo " echo 3 >  /proc/sys/vm/drop_caches" 

To set run permission on the script.sh file

[[email protected] ~]# 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.

[[email protected] ~]# 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.

[[email protected] ~]# 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

[[email protected] ~]# swapoff -a
[[email protected] ~]# swapon -a
[[email protected] ~]# 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.

[[email protected] ~]# 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.

[[email protected] ~]# 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
{{postCtrl.cmtErrMsg}}

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

Back To Top!
Rank
User
Points

Top Contributers

1
userNameLinuxHelp
26310

Top Contributers

2
userNamejackson
8070

Top Contributers

3
userNamegabriel
3370

Top Contributers

4
userNamemason
3175

Top Contributers

5
userNamegrayson
3170
Can you help W M ?
How to compare multiple files

Screen Shot 2019-11-09 at 2can someone tell me how to compare more than 2 files? when using the comm command it am only allowed to compare 2 files is there another command I can use?

Desktop Application
  • Linux app
  • Pithos
  • Retrospect
  • Scribe
  • TortoiseHg
  • 4Images
Monitoring Tool
  • Monit
  • Apache Server Monitoring
  • EtherApe 
  • Arpwatch Tool
  • Auditd
  • Barman
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
Web Application
  • Nutch
  • Amazon VPC
  • FarmWarDeployer
  • Rukovoditel
  • Mirror site
  • Chef
Contact Us | Terms of Use| Privacy Policy| Disclaimer
© 2021 LinuxHelp.com All rights reserved. Linux™ is the registered trademark of Linus Torvalds. This site is not affiliated with linus torvalds in any way.