How to free Linux memory

Let’s manage some junk!

s0rtino
5 min readMar 22, 2021
My tiny resource bar
My tiny resource bar

In my parrot OS, I check memory usage via the graph on the top-bar. It is usually enough to keep an eye on the overall system situation. Sometimes (at least once every 10 days) I see something strange from the graph. Maybe some virtualization left some data allocated on the memory even if no VM is any more active, or just my hundred projects are creating a traffic jam on the system.

In these situations, I need a piece of more detailed information and make some trick. Let’s see something!

Check the free memory

Let’s start with a more detailed dashboard of my system memory:

watch -n 1 -d free -mth

The command is made in two chunks:

The cycle part order to run the check each # seconds, and memory check.

From the first one:

  • watch: Periodically run the provided command;
  • -n 1: interval of seconds between two command executions (in this case 1 second);
  • -d: enlight the differences with the previous run of the command.

While for the memory check itself:

  • free: shows the amount of free memory giving the detail of physical and swap memory of the OS. It also provides info on buffers and caches used at the kernel level;
  • -m: show the amount of memory in Megabytes;
  • -t: Provide also the total amount used;
  • -h: more human-friendly output.

Output example:

Example of script output
There is the enlighting: this is due to value change between scripts runs

This script gives also information about shared memory (Unix-system-way to manage temporary file data without using the persistent one) and buff/cache (the sum of kernel buffers and page cache).

HOW to clear memory

User-oriented Linux distros have the habit of reserving more RAM over a period of time since its boot. This caches holding allows to re-launch already used programs more quickly (strong simplification here).

Usually, the memory clear is done on the reboot (volatile memory) or also by the kernel in case of lack of RAM resources, also via swapping: it moves some data in the secondary memory, making the system slow.

But Linux user can act directly on the system memory, like forcing the write of data from the cache to the persistent memory or flush them if it is coming from a process no more in use.

Sync command

Tell the system to write back the cache on the secondary (and persistent) memory. The simple command is ok for general-purpose situations and does well it’s a job. But it is possible to force only data or metadata info the syntax is sync [options] [file], allowing to write on secondary memory the file ([file]). Useful in case of OS GUI freeze (if you prefer call it tty7).

Before moving to more specific options, we have to recall some aspect of Linux memory management.

An Inode is a data structure that provides a representation of a file, allowing the file system (generally “ext-family” FS) to find the physical location of data on the disk, and link data and metadata (like file name, privileges, and other information).

Dentries (directory entries) is a data structure that represents a directory or a folder. It is used to represent pathnames, speeding up the lookup operation thanks to the Filesystem three hierarchy.

Page cache is anything the OS could store in the primary memory to use it.

Then we can choose among these commands:

# Flush file system buffers by executing
sync;
# free page cache
sync; echo 1 > /proc/sys/vm/drop_caches;
# free dentries and inodes
sync; echo 2 > /proc/sys/vm/drop_caches
# free page cache, dentries and inodes
sync; echo 3 > /proc/sys/vm/drop_caches

As introduced before, sync will write the cache on the disk, then thanks to “;” as command separation, we will run sequentially the drop_cache, which has the task of clear the cache without killing any application/service. The first one (only page cache) is quite safe also in the production environment.

Is it a good idea?

Without considering the idea to clear the memory to speed up a little the whole system (the kernel is quite efficient but in long run), it is useful when you are doing serious changes to your system and you want to check the new behaviour without a strong influence on previous settings. This is possible also via rebooting, but it could be time-consuming and force a downtime.

Previous settings could be still in use even after the change because Linux looks into cache before looking onto the disk. For this reason, in some production context, it’s better to clear the cache and force the read on the disk.

Is it a good idea to do it automatically in… production?

No, full stop.

Imagine a web server. On each script run, all user connections will force a disk read on the server. This could bring to a system crash.

Of course, you can plan to run at 3 AM, but you cannot know in advance if the webserver is in some peak hours due to a different time zone or for some particular company event.

What about the swap?

The Kernel use RAM into chunks (a piece of data), and it could move some of them to the disk temporarily in the case is running out of volatile memory. This process is called swapping.

The use of this approach allows extending the RAM amount, allowing to allocated more process resources at the cost of speed degradation. There is not a fixed advisable value, and it strongly depends on current hardware (amount of RAM), type of applications used, and also the use of hibernation or other situations where the power is removed but you want to preserve the sessions.

Alternatively, it is possible to manage the swap as a file. This approach allows changing easily the swap size without requiring to play with disk partitions.

Like the cache, also the swap can be clear via the command line

# swapoff -a && swapon -a

Putting all together

A single script to clear RAM Cache and Swap could be something like:

# echo 3 > /proc/sys/vm/drop_caches && swapoff -a && swapon -a && printf '\\n%s\\n' 'Ram-cache and Swap Cleared'OR$ su -c "echo 3 >'/proc/sys/vm/drop_caches' && swapoff -a && swapon -a && printf '\\n%s\\n' 'Ram-cache and Swap Cleared'" root

--

--

No responses yet