0

I need your advice on using this script on my Gnu/Linux debian server.

The server is used to host a website using magento platform. I have noticed that every 2-3 days when I check the memory it is showing 5 to 7 GB free memory from total of 64 GB.

I came across this website: How to Clear RAM Memory Cache, Buffer and Swap Space on Linux.

Where the script is as follows:

#!/bin/bash
# Note, we are using "echo 3", but it is not recommended in production
# instead use "echo 1" 
echo 3 > /proc/sys/vm/drop_caches
  1. The question is this, is this safe to do?
  2. Can some damage happen, if I put this into crontab, that runs it daily?

They have stated the following:

Is it a good idea to free Buffer and Cache in Linux that might be used by Linux Kernel? When you are applying various settings and want to check, if it is actually implemented specially on I/O-extensive benchmark, then you may need to clear buffer cache. You can drop cache as explained above without rebooting the System i.e., no downtime required.

Linux is designed in such a way that it looks into 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.

I noticed on some other websites the command to load up the memory looks like this :

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

Which is better what does "sync" before the echo do?

  • For the sync command, see: https://unix.stackexchange.com/questions/468600/what-does-the-sync-command-sync-in-fact – KevinO Feb 13 '20 at 21:55
  • 1
    for professional advice, you need to pay. "Profession: a paid occupation." – ctrl-alt-delor Feb 13 '20 at 22:01
  • What problems are you experiencing? Is it just the big numbers that are scary or are you seeing real problems. If you are not seeing real problems, then leave it alone. If you are seeing real problems, then this will (almost certainly) not fix it. The caching mechanism knows better that you and I, leave it to do its job. If it needs the memory for something important, then it will erase cache as needed. – ctrl-alt-delor Feb 13 '20 at 22:09
  • Are you running benchmarks on your server? – Stephen Kitt Feb 13 '20 at 22:10
  • Often, the less memory free the better your system is running. Unless you have problems just leave it alone. – Chris Davies Feb 13 '20 at 22:21

2 Answers2

1

My advice is don't!

Unless you have a very specific need for memory now, it is better to have the memory holding things that are potentially useful. The linux kernel will be able to reclaim the memory in less than a microsecond if it actually needs it. In contrast it will take tens of thousands of microseconds to get the data from traditional hard drives (a 7200rpm hard drive takes on average 4.2millseconds for the disk to rotate to the right spot, and seek times on higher end disks are also 4 milliseconds) if it turns out you do need it. Even ssd performance is going to crawl compared to ram

There are no power savings associated with not having useful information in memory.

Reasons for dropping the caches are mainly for benchmarks, particularly for bench-marking file-systems. One could imagine a hibernate tool also wanting to reduce the amount of main memory to have to write.

icarus
  • 17,920
0

Yes, it will reduce performance. But nothing else should break, to long as you don't introduce a but.

But why do it. There is no benefit. The kernel is trying to find a use for the otherwise unused memory.