[one-liner] Quickly Determine the Percentage of Used (Linux) Memory (RAM and SWAP)

Problem this snippet solves:

Starting in 13.x the linux kernel has increased swappiness, making it look like more RAM is in use. Well, that's because it is, and it's a good thing.

How to use this snippet:


Are you a fan of head-math?

Probably; Here is a challenge for you; look at the `free` output below and tell me what percentage of memory is used for each line of output (Mem:, -/+ buffers/cache:, Swap:)...Go!

Example `free` outupt.

# free

             total      used       free     shared   buffers    cached

Mem:      8199736   8091608    108128    105916     24532    392148

-/+ buffers/cache:   7674928    524808

Swap:     1023996     96020    927976

However let's pretend you're not a fan of head-math, or you simply like one-liners.

This is a simple one-liner to show you how much memory is actually used on the BIG-IP Linux Host system.

Pretty Memory Usage Output (In Percentage)

Straight-forward: "How much memory is each aspect of the Linux Host System using?"

Example Output (Percentages)

Mem Used:            98.6316

Used After Buffers:  6.83044

Swap Used:           9.37699

This output tells us that 98.6316 percent of RAM is used, however 91.80116 percent is just buffers/cache and is actually eligible to be used by any other service that needs it.

The takeaway here is that, though the memory used may appear to be high, that's actually a really good thing. It means that some data at some point was needed, and so it was cached in memory. It will now stay there until it is needed again or it gets pushed out by another process which needs that memory. Either way, this is a healthy system.

Code :

awk '
  while (("free" | getline) > 0 ) {
    if ($1 == "Mem:") {
      print("\nMem Used:\t\t", ($3 / $2) * 100)
    else if ($2 ~ "buffers") {
        print("Used After Buffers:\t", ($4 / $3) * 100)
    else if($1 == "Swap:") {
      print("Swap Used:\t\t", ($3 / $2) * 100)

Tested this on version:

Published May 16, 2019
Version 1.0

Was this article helpful?

No CommentsBe the first to comment