I.T. Security and Linux Administration

Jul 5 2011   8:23PM GMT

Fetching real available memory

Eric Hansen Eric Hansen Profile: Eric Hansen

When running “free -m”, I would see my “Free” memory go down further, even after closing out programs and such. While I didn’t know why this was before (but knew that free also involved disk caching), I looked into it and saw that basically the “-/+ buffers” line was your actual used and free memory statistics. In short, so I don’t start making this mistake again, I wrote the following Bash script (yes, Bash and awk are my weapons of choice when scripting).

#!/bin/sh

if [ -n "$1" ]; then
TYPE="-$1"
else
TYPE="-m"
fi

echo "Getting free of type $TYPE."

free $TYPE | grep "-" | awk '{printf("Used: %d , Free: %d\n", $3,$4)}'

This script is pretty simple. You run it by passing an argument of what type of memory you are wanting to view (defaults to m [MB] if nothing is given, see man free for more inforamtion). This is the same argument you’d pass to the free command itself, but my default is MB because its easier for me to analyze.Since we are only interested in the “-/+ buffers” line, we grep it with a simple check, and then use printf() inside of awk to print out the numbers in an easy-to-read format. You can remove the echo line, it was for testing purposes that I just haven’t taken out.

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: