-3

I have the following script to check free memory,

#!/bin/bash
THRESHOLD="500"
FREE_MEM=$(free -mh | awk '/^Mem:/{print $4}')

if [ "$FREE_MEM" -lt "$THRESHOLD" ]; then
        echo "insufficient storage. Available memory is ${FREE_MEM} MB"
fi

However, I am getting this error:

./memory_monitor.sh: line 6: [: 135Mi: integer expression expected
5
  • 1
    insufficient storage should probably be insufficient memory, right? Commented Dec 17, 2024 at 9:33
  • 2
    It is because you use -h switch and so it contains Gi/Mi like for example 600Mi, while it has to be pure integer Commented Dec 17, 2024 at 9:36
  • 1
    You likely want to know how much RAM is available rather than free / unused. A properly working system should aim to use most of the free RAM like in caches or buffers to speed up I/O operations for instance. Commented Dec 17, 2024 at 9:50
  • Agreed with StéphaneChazelas, while I have 32GB RAM on my laptop, it shows only 549Mi free, but 26Gi available. So, this is more of usual use case. Commented Dec 17, 2024 at 10:30
  • enable debug mode (#!/bin/bash -x), rerun script, and see what's being fed to the conditional; alternatively, after populating the variables add typeset -p FREE_MEM to display the actual contents of variable FREE_MEM; regardless of the method used you should find that FREE_MEM does not contain what you think it contains Commented Dec 17, 2024 at 21:27

2 Answers 2

3

On a Ubuntu 24.04.1 LTS system with 32GiB of memory running the free command from the script produces the following result:

$ free -mh | awk '/^Mem:/{print $4}'
25Gi

Notice that the use of the h (or --human) option has causes the output to be reported in units of gibibyte. From man free:

       -h, --human
              Show  all  output  fields automatically scaled to shortest three
              digit unit and display the units of print out.  Following  units
              are used.

                B = bytes
                Ki = kibibyte
                Mi = mebibyte
                Gi = gibibyte
                Ti = tebibyte
                Pi = pebibyte

The Gi suffix will lead to the integer expression expected error.

Therefore, remove the h option to get a value in mebibytes without a suffix. I.e.:

#!/bin/bash
THRESHOLD="500"
FREE_MEM=$(free -m | awk '/^Mem:/{print $4}')

if [ "$FREE_MEM" -lt "$THRESHOLD" ]; then
        echo "insufficient storage. Available memory is ${FREE_MEM} MiB"
fi

Or if you want megabytes (power of 1000) rather than mebibytes (power of 1024) as your MB suggests, use free --mega rather than free -m.

2

As the answer from "Chester Gillon" states, you need to use -m without h.

You can also use $7 for the output because free is not the available memory.

#!/bin/bash
THRESHOLD=500
FREE_MEM=$(free -m | awk '/^Mem:/{print $7}')

if [ "$FREE_MEM" -lt "$THRESHOLD" ]; then
    echo "Insufficient memory. Available memory is ${FREE_MEM} MiB"
else
    echo "Sufficient memory. Available memory is ${FREE_MEM} MiB"
fi

In the output of free,

Free memory is the amount of memory which is currently not used for anything. This number should be small, because memory which is not used is simply wasted.

Available memory is the amount of memory which is available for allocation to a new process or to existing processes.

What is available memory while using free command?

Meaning of "available" field in "free -m" command

What is available and free memory in response of free command on Linux?

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.