Many Linux systems, servers and VPS’s run on low memory and over time you will see a degradation of speed and responsiveness. By default, Linux got excellent Memory Management and it knows when to clean up cache to free up enough Memory to execute the next command. However, saying that, more new features being added to Linux everyday and when you are playing games, running a Web Server, a Database (i.e. MySQL, PostgreSQL, MariaDB etc.), Network Storage (NAS / SAN ), you will see there’s a drop on speed and responsiveness. By deleting and cleaning pagecache
, dentries
and inodes
related cache data from Memory, you can get free up some of your Memory (RAM) which then makes rest of system work bit faster. This article will show you 3 different options to delete and clean cache to free up memory on your slow Linux server and small VPS’s.
Three drop_caches options to clean caches
Option 1: Free pagecache
This is suitable for webserver as they tend to store lots of pagecaches
.
To free pagecache:
sync; echo 1 > /proc/sys/vm/drop_caches
Option 2: Free dentries and inodes info
To free dentries
and inodes
:
sync; echo 2 > /proc/sys/vm/drop_caches
Option 3: Free the whole lot – pagecache, dentries and inodes
This the maximum memory cleanup you can do on any Linux system without killing a process.
To free pagecache
, dentries
and inodes
:
sync; echo 3 > /proc/sys/vm/drop_caches
Home users vs Servers
There are two types of Linux users:
- Home Users – i.e. Desktop
- Servers – i.e. WebServers
For Home users, duh! do whatever you want … you can just do much damage. You clean cache, and the system will be slowed for about 3 seconds and then re-populate memory with necessary files. You can safely run echo 3
to cleanup maximum memory:
sync; echo 3 > /proc/sys/vm/drop_caches
For Servers, (i.e. WebServer, A VPS with Low memory), it is much safer to use option 1
, cleaning pagecaches
only.
sync; echo 1 > /proc/sys/vm/drop_caches
Why? cause pagecaches
fills up massive part of your servers memory and in case of a Apache webserver, 50% of memory is used forpagecaches
.
Example scenarios
You need to run free -m
and drop_caches
to see the differences:
My Desktop
Run the following command to see how much memory is being committed:
free -m
Then run
sync; echo 3 > /proc/sys/vm/drop_caches
As you can see, my desktop was doing next to nothing, so there’s not much to cleanup.
My server
But because my server is always busy and it’s got a small memory pool (for a server), running drop_caches
makes a massive difference:
super@myserver [~]#
super@myserver [~]# free -m
total used free shared buffers cached
Mem: 12792 12151 1640 0 1177 11263
-/+ buffers/cache: 1711 12080
Swap: 11999 1131 11868
super@myserver [~]#
super@myserver [~]# sync; echo 1 > /proc/sys/vm/drop_caches
super@myserver [~]#
super@myserver [~]# free -m
total used free shared buffers cached
Mem: 12792 1831 11960 0 0 1132
-/+ buffers/cache: 697 12094
Swap: 11999 1131 11868
super@myserver [~]#
How awesome is that? In an instant I recovered a massive pool of memory and my terminal to the server became lot more responsive.