If your Linux server is randomly crashing, dropping database connections, or running incredibly slowly, you are likely experiencing an “Out of Memory” (OOM) event. When a server runs completely out of RAM, the Linux Kernel panics and begins violently killing critical processes (like your web server or database) to keep the machine alive.
To diagnose the problem before the server crashes, you must understand exactly how much RAM is currently being used, how much is being cached, and which specific application is consuming it. Because most servers lack a graphical interface, you must extract this data using terminal commands.
Method 1: The “free” Command (The Quick Overview)
The absolute fastest way to get a snapshot of your server’s current RAM status is the free command. It reads the raw system data and prints a clean, two-line table.
- Open your terminal or SSH into your server.
- Type this command exactly:
free -h
- Press Enter.
The -h flag stands for “human-readable.” Without it, Linux will output the data in raw kilobytes, which is incredibly difficult to read. With the flag, it converts the numbers to Gigabytes (G) or Megabytes (M).
You will see a table with several columns. You must understand how to read them:
- total: The absolute physical maximum RAM installed in the server (e.g., 16G).
- used: The RAM currently being actively consumed by your applications (e.g., 4.2G).
- free: RAM that is completely untouched and empty. Do not panic if this number is very low.
- buff/cache: RAM that Linux is temporarily borrowing to speed up hard drive access. This is good. If an application suddenly needs more RAM, Linux will instantly dump this cache and hand the memory over to the app.
- available: This is the most important number. This is the true amount of RAM you have left before the server crashes. It combines the “free” RAM with the disposable “buff/cache” RAM.
Method 2: The “top” Command (The Live Feed)
If the free command tells you that 15GB of your 16GB server is “used,” you now need to figure out which application is eating it. You must use the top command.
- Type
topand press Enter. - The terminal will transform into a live, continuously updating dashboard.
- By default,
topsorts applications by CPU usage, not RAM usage. To fix this, you must press a keyboard shortcut while the dashboard is running. - Press Shift + M on your keyboard.
The live table will instantly re-sort itself. Look at the column labeled %MEM. The application at the absolute top of the list is the one consuming the most RAM. (For example, you might see mysqld consuming 45% of the server’s memory). To exit the dashboard, press the Q key.
Method 3: The “htop” Utility (The Visual Upgrade)
While top is installed on every Linux machine by default, it is visually chaotic and difficult to read. The vast majority of system administrators prefer a modern, color-coded upgrade called htop.
- Install the utility using your package manager (e.g.,
sudo apt install htopon Ubuntu, orsudo yum install htopon CentOS). - Type
htopand press Enter.
The terminal will display a beautiful, color-coded interface. The top-left corner features live horizontal bar graphs showing exactly how much RAM and Swap space is being consumed in real-time. You can use your mouse to click on the “MEM%” column header to instantly sort the running applications, and you can even select a rogue app and press F9 to kill it directly from the interface.
Method 4: Read the /proc/meminfo File (The Deep Dive)
If you are writing a custom bash script for server monitoring, and you need to extract a highly specific data point (like the exact amount of “Active(anon)” memory), you cannot use htop. You must read the raw configuration file that the Linux Kernel generates.
Type this command:
cat /proc/meminfo
Press Enter. The terminal will dump a massive list of over 40 highly technical data points regarding the microscopic state of your system’s memory architecture. This is the exact file that the free command reads to generate its summary table.