User Tools

Site Tools


clear_memory

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
clear_memory [2023/07/12 00:29] – created wigumsclear_memory [2024/07/23 16:19] (current) – external edit 127.0.0.1
Line 15: Line 15:
 <code>sync; echo 3 > /proc/sys/vm/drop_caches</code> <code>sync; echo 3 > /proc/sys/vm/drop_caches</code>
  
-Explanation of the above command.+===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. 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.
Line 21: Line 21:
 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. 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? 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 Free Buffer and Cache in Linux
  
Line 29: Line 31:
 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. 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.+Now we will be creating a shell script to auto clear RAM cache via a cron scheduler task. Create a shell script clearcache.sh and add the following lines. 
 + 
 +<code>#!/usr/bin/env bash 
 +LOGFILE=/var/log/mycron.log 
 + 
 +LOG() { 
 +        msg="$1" 
 +        echo "`date` - $msg" >> $LOGFILE 
 +
 + 
 +LOG "Dropping cached ram" 
 +sync; echo 3 > /proc/sys/vm/drop_caches 
 +LOG "Caches dropped"</code>
  
-#!/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. Set execute permission on the clearcache.sh file.
  
-chmod 755 clearcache.sh+<code>chmod 755 clearcache.sh</code>
  
 Now you may call the script whenever you are required to clear the ram cache. Now you may call the script whenever you are required to clear the ram cache.
Line 43: Line 54:
 Now set a cron to clear RAM cache every day at 2 am. Open crontab for editing. Now set a cron to clear RAM cache every day at 2 am. Open crontab for editing.
  
-crontab -e+<code>crontab -e</code> 
 + 
 +Append the below line, save and exit to run it at the top of the hour every hour. 
 + 
 +<code> *  *  *  *  /path/to/clearcache.sh</code> 
  
-Append the below line, save and exit to run it at 2 am daily. 
  
-0  2  *  *  *  /path/to/clearcache.sh+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.
  
-For more details on how to cron a jobyou may like to check our article on 11 Cron Scheduling Jobs. +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,
-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? How to Clear Swap Space in Linux?
  
 If you want to clear Swap space, you may like to run the below command. If you want to clear Swap space, you may like to run the below command.
  
-swapoff -a && swapon -a+<code>swapoff -a && swapon -a</code>
  
 Also, you may add the above command to a cron script above, after understanding all the associated risks. Also, you may add the above command to a cron script above, after understanding all the associated risks.
Line 66: Line 77:
 Now we will be combining both above commands into one single command to make a proper script to clear RAM Cache and Swap Space. 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'+<code>echo 3 > /proc/sys/vm/drop_caches && swapoff -a && swapon -a && printf '\n%s\n' 'Ram-cache and Swap Cleared'</code>
  
 OR OR
  
-su -c "echo 3 >'/proc/sys/vm/drop_caches' && swapoff -a && swapon -a && printf '\n%s\n' 'Ram-cache and Swap Cleared'" root+<code>su -c "echo 3 >'/proc/sys/vm/drop_caches' && swapoff -a && swapon -a && printf '\n%s\n' 'Ram-cache and Swap Cleared'" root</code>
  
  
clear_memory.1689121793.txt.gz · Last modified: 2024/07/23 16:19 (external edit)