Running out of system memory is the worst. Once you hit the limit, your system will start to utilize swap, if available. If it is, it will begin to thrash your hard disk in an attempt to stay up and available. Sadly, the attempt tends to be in vain as the system will begin to slow to a crawl. These times may leave you at a loss for what to be troubleshooting. Fortunately, there’s a command-line utility to help you diagnose these issues quickly and easily.
Knowing how to check your memory from the command-line may seem archaic, but on remote system where a system information GUI isn’t available, it’s your only hope.
Even on a desktop system, if things are too far gone, you may not even be able to open up the system information GUI to troubleshoot.
We’re going to be using the command
free. The command tends to be readily available on nearly all Unix-like operating systems like macOS and Linux.
On the off chance you don’t have the command available, consult with your system’s package manager to get them installed so you can follow along at home.
Alligator.io Recommends ⤵👉 Fullstack Advanced React & GraphQL by Wes Bos
Go fullstack and learn things like Next.js, styled-components, Apollo GraphQL and building a Node GraphQL backend by building a real world clothing store app.
Checking system memory
On the desktop, you can never have too much RAM / memory available. Chrome and it’s offshoots usually will eat up at least half of what you have available, with only a small handful of open tabs.
Servers on the other hand, can do a ton of heavy lifting without a ton of memory available. Because of this, when a server runs into an issue, it can exhaust the available memory quite quickly.
This causes the system to start utilizing the swap partition or file (if available) and will usually slow to a crawl as the disk starts to thrash.
Checking your system’s memory as well as swap, can be done with the
free command which will present you with a bunch of information about your system’s memory:
$ free total used free shared buff/cache available Mem: 32815856 4123580 21958592 1059220 6733684 27173368 Swap: 31249996 155456 31094540
Making it human readable
Those numbers are kind of intimidating, but can be corralled by passing in the
--human flag to make things a bit more human readable:
$ free -h total used free shared buff/cache available Mem: 31Gi 4.0Gi 20Gi 1.2Gi 6.6Gi 25Gi Swap: 29Gi 151Mi 29Gi
Ah, much better!
As you can see from the output, my system has 32GB of RAM. Wait… but that says it only has 31! That’s because
free reports the information in powers of 1024 and not 1000.
Using the power of 1000
To make things look a bit more like the marketing information for your system, you can pass in the
--si argument which will use a power of 1000 instead of 1024:
$ free -h --si total used free shared buff/cache available Mem: 32G 4.1G 21G 1.2G 6.8G 26G Swap: 30G 151M 30G
Phew, there we go!
In addition to the total amount of RAM, you can see how much is used, how much is free and available as well as information about how much is cached.
In addition to the physical memory, if you have swap available, you will see that as well.
Running multiple times
This is great if you want to see the current snapshot of the system’s memory, but what about when you’re actively monitoring an issue?
Sure, you could write a quick script to run the
free command every X seconds, or you could just use the
This argument lets you specify a delay, and the
free command will be run on that interface. You can even specify a floating point number if you want to run more frequently than once per second:
$ free -h --si -s5 total used free shared buff/cache available Mem: 32G 4.4G 21G 1.1G 6.7G 26G Swap: 30G 151M 30G total used free shared buff/cache available Mem: 32G 4.4G 20G 1.1G 6.7G 26G Swap: 30G 151M 30G total used free shared buff/cache available Mem: 32G 4.4G 21G 1.1G 6.7G 26G Swap: 30G 151M 30G ^C
Again and again, until you
CTRL+C to abort the process.
Running a fixed number of times
To take the
--seconds argument a step further, we can combine it with the
--count argument. This argument takes an integer value and tells
free to only run a certain number of times, on whatever delay we specified with the
$ free -h --si -s0.5 -c5 total used free shared buff/cache available Mem: 32G 4.4G 21G 1.1G 6.7G 26G Swap: 30G 151M 30G total used free shared buff/cache available Mem: 32G 4.4G 20G 1.1G 6.7G 26G Swap: 30G 151M 30G total used free shared buff/cache available Mem: 32G 4.4G 20G 1.1G 6.7G 26G Swap: 30G 151M 30G total used free shared buff/cache available Mem: 32G 4.4G 20G 1.1G 6.7G 26G Swap: 30G 151M 30G total used free shared buff/cache available Mem: 32G 4.4G 20G 1.1G 6.7G 26G Swap: 30G 151M 30G
Now instead of running indefinitely, the
free command will stop running on it’s own once it’s run the specified number of times.
We have it easy on our desktop systems. We have GUIs and can point and click our way into most of the information we may need.
When managing / administering remote machines, those luxuries aren’t generally available, so knowing how to do it from the command-line is important.
As always, if you’d like to learn even more about the
free command, open your terminal back up and type
man free to read all about it.
This is especially helpful because not every command is exactly the same on every system, so it’s good to be familiar with any nuances on your particular machines.