Drop RAM cache and clean swap GitHub

Contents of the material

How to free up cache memory in Linux

In every Linux distribution, you can use three commands to clear the Linux memory cache. Moreover, you do not have to complete any processes. First, log into the console as superuser:

sudo -i

Then run one of the commands. Clearing PageCache:

sync; echo 1 > /proc/sys/vm/drop_caches

Cleaning inode and dentrie:

sync; echo 2 > /proc/sys/vm/drop_caches

Cleaning inode and dentrie and PageCache:

sync; echo 3 > /proc/sys/vm/drop_caches

Now let’s look at what happens when these commands are executed.

The sync utility forces the system to write all cached but not yet written data to disk. This is necessary to free up as much memory as possible. By default, data is not deleted from the cache after being written to disk; this is necessary so that the program can read it faster if necessary.

If we do not execute the sync command, we will also free up some space, but after executing it, the result will be better.

Divide symbol; tells the shell to wait until the first command completes before executing another command. The last command echo 1 > /proc/sys/vm/drop_caches writes the value 1 to the file /proc/sys/vm/drop_caches. This signals to the kernel that we need to clear the type of cache we have selected.

Types of cache in Linux

Now let’s look at the types of caches that allow you to clear these commands, and how it all works.

PageCache or page cache is where the kernel puts all the data you wrote to or read from disk. This greatly speeds up the system, since if the program needs the same data a second time, it will simply be taken from RAM. But for this reason, this cache takes up the most space.

You can view the size of the page cache using the free utility. Here it is shown in the last column – cached:

free -h

This type of cache is the most efficient and safest to clear.

Inode and dentrie cache also applies to the file system. Only it is not the data itself that is written to it, but the structure of the file system, the location of files and folders. When asked for the location of a file or the contents of a folder, the kernel generates special structures that contain all this information. The next time the request is made, the structures will already be stored in memory. Each file system has its own inode cache and a shared dentrie cache.

This cache takes up very little memory. The data is presented in bytes, and as you can see, it is very small. You can view it with the command:

cat /proc/slabinfo | egrep dentry\|inode

It is not recommended to clear it to free up Linux memory, since little memory is consumed, and a new scan of the file system takes a relatively long time.

Do I need to clear the cache at all?

First, if there is a lot of memory being used, you may want to clear the page cache, especially if it is the page cache that is taking up a lot of memory. Secondly, you may need to clear the Linux memory cache if you have changed any file system or kernel settings, and now want to check how this affected the speed of read/write operations. In this case, you can clear all caches and do it without rebooting, which is very convenient.

The Linux operating system is designed in such a way that before accessing the disk, the disk cache will be looked at, and if the necessary data is there, the disk will not be accessed. If you clear the Linux cache, the operating system will run a little slower because it will have to look for data on the disk.

Automatic cache clearing

Let’s look at how to automatically clear the memory cache every day at two in the morning using the cron job scheduler.

First, let’s create a bash script with the following content:

sudo vi /usr/local/bin/clearcache.sh

#!/bin/bash
sync ; echo 1 > /proc/sys/vm/drop_caches

We will only clear the page cache, since it takes up the most. We will not touch other types, so as not to needlessly reduce system performance.

Next, make the script executable:

sudo chmod 755 /usr/local/bin/clearcache.sh

All that remains is to add the task to the cron scheduler. To do this, run the command:

sudo crontab -e

And in the editor that opens, add the line:

0 2 * * * /usr/local/bin/clearcache.sh

Now this script will run every night and clean up the memory so that the server can run normally.

How to Make the Most of Your RAM

Achieving the use of all available RAM is quite easy because it supports so many functions. Before you start removing programs from your computer, try these quick steps to free up RAM space.

Restart your computer

The first thing you can do to try to free up RAM is to restart your computer. When you restart or turn off your computer, all your RAM (saved data) will be erased and programs will be reloaded. This could potentially clean up some processes and programs that are running in the background and using your RAM.

Update your software

It is important that your computer has the latest software and installed applications. Older versions of software and applications may take up more memory to process, causing your computer to run slower.

Try a different browser

Something else you can try is to use a different browser, as some are known to use more data than others. Try using for example Chrome or Firefox, which are usually good browsers in terms of RAM consumption.

Clear your cache

If you are still running out of RAM, the next step is to try clearing your cache (cached data). Sometimes your cache can take up a lot of space because it uses RAM. The cache stores information that your computer uses to reload pages it previously opened so it doesn’t have to download them again. This can save you time when browsing, but if you don’t have enough RAM, you can sacrifice the cache without any problems.

Remove browser extensions

Surely, to simplify a number of operations, you have installed additional extensions in your browser. However, they also require constant use of RAM, so you can try disabling or even uninstalling these browser extensions and add-ons.

How can I find out how much RAM is being used?

To find out how much RAM is used on your computer, we will do the following:

  1. Let’s open the “Task Manager”. In Windows 10, this can be done by pressing the key combination Ctrl + Shift + Esc or through the context menu of the Start panel.

    You can also use the system search on the taskbar.

  2. We look at the list of running applications on the main page. Pay attention to the “Memory” column. After clicking on it, all processes will be sorted in descending order by the amount of resources used.
  3. We go to the “Performance” tab, and we will see more detailed information about RAM load. If this mark exceeds 80 percent, then it’s time to reduce the load.

How to prevent RAM overload?

To avoid computer freezes and any problems, you must strictly adhere to several rules:

  1. After you stop using the application, close it. After all, the fewer processes are running simultaneously, the lower the load on the RAM.
  2. No need to install extra programs. If there is no need for them, but at the same time you want them to be on the computer for show, it is better to abandon this idea. They do not occupy RAM when turned off, but run background processes. You won’t know about it, but the memory will be loaded.
  3. Clean your computer of unnecessary programs and check for viruses at least once a month. Viruses can also put a heavy load on RAM, and this needs to be taken into account.
  4. Use the automatic RAM cleaning function in system utilities or third-party programs. You can check it manually, but you do not need to stop or delete system processes, otherwise your computer may malfunction.

Third-party programs for cleaning RAM

If there is no desire or time for manual cleaning (and also when the above methods do not help), you can use programs from third-party developers.

RAM Rush

This application has one of the simplest user interfaces. It allows the user to monitor the consumption of processor and RAM resources in real time, while the program runs in quiet mode. The installer weighs no more than 540 kB.

Optionally, RAM Rush can be configured to automatically run optimization when free memory drops below 8 percent.

KCleaner

You can download it from the official website – the lite version weighs no more than 2.5 MB. After installation, one of the modes is selected in the program window – simple cleaning or with subsequent action (turning off the PC/rebooting/turning off the program).

When you click on the “Start” button, cleaning starts in the background. To view more detailed details, you can click on the “Expert Mode” panel.

CCleaner

CCleaner is not designed to clear RAM, but rather to remove cache and data from the clipboard. If the computer is used to work with documents, then this will be more than enough. If graphic applications are installed on the PC, then you will need to resort to other methods.

Mz RAM Booster

Mz RAM Booster is considered an analogue of the “Task Manager” from third-party developers. The program does not use many resources and does not load the RAM. Moreover, with its help you can even overclock the processor and optimize the performance of your PC.

Wise Memory Optimizer

The Wise Memory Optimizer utility automatically frees RAM from useless applications. To do this, you just need to download the application and install it. The user can independently set the necessary parameters and apply them.

CleanMem

CleanMem installs a small widget on your desktop. It displays brief information about the RAM load. The menu is entirely in English. To optimize memory, you just need to right-click on the icon and select “Clean Memory Now”.

Tags