Clearing Linux memory cache

Every day solutions to every day challenges. + Brilliant stuff

Moderators: b1o, jkerr82508

Forum rules
Please feel free to post your tip it does not have to be advanced. Also ask questions directly related to the tip here. But do not start new threads with questions or ask for help here. That is what the help section is for. forum rules: http://bjoernvold.com/forum/viewtopic.php?f=8&t=568
User avatar
viking60
Über-Berserk
Posts: 9351
Joined: 14 Mar 2010, 16:34

Clearing Linux memory cache

Postby viking60 » 21 Oct 2014, 00:01

I have a box with 2 G memory and I often see that it hits the roof and uses swap from time to time. so my htop indicates that more than 90% of the memory is used.

After having used x2go that cache seems to fill up quickly and stays there (and oh boy... Steam...).
When I check out the running processes in Atop they are not taking much but there is a cache of 1.2 G.
Linux mem will be maxed from time to time and that is mostly no problem, just Linux doing its job.
(If you have the memory - use it! It does make sense).

But when you use that program that goes beyond the limit your computer can freeze up a bit and applications can crash - typically games.
There is not much harm in (occasionally) removing it and frankly the output of Atop and Htop make more sense then.

So here we go:

Code: Select all

sudo sh -c "sync; echo 3 > /proc/sys/vm/drop_caches" 


..and your memory cache is gone. Htop looks nice again and in some strange way it feels way better to look at the mem in one of those programs now +1

If you do not have programs that push the limits - you do not need to do this -leave it to Linux.

But then there are those times when one program has made all lamps blink red....
Or you want to benchmark the speed of your disks - you need to clear the cache for that. (yup that cache normally makes your computer faster - not slower)
Or as a security feature.

If you want to clear your swap you can just turn it off and on again:

Code: Select all

swapoff -a
and

Code: Select all

swapon -a
Manjaro 64bit on the main box -Intel(R) Core(TM) i7 CPU 920 @ 2.67GHz and nVidia Corporation GT200b [GeForce GTX 275] (rev a1. + Centos on the server - Arch on the laptop.
"There are no stupid questions - Only stupid answers!"

Return to “Tips & Tricks”