When a Linux server suddenly becomes unresponsive or web applications start crashing unexpectedly, the most common culprit is a lack of available memory. If a runaway process consumes all the available RAM, the Linux kernel will trigger the “Out Of Memory” (OOM) killer to violently terminate applications in a desperate attempt to keep the system alive.
Before your server reaches that breaking point, you should routinely monitor your memory consumption. The fastest, most straightforward way to check your RAM in the Linux terminal is by using the free command.
The Basic free Command Output
To get a snapshot of your system’s memory, simply open your terminal, type the following command, and press Enter:
free
The command will output a small table displaying two rows: Mem (physical RAM) and Swap (virtual memory on your hard drive). However, you will immediately notice that the numbers are massive and completely unreadable, because the free command defaults to displaying data in kilobytes.
Making the Output Human-Readable
To translate those massive strings of numbers into gigabytes and megabytes, you must append the -h (human-readable) flag.
free -h
The output will now display easily identifiable values like 15G (Gigabytes) or 512M (Megabytes). The table contains several columns, and understanding exactly what they mean is crucial for accurate diagnostics.
Understanding the Columns
When looking at the Mem: row, Linux administrators often panic when they look at the “Free” column and see it is almost at zero. Do not panic. This is normal Linux behavior.
- Total: The total amount of physical RAM installed in the machine.
- Used: The memory currently being used by running applications and system processes.
- Free: Memory that is completely unassigned and doing absolutely nothing.
- Buff/Cache: This is the most misunderstood column. Linux hates wasted memory. If you have “Free” memory, Linux will automatically use it to cache files from the hard drive, making your server run faster. If an application suddenly needs more RAM, Linux will instantly dump this cache to make room. Therefore, cached memory is effectively “available” memory.
- Available: This is the only column that matters. It calculates how much memory is truly available for starting new applications without forcing the system into swap space.
How to Monitor Memory Continuously
If you are actively troubleshooting a memory leak, running the free command over and over again is tedious. You can use the -s (seconds) flag to force the command to refresh automatically.
For example, to continuously monitor your memory, refreshing the data every 3 seconds, type:
free -h -s 3
The terminal will print a new memory table every three seconds. You can launch your suspect application in a different terminal window and watch in real-time as the Available column shrinks. When you are finished monitoring, press Ctrl+C to terminate the continuous loop. This gives you unparalleled visibility into your server’s memory consumption.