Result of htop


cat /proc/sys/vm/swappiness


 root@server07:~# free

              total        used        free      shared  buff/cache   available

Mem:       16256132     4155932      553748      145196    11546452    10514464

Swap:      19721212     8327508    11393704

All of ram is being consumed by cache?

WHY? How can i release the cache from ram and move swap ram to real ram?


run this script once per day:


echo 1 >/proc/sys/vm/drop_caches
echo 2 >/proc/sys/vm/drop_caches
echo 3 >/proc/sys/vm/drop_caches

mem=$(LC_ALL=C free  | awk '/Mem:/ {print $4}')
swap=$(LC_ALL=C free | awk '/Swap:/ {print $3}')

echo $mem
echo $swap

if [ $mem -lt $swap ]; then
    echo "ERROR: not enough RAM" >&2
    exit 1

swapoff -a &&
swapon -a

Swap is not bad, disabling swap is. Read 'Why do I need swap' paragraph.

That being said, Run this command to turn of swap and turn it back on, essentially clearing the swap.

sudo swapoff -a && sudo swapon -a

You can write a script to do this periodically by doing

create the file

sudo nano /usr/local/sbin/swap2ram.sh

paste this code


mem=$(LC_ALL=C free  | awk '/Mem:/ {print $4}')
swap=$(LC_ALL=C free | awk '/Swap:/ {print $3}')

if [ $mem -lt $swap ]; then
    echo "ERROR: not enough RAM to write swap back, nothing done" >&2
    exit 1

swapoff -a &&
swapon -a

make executable

sudo chmod +x /usr/local/sbin/swap2ram.sh

At this point, you can run the command below to dump your swap back to memory (if enough memory available)

execute the script

sudo /usr/local/sbin/swap2ram.sh

make a cronjob to run it hourly (pick own preference)

sudo crontab -e
@hourly /usr/local/sbin/swap2ram.sh

see the log

grep CRON /var/log/syslog


My suggestion

Reducing swappiness is not a very good solution for desktop pc, although many sites will suggest you to do this 1st thing after you setup.

I would suggest to restore swappiness to default 60 and enable Zswap. It will compress and store your old pages in memory, rather than directly saving in swap. You can see this step by step guide.

I have it enabled and this is my mem consumption (with android studio, android emulator, chrome, atom running)

system monitor

  • thanks for reply.. :) however swapoff -a does not remove the buff/cache ram.. it just does not allow any ram to be written to swap while it is switching to to ram.. btw, we are on server environment.. I like the script, however there is still something needed to remove the cached ram.. :( Also isnt this bug of ubuntu/linux? if swappiness is set to really low number, it should not prefer to use swap instead of clearing the cache.. – Scholtz Apr 21 '17 at 14:43
  • even if i put echo 1 >/proc/sys/vm/drop_caches echo 2 >/proc/sys/vm/drop_caches echo 3 >/proc/sys/vm/drop_caches before the script, the script thinks there is enough of free ram, however it does not move the swap right away to the ram and ram gets consumed by new cache btw, can this force cleaning of ram cause the error in db ? for example if mysql thinks it wrote to harddrive, but linux is persisting this in cachce, this information that should go to hdd is flushed away? or this cache in ram is just for read cache? – Scholtz Apr 21 '17 at 15:02
  • Did you get any solution? – Sirajus Salekin Apr 23 '17 at 6:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.