Giuseppe Parrello

 

How to clear the disk cache on a router


Introduction

The first thing we notice when buying a router is the drastic drop in available RAM when using an external USB drive. This issue has often been discussed on Internet discussion forums, especially on those routers that have at least 512 MB of RAM. This problem is seen as a router defect, in reality, as we will see later, it is a peculiar feature of all routers that have a Linux-based operating system.


Memory management in Linux

Many users are used to memory management in Microsoft Windows. Microsoft Windows reserves a minimal part of the memory available for the disk cache, this amount unfortunately does not increase with the increase of the Input/Output operations on disk, therefore those users who have for example 32 GB of RAM, see a lot of available RAM not used. Linux does not have this anomaly, Linux in fact uses all available RAM as disk cache memory in order to increase the Input/Output performance on disk. The RAM used by the router as a disk cache memory is then made available for applications and services when the latter require it. This dynamic management of the RAM makes many routers very fast, since a part of the data is stored in RAM during Input/Output operations.
Just as there is no way to increase the amount of RAM used as disk cache in Microsoft Windows, there is no way to reduce the amount of RAM used as disk cache in Linux. On the other hand, it is possible in Linux to free the disk cache memory with a specific command, which we will see later. This operation is definitely not recommended, since it is simply a temporary operation, it does not take anything away from Linux the possibility of occupying the available memory again to use it as disk cache memory for Input/Output operations. The definitive solution would be to remove the external USB disk, in this way Linux would no longer need to reserve a good part of the available memory to use it as disk cache memory for Input/Output operations, since the disk drive is missing. This obviously prevents the user from using external storage, the only possibility available in a router - a router does not have internal storage.


How to free the reserved cache memory

To free the disk cache memory reserved for Input/Output operations, the Linux command "sync; echo 3 > /proc/sys/vm/drop_caches" is used, this command consists of two different instructions. The first instruction allows to synchronize the data still present in the disk buffers and to empty the contents of the disk cache memory. The second instruction allows to free all the occupied buffers and therefore to free the occupied disk cache memory. This allows to recover all the RAM memory previously occupied by the disk cache memory.
As said before it is not recommended to perform these operations frequently, but even more it is not recommended to perform them during Input/Output operations, especially if performed by Linux services/daemons - a solution would be to stop the Linux services started by the user, perform the above operations and only then restart the previously stopped services. The execution of the above-mentioned Linux command during the Input/Output operations could cause data corruption, a malfunction of the router or even worse, the restart of the router itself.
To get an idea of the freed cache memory, we use the Linux "free" command, before and after running the above Linux command to free the cache memory. An example of the "free" command executed before and after is the following - note the "used", "free" and "cached" columns:

             total       used       free     shared    buffers     cached
Mem:        440420     411960      28460        828       6584     213140
-/+ buffers/cache:     192236     248184
Swap:       524284      46080     478204

             total       used       free     shared    buffers     cached
Mem:        440420     194904     245516        828        136      10892
-/+ buffers/cache:     183876     256544
Swap:       524284      46080     478204

Script to free disk cache memory

To free the disk cache memory, a specially designed Bash script can be used to write to a .LOG file inside the router so that the user can know when the cache memory's flush operation was performed and how much RAM has been freed - this script can be used via a schedule made with the Linux "crontab". An example of Bash script could be:

#!/bin/sh

CURRENTDATE="$(date +%Y%m%d_%H%M%S)"
NEWLOGFILE="/opt/var/log/drop_caches.log"

echo $CURRENTDATE
echo $NEWLOGFILE

echo "-----------------------------------------" >> $NEWLOGFILE
echo $CURRENTDATE >> $NEWLOGFILE
echo "   " >> $NEWLOGFILE
free >> $NEWLOGFILE
echo "   " >> $NEWLOGFILE
sync
sync
echo 3 > /proc/sys/vm/drop_caches
sync
free >> $NEWLOGFILE
echo "-----------------------------------------" >> $NEWLOGFILE