Script automatically frees up memory

by Daniel Pham
Published: Updated:

This article will provide to you a script automatically frees up memory in Linux. This script is for Linux servers.

Commands to clear cache in Linux

You know, most of the memory in Linux will turn into cache during operation.

Linux provides the following three commands to clear the cache at different levels.

1. Command to clear PageCache only.

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

2. Command to clear dentries and inodes.

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

3. Command to clear PageCache, dentries and inodes.

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

My script will use the 3rd command above, you can edit it according to your needs.

Script automatically frees up memory

At this point, you can completely think that just using the above command is done, using the script for what.

Recommended Reading: Calculate percent of memory used in Linux

The problem is, Linux server works 24/24 and I can’t wake up 24/24. I need a tool to monitor server memory. I need it to automatically free the server’s memory if the memory exceeds a certain threshold (which I will declare).

My script will do the following two things.

  1. Every 1 minute, this script will check % of server memory usage (run through crontab).
  2. Automatically free memory if it exceeds 80% of memory.

Download the script and use it

script automatically frees up memory in linux server
Script automatically frees up memory in Linux server.

Use the following command to download the script to your server.

wget -O /opt/

Next, grant the script permission.

chmod 0700 /opt/

There are two ways you can use this script.

Recommended Reading: Script free disk space from deleted files

Using the manual, you can type one of the following commands to execute the script manually.

bash /opt/



Run the script automatically with crontab

The obvious thing here is that you want the script to work automatically, not manually.

We use crontab in Linux, setting it to automatically execute the script once every minute.

crontab -l | { cat; echo "# Check memory used every 1 minute & auto frees up memory if it over 80%"; } | crontab -
crontab -l | { cat; echo "*/1 * * * * /opt/"; } | crontab -


A simple script that will be very effective for your work. The services on the server now need a lot of memory, and if you don’t pay attention, memory overflow is possible. At that time, your service will be interrupted.

(This is an article from my old blog that has been inactive for a long time, I don’t want to throw it away so I will keep it and hope it helps someone).

0 0 votes
Article Rating

You may also like

Notify of
Inline Feedbacks
View all comments

DevOps Lite is a personal blog specializing in technology with main topics about DevOps, DevSecOps, SRE and System Administrator. Articles are shared for free and contributed to the community.



Subscribe my Newsletter for new blog posts. Stay updated from your inbox!

© 2021-2024 – All rights reserved.

Please write sources “” when using articles from this website.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Would love your thoughts, please comment.x

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.