Drop_caches, clean the RAM memory on your system from the terminal

About drop_caches

In the next article we will see how clean RAM from the Ubuntu terminal. By default, Gnu / Linux has a very efficient way of managing the RAM on our computers. With this we obtain a better performance of the resources available in our system. This approach can sometimes confuse users as RAM seems completely eaten up despite closing all applications.

Gnu / Linux uses available memory to cache applications from the hard drive, with the aim of achieve faster read times. This advantage becomes a frustrating experience, especially for system administrators who are troubleshooting PC problems. Changes applied to system files on the hard disk may not be read. This happens because Gnu / Linux is loading them from RAM. Therefore, to fix this problem, it is a good idea clean RAM instead of restarting the PC.

Clean RAM in Ubuntu, Linux Mint and derivatives with drop_caches

We are going to start the terminal (Ctrl + Alt + T) and type the following commands:

drop_caches run in terminal

sudo su

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

To start we let's log in as root. Then the command 'sync' going to clear the file system buffer. In this way, we will be sure that all the objects that are cached are released. Otherwise there could be problems. The command 'threw out'does the work of writing to the file and drop_caches is clearing the cache without removing any app / service. You should immediately see that the RAM is freed up.

If you have to clear the disk cache, "… Echo> 3…”Is safer in a company and in equipment in the production phase since«… Echo 1>….»Will delete only the page. It is not recommended to use the third option «… Echo 3>…»In production until you know what you are doing, since will clear the page cache, dentries, and inodes. The option "… Echo 0>…»Will not release anything, and the option«… Echo 2>…»Will free only inodes and dentries.

This operation it can slow down the system for a few seconds, while the cache is cleared and all the resources required by the OS are loaded back into the disk cache.

Gnu / Linux is designed in such a way that it examines the disk cache before searching the disk. If it finds the resource in the cache, then the request will not hit disk. If we clear the cache, the operating system will search for the requested resource on the disk.

Automate RAM release using Cron tasks

Now that we know how to free memory on our computers, we may want to automate the memory erasure process on a regular basis. This can be easily done by cron tasks. This task should be used only on desktops and laptops.

STEP 1

To begin, we will start the terminal (Ctrl + Alt + T) and write the following command to install vim, or that everyone use their preferred editor:

sudo apt-get install vim

STEP 2

Now we are going to create a .sh file called eraseram.sh. In it, we will add the script:

vim borraram.sh

STEP 3

vim drop_caches

If we use the vim editor, we will have to press the 'esc' key and then the 'i' key to enter INSERT mode. Next, we will add the following script.

#!/bin/bash
sync
echo "echo 3 > /proc/sys/vm/drop_caches"

The first line below is the shebang. Then we will write the same command that we use to erase the RAM from the terminal.

Once everything is written, we save the sh file and we will exit the vim text editor. To do this we will press 'esc' we will write : Wq and we will press Enter. Vim will save the sh file and exit to the terminal. For this example I have saved the script inside a root home folder.

STEP 4

Back in the terminal, we will write the following command to grant read / write permissions:

sudo chmod 755 borraram.sh

STEP 5

Now it's time to call the command crontab:

sudo crontab -e

STEP 6

drop_caches cron task

Suppose we want clear RAM every day at 1 PM. This is to everyone's taste.

0 13 * * * /root/scripts/borraram.sh

The sh file, we can move it to the location we want, but we must remember the path since that is the one we will have to give to the previous command.


Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   Ricardo Melgoza said

    Rosita Melgoza looks for you to see what suits you. Ask your teacher what OS you are going to install

  2.   Syslog said

    This post should be deleted, it contains a lot of incorrect and inaccurate information. It is not recommended to clear the disk cache, that does not help an end user at all. It is also a lie that "this advantage becomes a frustrating experience, especially for system administrators who are troubleshooting PC problems", because ??? Nor is it explained why you do a sync, ... it is an article that is confusing and erroneous.