How to clean memory in linux

How to Clear Memory Cache in Linux

Memory cache is a critical component of any Linux system, helping to improve performance by storing frequently accessed data in a fast and easily accessible location. However, the cache can also become bloated over time, leading to degraded performance. Fortunately, it is relatively easy to clear the memory cache in Linux.

Clearing the memory cache is safe but not recommended every time. It can slow down the system performance as reading files from memory is much faster than persistent disk. Since it discards cached objects from memory, it may cost a significant amount of I/O and CPU to recreate the dropped objects.

This tutorial will help you to clear the memory cache on Linux/Unix system via the command line.

How to Clear Memory Cache on Linux

There are three options available to clear the memory cache in Linux. Choose one of the below options to flush the Linux system cache memory as per your requirements.

    Clear PageCache, dentries and inodes in cache memory. In short it will clear all the memory cache:

sync; echo 3 | sudo tee /proc/sys/vm/drop_caches 
sync; echo 2 | sudo tee /proc/sys/vm/drop_caches 
sync; echo 1 | sudo tee /proc/sys/vm/drop_caches 

Here the first command sync is used to synchronize all the in-memory cache files to the persistent storage. The next command is separated with a “;”. Once the first command is completed, the next command will be triggered to clear cache memory.

Scheduleng the Clear Memory Cache usiing Crontab

You can also schedule a corn job to clear the cache on a regular basis. Schedule the following in system crontab to automatically flush cache memory at a regular interval.

Open a terminal and execute ‘crontab -e’ command to edit crontab:

Append below entry to the file:

0 10 * * * sync; echo 3 | sudo tee /proc/sys/vm/drop_caches

The above cron will execute on every hour and flushes the memory cache on your system.

Читайте также:  Fdisk linux разбить раздел

On the production servers, it is not recommended to schedule a clear cache command. It can lead to data corruption or data loss. So beware before running the above command in a production environment.

How to find Cached Memory in Linux

Use free command to find out cache memory uses by Linux system. The output of the free command is like below

Output
total used free shared buffers cached Mem: 16050 15908 142 0 120 12953 -/+ buffers/cache: 834 15216 Swap: 0 0 0

Here the last column is showing cached memory (12953 MB) on Linux system. The -m option is used to show output MB’s.

Источник

How to Clear RAM Memory Cache, Buffer and Swap Space on Linux

Like any other operating system, GNU/Linux has implemented memory management efficiently and even more than that. But if any process is eating away your memory and you want to clear it, Linux provides a way to flush or clear ram cache.

How to Clear Cache in Linux?

Every Linux System has three options to clear cache without interrupting any processes or services.

1. Clear PageCache only.

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

2. Clear dentries and inodes.

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

3. Clear pagecache, dentries, and inodes.

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

Explanation of the above command.

sync will flush the file system buffer. Command Separated by “;” run sequentially. The shell waits for each command to terminate before executing the next command in the sequence. As mentioned in the kernel documentation, writing to drop_cache will clean cache without killing any application/service, command echo is doing the job of writing to file.

If you have to clear the disk cache, the first command is safest in enterprise and production as “. echo 1 > ….” will clear the PageCache only. It is not recommended to use the third option above “. echo 3 >” in production until you know what you are doing, as it will clear pagecache, dentries, and inodes.

Is it a good idea to free Buffer and Cache in Linux that might be used by Linux Kernel?

Free Buffer and Cache in Linux

When you are applying various settings and want to check, if it is actually implemented specially on the I/O-extensive benchmark, then you may need to clear the buffer cache. You can drop cache as explained above without rebooting the System i.e., no downtime required.

Читайте также:  Editor pdf linux libreoffice

Linux is designed in such a way that it looks into the disk cache before looking onto the disk. If it finds the resource in the cache, then the request doesn’t reach the disk. If we clean the cache, the disk cache will be less useful as the OS will look for the resource on the disk.

Moreover, it will also slow the system for a few seconds while the cache is cleaned and every resource required by OS is loaded again in the disk cache.

Now we will be creating a shell script to auto clear RAM cache daily at 2 am via a cron scheduler task. Create a shell script clearcache.sh and add the following lines.

#!/bin/bash # Note, we are using "echo 3", but it is not recommended in production instead use "echo 1" echo "echo 3 > /proc/sys/vm/drop_caches"

Set execute permission on the clearcache.sh file.

Now you may call the script whenever you are required to clear the ram cache.

Now set a cron to clear RAM cache every day at 2 am. Open crontab for editing.

Append the below line, save and exit to run it at 2 am daily.

0 2 * * * /path/to/clearcache.sh

For more details on how to cron a job, you may like to check our article on 11 Cron Scheduling Jobs.

Is it a good idea to auto clear the RAM cache on the production server?

Clear RAM Cache on Linux Production Server?

No! it is not. Think of a situation when you have scheduled the script to clear ram cache every day at 2 am. Every day at 2 am the script is executed and it flushes your RAM cache. One day for whatsoever reason may be more than expected users are online on your website and seeking resources from your server.

At the same time, the scheduled script runs and clears everything in the cache. Now all the users are fetching data from the disk. It will result in a server crash and corrupt the database. So clear ram-cache only when required, and known your footsteps, else you are a Cargo Cult System Administrator.

How to Clear Swap Space in Linux?

If you want to clear Swap space, you may like to run the below command.

Also, you may add the above command to a cron script above, after understanding all the associated risks.

Now we will be combining both above commands into one single command to make a proper script to clear RAM Cache and Swap Space.

# 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

After testing both the above commands, we will run the command “free -h” before and after running the script and will check the cache.

Читайте также:  Linux на смартфоне samsung

That’s all for now, if you liked the article, don’t forget to provide us with your valuable feedback in the comments to let us know, what you think is a good idea to clear ram cache and buffer in production and Enterprise?

Источник

How do I clean or disable the memory cache?

After the system starts, in a few minutes my memory cache fills up and it starts using the swap. Here’s a screenshot of /proc/meminfo. memoryHowever, if I can disable this process / clean the cache once after that, I think my system will speed up to some extent. Correct me if I am wrong here. I have also tried Bleachbit memory cleaning, but it doesn’t seem to clean the memory cache properly. Also, the feature is still in an experimental stage. bleachbit memoryThere was already a similar question posted here: How can I disable the prefetch cache?, but it was regarding Ubuntu Server, and also the answers involved manual settings etc. So, I want to know if there’s some software for disabling/enabling Memory Cache for Ubuntu 12.04.

Are you sure this will solve your problem? Memory doesn’t fill up without a reason. I also had the problem that my memory was filling up, and traced it back to tracker-miner . After I uninstalled all tracker packages, my problem went away. I also have 4GB memory, and 2GB are used by chaches, but I’m nor experiencing any problems.

@izx Yes, I know. It could just be that he looks for the solution of the wrong problem. The kernel is smart enough not to fill up memory with caches when it is needed by programs. That’s why I think that his memory problems come from a program that uses too much memory.

@danielkullmann: I am curious to know how you traced it back to one process that’s gobbling up all that memory. Is there some tool/command for that?

Источник

Оцените статью
Adblock
detector