Script automatically frees up memory

Share this post to your friends !

Script automatically frees up memory
5 (100%) 1 vote[s]

WriteBash - 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.

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 Script automatically frees up memory
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.

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

# bash /opt/


# /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.

If you liked this article, then please subscribe to our YouTube Channel for more video tutorials. You can also find us on Twitter and Facebook.

Share this post to your friends !
If you appreciate what we share in this blog, you can support us by:
  1. Stay connected to: Facebook | Twitter | Google Plus | YouTube
  2. Subscribe email to recieve new posts from us: Sign up now.
  3. Start your own blog with SSD VPS - Free Let's Encrypt SSL ($2.5/month).
  4. Become a Supporter - Make a contribution via PayPal.
  5. Support us by purchasing Ribbon Lite Child theme being using on this website.

We are thankful for your support.

single post bottom banner
«« »»

Got something to say? Join the discussion

Please keep in mind that all comments are subject to our Comment Policy. Your email address will not be published.
This site uses Akismet to reduce spam. Learn how your comment data is processed.