Linux How To Free Wasted Memory RAM in Buffers

We all know Linux is known for good memory management but is it really?  It seems all on its own with hardly anything running that you can come back in days or weeks and find that almost all of your RAM is used!

And many will say "no don't worry it's buffers for optimization" but it doesn't seem to help because what is in buffers is not available to use for new programs running or ones that allocate more RAM as far as I can tell.

The reason I know this is because you can see that SWAP will get used so if there was enough RAM not eaten by buffers it should be freed for new or growing memory allocations but it is not.

By freeing your RAM in Linux you will often realize performance benefits and lower loads in many server based scenarios.  A lot of admins will think they need more RAM when they don't.  In fact the cache/buffers in Linux will eat up all of your RAM no matter how much you have.   The kernel documentation warns you could lower your performance but this to me only makes sense for a basic user who doesn't generate a lot of IO, but for servers and heavy reads you would be better to never have the cache.

The key is Linux's vm/drop_caches which I totally disagree with (but I am not aware of anyway to actually disable the feature).  Basically it starts caching and eating up your RAM.  A lot of people say "it is fine don't worry it is normal".  But it is not, after some days, or weeks depending on your usage you could be swapping whether you have 32GB, 64GB or even hundreds of GB of RAM because of the page caching/buffers resulting from a lot of IO.

To free pagecache:
    echo 1 > /proc/sys/vm/drop_caches
To free reclaimable slab objects (includes dentries and inodes):
    echo 2 > /proc/sys/vm/drop_caches
To free slab objects and pagecache:
    echo 3 > /proc/sys/vm/drop_caches

So how do we fix this or clear out Linux's memory buffers to free RAM?

I would recommend running the command as a cronjob every minute or at least every hour depending on your usage.

[ ~]# free -m
             total       used       free     shared    buffers     cached
Mem:          3851       3738        113          0       3269        222
-/+ buffers/cache:        246       3605
Swap:         2999          0       2999

The least invasive is to free page cache by echoing 1:

I find that on a Desktop machine that it will actually kill processes you may be using if you use 3.
echo 1 > /proc/sys/vm/drop_caches

The most invasive is to free page cache and slab:
In plain English I have found this to kill programs I have been using.

echo 3 > /proc/sys/vm/drop_caches

[ ~]#
[ ~]#
[ ~]# free -m
             total       used       free     shared    buffers     cached
Mem:          3851        146       3705          0          0          9
-/+ buffers/cache:        137       3714
Swap:         2999          0       2999

Now we can see that we have our RAM back!


From the kernel documentation:



Writing to this will cause the kernel to drop clean caches, as well as
reclaimable slab objects like dentries and inodes.  Once dropped, their
memory becomes free.

To free pagecache:
    echo 1 > /proc/sys/vm/drop_caches
To free reclaimable slab objects (includes dentries and inodes):
    echo 2 > /proc/sys/vm/drop_caches
To free slab objects and pagecache:
    echo 3 > /proc/sys/vm/drop_caches

This is a non-destructive operation and will not free any dirty objects.
To increase the number of objects freed by this operation, the user may run
`sync' prior to writing to /proc/sys/vm/drop_caches.  This will minimize the
number of dirty objects on the system and create more candidates to be

This file is not a means to control the growth of the various kernel caches
(inodes, dentries, pagecache, etc...)  These objects are automatically
reclaimed by the kernel when memory is needed elsewhere on the system.

Use of this file can cause performance problems.  Since it discards cached
objects, it may cost a significant amount of I/O and CPU to recreate the
dropped objects, especially if they were under heavy use.  Because of this,
use outside of a testing or debugging environment is not recommended.

You may see informational messages in your kernel log when this file is

    cat (1234): drop_caches: 3

These are informational only.  They do not mean that anything is wrong
with your system. To disable them, echo 4 (bit 2) into drop_caches.


linux, ram, bufferswe, quot, buffers, optimization, doesn, programs, allocate, swap, freed, allocations, realtechtalk, cached, mem, cache, echo, proc, sys, vm, drop_caches,

Latest Articles

  • Ubuntu Debian Linux Cannot Install Wine Solution - wine1.6 : Depends: wine1.6-i386 (= 1:1.6.2-0ubuntu14.2) but it is not installable wine1.4 : Depends: wine1.6 but it is not going to be installed
  • How To Install python 3.4 3.5 and up on Linux with wine - Working Solution
  • using Xvfb on virtual remote ssh server to have X graphical programs work
  • ssh Received disconnect from port 22:2: Too many authentication failures
  • named bind errors - DNSKEY: unable to find a DNSKEY which verifies the DNSKEY RRset and also matches a trusted key for '.'
  • OpenVZ vs LXC DIR mode poor security in LXC
  • httpd: Syntax error on line 221 of /etc/httpd/conf/httpd.conf: Syntax error on line 6 of /etc/httpd/conf.d/php.conf: Cannot load modules/ into server: /lib64/ symbol __h_errno, version GLIBC_PRIVATE not defined in file libc.s
  • Radeon R3 GPU on Debian Crashing
  • MySQL 5.7 on Debian and Ubuntu - How To Reset Root Password
  • SSH and sshfs timeout settings keepalive
  • Linux How To Add User To Additional Group
  • Howto Set Static IP on boot in initramfs for dropbear or other purposes NFS, Linux, Debian, Ubuntu, CentOS
  • Convert and install to LUKS Encrypted Drive Ubuntu 18.04 19.10 Linux Mint and Debian Based Linux
  • Debian and Netplan
  • CentOS 8 how to restart the network!
  • CentOS 8 how to convert to a bootable mdadm RAID software array
  • ADATA USB Thumb Drive Issues
  • KMODE EXCEPTION NOT HANDLED - QEMU/KVM Won't Boot Windows 2016 or 10 Image or Physical Machine
  • Linux Mint / Ubuntu / Debian Mate Disable Guest Session and Hide Usernames on Lightdm Login screen GUI
  • SSH How To Create Public/Private Key Pair and with a Larger Keysize than 2048 bits