Clear Memory Cache on Linux Server

By default the Linux OS has a very efficient memory management process that should be freeing any cached memory on the machine that it is being run on. However when it comes to Cached memory the Linux OS may at times decide that the Cached memory is being used and is needed which can lead to memory related issues and ultimately rob your server of any potentially free memory. To combat this you can force the Linux OS to free up and stored Cached memory.

  1. Connect via shell using a program such as Putty
  2. At the shell prompt type crontab -e <enter> as this will allow you to edit cron jobs for the root user.
    • If you are not familiar with vi (linux editor) you press "i" to insert text and once done hit "esc" and type ":wq" to save the file.
  3. Scroll to the bottom of the cron file using the arrows key and enter the following line:
  4. 0 * * * * /root/clearcache.sh
  5. Create a file in '/root' called 'clearcache.sh' with the following content:
  6. #!/bin/sh 
    sync; echo 3 > /proc/sys/vm/drop_caches

  7. Once you have saved this file, the job is complete!

 

Every hour the cron job will run this command and clear any memory cache that has built up.

An example from a test server before and after running this task.

BEFORE:

AFTER:

Note before the server was using 1.918Gb of RAM with 1.4983Gb in Cache and after running the script the server is now only using 172Mb of RAM and only 38.9Gb in Cache.

  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

Installing htop

htop is an interactive process viewer for Linux, which is a powerful alternative to the...

Bind Multiple IP Addresses to a Single Network Interface Card (NIC)

This tutorial demonstrates how to bind multiple IP addresses to a single NICI'll be using LAN...

Set the time on a Linux server

Log into the server via SSHAt the shell prompt run the following command:date MMDDHHmmYYYYFor...

Check your disk usage in Linux

A good way to check your disk usage for folders on your Linux server is via shell.Download...

Add custom SpamAssassin rules for content filtering

Your local SpamAssassin rules should be stored in a file called /etc/mail/spamassassin/local.cf...